Reading MSG Files

Reading MSG Files

This guide shows how to read Outlook .msg files using Aspose.Email FOSS for .NET. Three access levels are available: the high-level MapiMessage API for typed field access, the mid-level MsgDocument/MsgReader for structural inspection, and low-level CfbReader for raw binary traversal.


High-Level: MapiMessage

MapiMessage is the primary class for reading MSG files. It exposes strongly-typed properties for all standard email fields.

Load from File or Stream

Call MapiMessage.FromFile() with a file path, or MapiMessage.FromStream() with an open stream, to load the MSG data into memory:

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);

The optional strict parameter enables validation and populates the ValidationIssues list without throwing exceptions:

// 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}");

Access Message Fields

Read subject, body, sender, message ID, delivery time, and message class from the strongly-typed properties on MapiMessage:

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}");

Read Recipients

Iterate message.Recipients and check RecipientType to distinguish To, Cc, and Bcc recipients by their display name and email address:

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}>");
}

Read Attachments

Iterate message.Attachments to access filename, MIME type, raw bytes, and any embedded MapiMessage for nested MSG attachments:

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);
    }
}

Mid-Level: MsgDocument and MsgReader

Use MsgDocument.FromFile() to open an MSG file and navigate the root storage, which contains recipient and attachment sub-storages accessible by their Role property.

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 gives read-only access with validation:

using var reader = MsgReader.FromFile("sample.msg", strict: true);
Console.WriteLine($"Validation issues: {reader.ValidationIssues.Count}");

Low-Level: CfbReader

For raw binary inspection, use CfbReader to traverse the CFB container directly:

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");
    }
}

See Also

See Also

 English