Read and Create Outlook Items

This article focuses on working with different Outlook elements, including Calendar, Contact, Task, Journal, Note, and Distribution lists. In Aspose.Email for .NET, these elements are represented by the following classes: MapiCalendar, MapiContact, MapiTask, MapiJournal, MapiNote, and MapiDistributionList. These classes serve as wrappers for the MapiMessage class to simplify the process of handling item data from MAPI properties, providing a more intuitive interface for accessing and manipulating item data.

To work with Outlook items programmatically in C#, we’re going to use Aspose.Email for .NET, a progressive library that allows you to easily manage emails, contacts, appointments, tasks, and other Outlook items. Some key features of the library include reading and writing emails in various formats, managing appointments and tasks, working with contacts, integrating with Exchange Server, and performing advanced email operations. In this blog post, we will demonstrate how to use it for interaction with Outlook items within .NET applications. Practical examples and code snippets are provided to help you understand and implement the functionalities of Aspose.Email for .NET seamlessly in your C# projects.

To leverage the power of the API, it is possible to either download its DLL or install it from NuGet using the following command:

PM> Install-Package Aspose.Email

General Approach

The general approach to working with Outlook items involves loading a MapiMesage, converting it to a specific Outlook item class (such as MapiContact or MapiCalendar), and then accessing or manipulating its properties. This approach simplifies working with MAPI properties and provides a straightforward way to manage different types of Outlook items. On the other hand, you can always return back to the MapiMessage object simply by calling the GetUnderlyingMessage method on any item object.

Manipulating Calendar Items

Outlook calendar items are represented by the MapiCalendar class. The class provides properties and methods to create, manipulate, and manage calendar items such as appointments, meetings, and events within an Outlook calendar. Users can use this class to easily create, modify, and interact with calendar items programmatically in their .NET applications. In the following paragraphs, you will learn how to create and read calendar items.

Reading Calendar Items

The code sample below demonstrates how to load a .msg file as a MapiMessage object. It then checks if the loaded message is of type Calendar using the SupportedType property. If it is indeed a Calendar item, the code casts the message as a MapiCalendar object and proceeds to output details of the calendar item.

The following are the steps to the code:

  1. Load the file as a MapiMessage object using the Load method.
  2. Check if the loaded message is of type Calendar.
  3. If it is a Calendar item, cast the message as a MapiCalendar object.
  4. Output the Location, Subject, Start Date, and End Date of the calendar item.

Creating a Calendar Item

To create a new calendar item and save it in different formats, use the following code sample provided by Aspose.Email:

  1. Create a new calendar item MapiCalendar with specific details such as location, subject, description, start time, and end time.
  2. Call the Save method on the calendar object with parameters specifying the name of the output file and the save options. The calendar item is saved as an ICS (iCalendar) file with the extension “.ics” using MapiCalendarSaveOptions.DefaultIcs.
  3. Similarly, the Save method is called again on the calendar object to save the calendar item as an MSG (Outlook message) file with the extension “.msg” using MapiCalendarSaveOptions.DefaultMsg.

Manipulating Contact Items

Aspose.Email offers the MapiContact class which is used for working with Microsoft Outlook contact items. It allows developers to create, read, update, and manipulate Outlook contacts in various formats such as MSG and VCF. This class provides properties and methods to set and retrieve contact information, including name, email address, phone numbers, job title, company, and other contact details.

The code samples below demonstrate how to use MapiContact class in creating and reading contacts.

Reading Contact Items

The process of reading a contact item involves loading a contact message from a MAPI message file and displaying details of the contact:

  1. Load a MapiMessage from a file using the MapiMessage.Load.
  2. Check if the MapiMessage’s supported type is a Contact.
  3. If it is a Contact, convert the MapiMessage to a MapiContact object.
  4. Display the Name, Email, and Company details of the contact.

Creating a Contact Item

Let’s create a new contact using the MapiContact class, set various properties such as name, email address, and company info, and then save the contact details to both a VCARD and MSG file formats.

The process involves the following steps:

  1. Create a new MapiContact object and initialize its properties including the name, email address, and company info.
  2. Save the contact details to a VCARD file format using the Save method with the ContactSaveFormat.VCard parameter.
  3. Save the contact details to a MSG file format using the Save method with the ContactSaveFormat.Msg parameter.

Manipulating Task Items

Aspose.Email provides a solution to efficiently manage Outlook tasks. The MapiTask class is an excellent choice for creating and reading tasks seamlessly. Let’s see how this class works in code.

