Właściwości MAPI
Właściwości MAPI
Każdy plik MSG przechowuje dane jako właściwości MAPI — oznaczone pary klucz‑wartość identyfikowane przez identyfikator właściwości i kod typu. Aspose.Email FOSS for .NET zapewnia bezpośredni dostęp do tych właściwości poprzez MapiMessage, MapiPropertyCollection oraz enum CommonMessagePropertyId.
Odczytywanie właściwości
Właściwości ułatwiające
MapiMessage udostępnia najczęstsze właściwości jako typowane właściwości 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?Surowy dostęp do właściwości
Dla właściwości nie udostępnionych jako pola wygodne, użyj 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}");Iteruj wszystkie właściwości
// 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}");
}Właściwości zapisu
Ustaw właściwości nowej wiadomości
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));Bezpośredni dostęp do kolekcji
MapiPropertyCollection zapewnia Add, Set, Get i 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);Wspólne identyfikatory właściwości
Enum CommonMessagePropertyId zapewnia nazwane stałe dla często używanych identyfikatorów właściwości MAPI:
| Property | Description |
|---|---|
Subject | Temat wiadomości |
Body | Treść w formacie tekstowym |
BodyHtml | Treść HTML |
SenderName | Wyświetlana nazwa nadawcy |
SenderEmailAddress | Adres e‑mail nadawcy |
DisplayTo | Łańcuch wyświetlany odbiorców (Do) |
DisplayCc | Łańcuch wyświetlany odbiorców DW |
DisplayBcc | Łańcuch wyświetlany odbiorców UDW |
InternetMessageId | Identyfikator wiadomości RFC 5322 |
MessageDeliveryTime | Znacznik czasu dostarczenia |
TransportMessageHeaders | Surowe nagłówki RFC 5322 |
AttachFilename | Krótka nazwa pliku załącznika |
AttachLongFilename | Długa nazwa pliku załącznika |
AttachMimeTag | Typ MIME załącznika |
AttachContentId | Identyfikator treści (Content-ID) załącznika |
Kody typów nieruchomości
Enum PropertyTypeCode mapuje kody typów MAPI:
| Typ | Opis |
|---|---|
PtypString | ciąg Unicode |
PtypString8 | ciąg ANSI |
PtypInteger32 | liczba całkowita 32‑bitowa |
PtypInteger64 | liczba całkowita 64‑bitowa |
PtypBoolean | wartość logiczna |
PtypTime | DateTime (FILETIME) |
PtypBinary | tablica bajtów |
PtypGuid | GUID |
Właściwości załącznika
Każdy MapiAttachment również ma własną kolekcję 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}");
}Zobacz także
- Odczyt plików MSG — Ładowanie i przeglądanie plików MSG
- Konwersja EML — Konwersja między formatami
- Kontenery CFB — Dostęp do niskopoziomowych kontenerów binarnych