Lettura di file MSG
Lettura di file MSG
Aspose.Email FOSS for .NET fornisce tre livelli di accesso per la lettura dei file Outlook .msg:
l’API di alto livello MapiMessage, il livello intermedio MsgDocument/MsgReader e il livello basso CfbReader per l’ispezione binaria grezza.
Alto livello: MapiMessage
MapiMessage è la classe principale per la lettura dei file MSG. Espone proprietà tipizzate fortemente
per tutti i campi standard dell’e‑mail.
Carica da file o stream
using Aspose.Email.Foss.Msg;
// From file path
using var message = MapiMessage.FromFile("sample.msg");
// From stream
using var stream = File.OpenRead("sample.msg");
using var message2 = MapiMessage.FromStream(stream);Il parametro opzionale strict controlla il comportamento della convalida:
// Strict mode — populates ValidationIssues but does not throw
using var message = MapiMessage.FromFile("sample.msg", strict: true);
foreach (var issue in message.ValidationIssues)
Console.WriteLine($"Warning: {issue}");Accedi ai campi del messaggio
Console.WriteLine($"Subject: {message.Subject}");
Console.WriteLine($"Body: {message.Body}");
Console.WriteLine($"HTML Body: {message.HtmlBody}");
Console.WriteLine($"From: {message.SenderName} <{message.SenderEmailAddress}>");
Console.WriteLine($"Message-ID: {message.InternetMessageId}");
Console.WriteLine($"Date: {message.MessageDeliveryTime}");
Console.WriteLine($"Message Class: {message.MessageClass}");Leggi Destinatari
foreach (var recipient in message.Recipients)
{
var type = recipient.RecipientType switch
{
MapiMessage.RecipientTypeTo => "To",
MapiMessage.RecipientTypeCc => "Cc",
MapiMessage.RecipientTypeBcc => "Bcc",
_ => "Unknown"
};
Console.WriteLine($" {type}: {recipient.DisplayName} <{recipient.EmailAddress}>");
}Leggi gli allegati
foreach (var attachment in message.Attachments)
{
Console.WriteLine($" {attachment.Filename} ({attachment.MimeType})");
if (attachment.IsEmbeddedMessage)
{
Console.WriteLine($" Embedded subject: {attachment.EmbeddedMessage!.Subject}");
}
else
{
File.WriteAllBytes(attachment.Filename!, attachment.Data);
}
}Livello intermedio: MsgDocument e MsgReader
MsgDocument fornisce l’accesso all’albero di archiviazione MSG — archivi, flussi, destinatari e allegati come elementi strutturali.
using Aspose.Email.Foss.Msg;
var document = MsgDocument.FromFile("sample.msg");
// Navigate storages by role
foreach (var storage in document.Root.Storages)
{
Console.WriteLine($"{storage.Name} — role: {storage.Role}");
if (storage.Role == MsgStorageRole.Recipient)
Console.WriteLine(" (recipient storage)");
else if (storage.Role == MsgStorageRole.Attachment)
Console.WriteLine(" (attachment storage)");
}MsgReader fornisce accesso in sola lettura con convalida:
using var reader = MsgReader.FromFile("sample.msg", strict: true);
Console.WriteLine($"Validation issues: {reader.ValidationIssues.Count}");Livello basso: CfbReader
Per l’ispezione binaria grezza, usa CfbReader per attraversare direttamente il contenitore CFB:
using Aspose.Email.Foss.Cfb;
using var reader = CfbReader.FromFile("sample.msg");
// Traverse the full directory tree
foreach (var (depth, entry) in reader.IterTree())
{
var indent = new string(' ', depth * 2);
Console.WriteLine($"{indent}{entry.Name} [type={entry.ObjectType}, size={entry.StreamSize}]");
}
// Read raw stream data
var rootChildren = reader.IterChildren(CfbConstants.RootStreamId);
foreach (var entry in rootChildren)
{
if (entry.IsStream())
{
var data = reader.GetStreamData(entry.StreamId);
Console.WriteLine($"{entry.Name}: {data.Length} bytes");
}
}Vedi anche
- Features — Riferimento completo delle funzionalità
- EML Conversion — Converti tra EML e MSG
- CFB Containers — Accesso binario CFB a basso livello