Proprietà MAPI
Proprietà MAPI
Ogni file MSG memorizza i dati come proprietà MAPI — coppie chiave‑valore contrassegnate identificate da un
ID proprietà e un codice tipo. Aspose.Email FOSS for .NET fornisce accesso diretto a queste proprietà
tramite MapiMessage, MapiPropertyCollection e l’enum CommonMessagePropertyId.
Lettura delle proprietà
Proprietà di convenienza
MapiMessage espone le proprietà più comuni come proprietà tipizzate 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?Accesso a proprietà grezzo
Per le proprietà non esposte come campi di convenienza, utilizzare 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}");Itera tutte le proprietà
// 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}");
}Proprietà di scrittura
Imposta le proprietà su un nuovo messaggio
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));Accesso Diretto alla Collezione
MapiPropertyCollection fornisce Add, Set, Get e 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);ID Proprietà Comuni
L’enumerazione CommonMessagePropertyId fornisce costanti nominate per gli ID di proprietà MAPI usati frequentemente:
| Property | Description |
|---|---|
Subject | Oggetto del messaggio |
Body | Corpo in testo semplice |
BodyHtml | Corpo HTML |
SenderName | Nome visualizzato del mittente |
SenderEmailAddress | Indirizzo email del mittente |
DisplayTo | Stringa di visualizzazione dei destinatari “A” |
DisplayCc | Stringa di visualizzazione dei destinatari “Cc” |
DisplayBcc | Stringa di visualizzazione dei destinatari “Ccn” |
InternetMessageId | Message-ID RFC 5322 |
MessageDeliveryTime | Timestamp di consegna |
TransportMessageHeaders | Header RFC 5322 grezzi |
AttachFilename | Nome file allegato breve |
AttachLongFilename | Nome file allegato lungo |
AttachMimeTag | Tipo MIME dell’allegato |
AttachContentId | Content-ID dell’allegato |
Codici Tipo di Proprietà
L’enumerazione PropertyTypeCode mappa i codici tipo MAPI:
| Tipo | Descrizione |
|---|---|
PtypString | stringa Unicode |
PtypString8 | stringa ANSI |
PtypInteger32 | intero a 32 bit |
PtypInteger64 | intero a 64 bit |
PtypBoolean | Booleano |
PtypTime | DateTime (FILETIME) |
PtypBinary | array di byte |
PtypGuid | GUID |
Proprietà dell’allegato
Ogni MapiAttachment ha anche la propria raccolta 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}");
}Vedi anche
- Lettura di file MSG — Carica e ispeziona i file MSG
- Conversione EML — Converti tra formati
- Contenitori CFB — Accesso a contenitori binari a basso livello