Властивості MAPI
Властивості MAPI
Кожен файл MSG зберігає дані як властивості MAPI — позначені пари ключ‑значення, ідентифіковані за допомогою
ID властивості та коду типу. Aspose.Email FOSS for .NET надає прямий доступ до цих властивостей через MapiMessage, MapiPropertyCollection та enum CommonMessagePropertyId.
Читання властивостей
Зручні властивості
MapiMessage надає найпоширеніші властивості як типізовані властивості C#:
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?Прямий доступ до властивості
Для властивостей, які не представлені як зручні поля, використовуйте GetPropertyValue:
// 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}");Перебрати всі властивості
// List all property keys (ID + type pairs)
foreach (var key in message.IterPropertyKeys())
Console.WriteLine($"Property: 0x{key:X8}");
// 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}");
}Запис властивостей
Встановити властивості нового повідомлення
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));Прямий доступ до колекції
MapiPropertyCollection надає Add, Set, Get та Remove:
// 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);Загальні ідентифікатори властивостей
Перелічення CommonMessagePropertyId надає іменовані константи для часто використовуваних ідентифікаторів властивостей MAPI:
| Властивість | Опис |
|---|---|
Subject | Тема повідомлення |
Body | Тіло у форматі простого тексту |
BodyHtml | Тіло у форматі HTML |
SenderName | Відображуване ім’я відправника |
SenderEmailAddress | Електронна адреса відправника |
DisplayTo | Рядок відображення отримувачів “To” |
DisplayCc | Рядок відображення отримувачів “Cc” |
DisplayBcc | Рядок відображення отримувачів “Bcc” |
InternetMessageId | RFC 5322 Message-ID |
MessageDeliveryTime | Час доставки |
TransportMessageHeaders | Необроблені заголовки RFC 5322 |
AttachFilename | Коротка назва файлу вкладення |
AttachLongFilename | Довга назва файлу вкладення |
AttachMimeTag | MIME-тип вкладення |
AttachContentId | Content-ID вкладення |
Коди типу власності
Перелічення PropertyTypeCode відображає коди типу MAPI:
| Тип | Опис |
|---|---|
PtypString | Unicode-рядок |
PtypString8 | ANSI-рядок |
PtypInteger32 | 32‑бітове ціле число |
PtypInteger64 | 64‑бітове ціле число |
PtypBoolean | Boolean |
PtypTime | DateTime (FILETIME) |
PtypBinary | Масив байтів |
PtypGuid | GUID |
Властивості вкладення
Кожен MapiAttachment також має свою власну колекцію Properties:
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}");
}Дивіться також
- Reading MSG Files — Завантажуйте та переглядайте файли MSG
- EML Conversion — Конвертуйте між форматами
- CFB Containers — Доступ до низькорівневих бінарних контейнерів