Manag MAPI Properties

Managing email data programmatically can be complex, particularly when dealing with the Messaging Application Programming Interface (MAPI) properties. These properties are attributes or metadata associated with MAPI objects such as messages, folders, recipients, and other components in a messaging system. These properties provide detailed information about the object, like the subject of an email, the sender’s address, the date an email was sent, and more. MAPI properties are used extensively in email clients and other messaging applications to manage and manipulate email data. This article delves into various techniques for accessing, enumerating, and manipulating MAPI properties using Aspose.Email for .NET.

.NET API to Manage MAPI Properties in C#

Aspose.Email for .NET provides comprehensive support for handling Messaging Application Programming Interface properties, allowing developers to manage and manipulate email data with detailed control. It is also an extensive API that allows developers to equip their email applications with robust features for message processing and other related tasks.

Using Aspose.Email for .NET, developers can create sophisticated email management and processing solutions. To begin utilizing the library, you can download it from the Aspose website or NuGet and integrate it into your project.

Categories of MAPI Properties

Make sure you have Aspose.Email in your project and let’s get started. MAPI properties can be categorized into three main types: standard properties, named properties, and custom (user-defined) properties. Each category serves a different purpose and provides varying levels of flexibility and control for developers working with email data. Here’s an overview of each type:

Standard Properties

Defined by the MAPI specification, they have predefined property tags and are commonly used for standard attributes of messaging objects. For example:

Named Properties

Identified by a GUID and a string name or an integer identifier, these properties allow for more specific categorization and identification beyond standard properties.

Custom (User-defined) Properties

Defined by developers, they extend the functionality of MAPI objects, allowing for customized and application-specific data handling.

Listing All MAPI Properties

To list all MAPI properties in a message, we are going to use the MapiMessage.Load method to load the message and iterate over its properties. The code sample below demonstates how to load a MAPI message, iterate through its properties, and print detailed information about each property using Aspose.Email for .NET:

  1. Load the MAPI message file from the specified path.
  2. Iterate over msg.Properties.Values collection to access each property.
  3. For each MAPI property, print out its tag, canonical name, and data type:
    • Tag property provides the unique identifier for the MAPI property.
    • Descriptor.CanonicalName property gives the human-readable name of the property.
    • Descriptor.DataType property indicates the data type of the property.

Listing Named MAPI Properties

While regular MAPI properties are identified by a 16-bit property tag consisting of a property type and a property identifier, named properties are identified by a combination of a GUID and either a string name or an integer identifier:

  • GUID (Globally Unique Identifier): A unique identifier that ensures that the property set is unique. This GUID is usually provided by the developer.
  • Name or ID:
    • A string name (case-sensitive).
    • A 32-bit integer identifier.

The following code snippet demonstrates how to use Aspose.Email for .NET to iterate through named MAPI properties of a message and print out detailed information based on the property descriptor type:

  1. Iterate through each named property by accessing the ‘NamedProperties.Values’ collection of the loaded MapiMessage object to get all named MAPI properties.
  2. Check if the Descriptor of the current named property is of type PidNamePropertyDescriptor. If it is, cast the Descriptor to PidNamePropertyDescriptor. Print out the GUID, Canonical Name, and Data Type of the property.
  3. Check if the Descriptor of the current named property is of type PidLidPropertyDescriptor. If it is, cast the Descriptor to PidLidPropertyDescriptor. Print out the GUID, Canonical Name, Long ID, and Data Type of the property.

Thus, we have printed specific attributes like PropertySet, CanonicalName, DataType, and LongId deprnding on the descriptor type (PidNamePropertyDescriptor or PidLidPropertyDescriptor).

Retrieving Properties by Descriptor

Retrieving properties by descriptor is a straightforward way to access specific MAPI properties without iterating through the entire property collection. Aspose.Email for .NET provides a convenient way to retrieve properties using the KnownPropertyList. The code sample below demonstrates how to retrieve and print the details of a specific property, such as the InternetMessageId, if it exists.

  1. Access the InternetMessageId property from the Properties collection of the MapiMessage object using the KnownPropertyList.
  2. Verify that the property is not null to ensure it exists in the message.
  3. Print details of the retrieved property: Tag, Canonical Name, and Data Type.
  4. If the property data type is a string, use the GetString method to retrieve the property value and print it.

Retrieving Properties by Tag

A property tag is a 32-bit identifier that uniquely identifies a MAPI property. It consists of a property ID and a property type. Using property tags, you can directly access specific properties within a MAPI message. The code sample below demonstrates how to retrieve and print the details of a specific property, such as PR_HASATTACH, if it exists.

  1. Use the MapiPropertyTag to directly access the PR_HASATTACH property from the Properties collection of the MapiMessage object.
  2. Verify that the property is not null to ensure it exists in the message.
  3. Print details of the retrieved property: Tag and Data Type.
  4. If the property data type is boolean, use the GetBoolean method to retrieve the property value and print whether the message has attachments.

Using the GetProperty Method

The GetProperty method simplifies the process of retrieving both regular and named properties from a MAPI message. This method allows you to directly access a property by its descriptor or tag without manually checking the properties collection. The code sample below demonstrates how to utilize the GetProperty method for accessing specific MAPI properties:

  1. Use the GetProperty method to directly access the CurrentVersionName property from the KnownPropertyList class.
  2. Verify that the property is not null to ensure it exists in the message.
  3. Use the GetString method to retrieve the property value and print it.

Here, GetProperty is used to access the CurrentVersionName property directly.

Working with Custom Properties

Custom MAPI properties are additional properties that are not part of the predefined set of MAPI properties. They are created to hold application-specific data and are defined by the developer. By allowing developers to define and use additional properties, MAPI provides a flexible and powerful way to store and manage custom data within messaging applications. Custom properties are named properties (used a GUID and string name). Aspose.Email introduces an approach that you can use to fetch all custom properties defined in the message. This is achieved with the GetCustomProperties method. The code sample below demonstartes how to fetch and print all custom properties defined in a MAPI message:

  1. Use the GetCustomProperties method to obtain a collection of all custom properties defined in the message.
  2. Iterate through each custom property in the retrieved collection.
  3. Print property details: Tag, Canonical Name, and Data Type.

Conclusion

This article has explored various techniques for accessing, enumerating, and manipulating MAPI properties using Aspose.Email for .NET. Aspose.Email provides comprehensive support for handling these properties, allowing developers to manage and manipulate email data with detailed control. By using this library, developers can create sophisticated email management and processing solutions, making it easier to work with standard, named, and custom properties.

Additionally, Aspose.Email provides comprehensive documentation, an extensive API reference, and a variety of free online tools and apps to enhance your development process. Developers can also access a free support forum for community assistance and insights, and stay updated with the latest tips and tutorials through the Aspose blog. These resources are invaluable for maximizing the potential of the library in your projects.

See Also