Reading Task Items

To access and manipulate task items, load one from a file using MapiMessage.Load. With a few lines of code, you can retrieve important task details like the subject, status, and percentage complete.

  1. Load a task from the file using MapiMessage.Load.
  2. Check if the loaded message is of MapiItemType Task.
  3. If the message is a task, cast it to MapiTask type.
  4. Display task details by printing subject, status, and percent complete.

Creating a Task Item

The following code sample demonstrates how to create a task item, set its properties like title, description, start and due dates, completion percentage, and status, and save it as an MSG file:

  1. Create a new task object using MapiTask class with specified details like title, description, start date, end date, percent completion, and status.
  2. Set the PercentComplete property of the task and the Status property.
  3. Save the created task to a MSG file using the Save method with TaskSaveFormat.Msg as the format parameter.

Managing Journal Items

An Outlook journal entry is represented by the MapiJournal class. The following instructions will guide you on how to create and read a journal entry.

Reading a MapiJournal Item

To read a MapiJournal item, you can load the journal message using the MapiMessage.Load method. After verifying that the supported type is a Journal, you can retrieve and display the details of the journal such as its subject, document status, start time, and end time.

  1. Load a MAPI message from the file.
  2. Check if the supported type of the loaded message is a Journal item.
  3. If the message is of type Journal, convert it to a MapiJournal object.
  4. Output the Journal details including Subject, Status, Start Time, and End Time.

Creating a MapiJournal Item

Now we’re going to create a journal entry and include details. The entry will also contain a brief meeting description. This code demonstrates how to create and save a MapiJournal.

  1. Define a new journal entry using the MapiJournal class with specific details such as the title, description, meeting description, and discussion summary.
  2. Set the start and end time.
  3. Specify the company and categorize the entry.
  4. Save the created journal entry to a file using the Save method.

Managing Note Items

Aspose.Email offers the MapiNote class representing an Outlook note. It provides a solution to create and retrieve note information effectively. Let’s see how it works.

Reading a Note Item

To access and display the details of a note, load the note message using MapiMessage.Load method and retrieve the note object. Then, you can access and showcase specific details such as the subject, body, and color of the note for further processing.

  1. Load a MapiMessage from a file.
  2. If the loaded message is of type Note, convert the MapiMessage to a MapiNote object.
  3. Print the subject, body, and color of the converted MapiNote object.

Creating a Note Item

Creating a new note is easily achieved by defining the note’s subject, body, and color properties within a new MapiNote object. Once the note is configured, it can be saved as a message file using the Save method with the specified file format for future reference and management.

  1. Create a new MapiNote object with Subject, Body, and Color.
  2. Save the note to msg file by calling the Save method.

Managing Distribution Lists

To create and read an Outlook distribution list, we’re going to use the MapiDistributionList class. The following code samples with steps will help you understand how this functionality can be implemented into a project.

Reading a Distribution List

To read a distribution list item, begin by loading it from a file using the MapiMessage.Load method. If the loaded item is of type DistList, you can then display its details such as name and members’ information.

  1. Load a distribution list from a file.
  2. Check if the loaded message is of type distribution list (DistList).
  3. Convert the message to a MapiDistributionList object.
  4. Display the distribution list’s name and members.

Creating a Distribution List

For creating a new distribution list, use the Aspose.Email.Mapi namespace. Add members to it, specifying their names and email addresses. You can save the distribution list in VCF or MSG format for future use.

  1. Create a new distribution list with the MapiDistributionList class.
  2. Add members to the distribution list along with their email addresses.
  3. Save the distribution list to VCF format using the Save method.
  4. Save the distribution list to MSG format with the same Save method.

Conclusion

In this blog post, we have explored ways of reading and creating Outlook items such as calendars, contacts, tasks, journal entries, notes, and distribution lists programmatically in C#. For coding, we used Aspose.Email for .NET, a comprehensive library, which makes it easy to work with email-related tasks including manipulations of various Outlook elements. By leveraging the classes provided by the library, you can create, read, and manipulate these elements with ease. This post covered basic examples of each element, providing a foundation for more complex operations you may need in your applications. Intuitive code samples help you evaluate the library’s capabilities and understand how to implement its functionalities in your project.

Feel free to use our public resources:

  • API reference offers in-depth information on classes and methods.
  • Documentation provides comprehensive guides with examples.
  • Support forum allows users to seek help and discuss issues.
  • Blog features updates, tutorials, and best practices.

See Also