MAPI Properties
MAPI Properties
This guide shows how to read, iterate, and write MAPI properties in Outlook MSG files using Aspose.Email FOSS for .NET. Every MSG file stores data as tagged key-value pairs; access them through MapiMessage, MapiPropertyCollection, and the CommonMessagePropertyId enum.
Reading Properties
Convenience Properties
MapiMessage exposes the most common properties as typed C# properties:
using Aspose.Email.Foss.Msg;
using var message = MapiMessage.FromFile("sample.msg");
Console.WriteLine(message.Subject); // string?
Console.WriteLine(message.Body); // string?
Console.WriteLine(message.HtmlBody); // string?
Console.WriteLine(message.SenderName); // string?
Console.WriteLine(message.SenderEmailAddress); // string?
Console.WriteLine(message.InternetMessageId); // string?
Console.WriteLine(message.MessageDeliveryTime); // DateTime?
Console.WriteLine(message.MessageClass); // string?Raw Property Access
For properties not exposed as typed convenience fields, call GetPropertyValue() with the property ID, type code, and a decode flag to retrieve the raw value:
// Read a string property by ID and type
var displayTo = message.GetPropertyValue(
(ushort)CommonMessagePropertyId.DisplayTo,
(ushort)PropertyTypeCode.PtypString,
decode: true);
Console.WriteLine($"Display To: {displayTo}");Iterate All Properties
Call IterPropertyKeys() to enumerate all (property ID, type) pairs, or use IterProperties() to get the full property objects including tag and decoded value:
// List all property keys (ID + type pairs)
foreach (var (propId, propType) in message.IterPropertyKeys())
Console.WriteLine($"Property: 0x{propId:X4} (type: 0x{propType:X4})");
// Iterate full property objects
foreach (var prop in message.IterProperties())
{
Console.WriteLine($"ID=0x{prop.PropertyId:X4} " +
$"Type=0x{prop.PropertyType:X4} " +
$"Tag=0x{prop.PropertyTag:X8} " +
$"Value={prop.Value}");
}Writing Properties
Set Properties on a New Message
Create a MapiMessage with MapiMessage.Create(), then call SetProperty() with a CommonMessagePropertyId constant, a PropertyTypeCode, and the value to set:
var message = MapiMessage.Create("Test", "Body");
message.SetProperty(
(ushort)CommonMessagePropertyId.SenderName,
(ushort)PropertyTypeCode.PtypString,
"Alice");
message.SetProperty(
(ushort)CommonMessagePropertyId.SenderEmailAddress,
(ushort)PropertyTypeCode.PtypString,
"alice@example.com");
message.SetProperty(
(ushort)CommonMessagePropertyId.MessageDeliveryTime,
(ushort)PropertyTypeCode.PtypTime,
new DateTime(2026, 3, 15, 10, 30, 0, DateTimeKind.Utc));Direct Collection Access
MapiPropertyCollection exposes Add, Set, Get, and Remove methods for fine-grained control over individual MAPI properties without going through the typed convenience API:
// Access the property collection
var props = message.Properties;
// Add a property
props.Add(
(ushort)CommonMessagePropertyId.InternetCodepage,
(ushort)PropertyTypeCode.PtypInteger32,
65001, // UTF-8
flags: 0);
// Read it back
var codepage = props.Get(
(ushort)CommonMessagePropertyId.InternetCodepage,
(ushort)PropertyTypeCode.PtypInteger32);
Console.WriteLine($"Codepage: {codepage?.Value}");
// Remove a property
props.Remove(
(ushort)CommonMessagePropertyId.InternetCodepage,
(ushort)PropertyTypeCode.PtypInteger32);Common Property IDs
The CommonMessagePropertyId enum lists property IDs. Pass these constants to GetPropertyValue and SetProperty instead of numeric literals:
| Property | Description |
|---|---|
Subject | Message subject |
Body | Plain text body |
BodyHtml | HTML body |
SenderName | Sender display name |
SenderEmailAddress | Sender email address |
DisplayTo | To recipients display string |
DisplayCc | Cc recipients display string |
DisplayBcc | Bcc recipients display string |
InternetMessageId | RFC 5322 Message-ID |
MessageDeliveryTime | Delivery timestamp |
TransportMessageHeaders | Raw RFC 5322 headers |
AttachFilename | Short attachment filename |
AttachLongFilename | Long attachment filename |
AttachMimeTag | Attachment MIME type |
AttachContentId | Attachment Content-ID |
Property Type Codes
The PropertyTypeCode enum maps MAPI type codes:
| Type | Description |
|---|---|
PtypString | Unicode string |
PtypString8 | ANSI string |
PtypInteger32 | 32-bit integer |
PtypInteger64 | 64-bit integer |
PtypBoolean | Boolean |
PtypTime | DateTime (FILETIME) |
PtypBinary | Byte array |
PtypGuid | GUID |
Attachment Properties
Each MapiAttachment also has its own Properties collection. Iterate attachment.Properties.IterProperties() to enumerate all MAPI property entries for that attachment:
foreach (var attachment in message.Attachments)
{
Console.WriteLine($"Attachment: {attachment.Filename}");
foreach (var prop in attachment.Properties.IterProperties())
Console.WriteLine($" 0x{prop.PropertyTag:X8} = {prop.Value}");
}See Also
- Reading MSG Files — Load and inspect MSG files
- EML Conversion — Convert between formats
- CFB Containers — Low-level binary container access