Features and Functionalities

Features and Functionalities

Features and Functionalities

This page covers every major feature of Aspose.Email FOSS for .NET with working C# examples.


Reading MSG Files

Load an MSG file from a path or stream and access all of its properties:

using System.IO;
using Aspose.Email.Foss.Msg;

using var stream = File.OpenRead("message.msg");
var message = MapiMessage.FromStream(stream);

Console.WriteLine(message.Subject);
Console.WriteLine(message.Body);
Console.WriteLine(message.HtmlBody);
Console.WriteLine(message.SenderName);
Console.WriteLine(message.SenderEmailAddress);
Console.WriteLine(message.MessageDeliveryTime);
Console.WriteLine(message.InternetMessageId);

Use strict: false for lenient parsing of non-standard MSG files:

var message = MapiMessage.FromFile("message.msg", strict: false);

Check ValidationIssues to review any format warnings without throwing:

foreach (var issue in message.ValidationIssues)
    Console.WriteLine($"Warning: {issue}");

Creating MSG Files

Build a complete email with MapiMessage.Create() and serialize with Save():

using System.IO;
using Aspose.Email.Foss.Msg;

var message = MapiMessage.Create("Hello", "Body");
message.SenderName = "Alice";
message.SenderEmailAddress = "alice@example.com";
message.HtmlBody = "<p>Body</p>";
message.InternetMessageId = "<hello@example.com>";
message.MessageDeliveryTime = new DateTime(2024, 1, 2, 3, 4, 5, DateTimeKind.Utc);

message.AddRecipient("bob@example.com", "Bob");

using var output = File.Create("hello.msg");
message.Save(output);

Save() without arguments returns a byte[]. Save(path) writes to a file path directly.


Recipients

Add To, Cc, and Bcc recipients with AddRecipient():

message.AddRecipient("alice@example.com", "Alice");
message.AddRecipient("bob@example.com", "Bob", RecipientType.Cc);
message.AddRecipient("carol@example.com", "Carol", RecipientType.Bcc);

Iterate recipients from a loaded message:

foreach (var recipient in message.Recipients)
    Console.WriteLine($"{recipient.DisplayName} <{recipient.EmailAddress}>");

Attachments

Add file or stream attachments with metadata:

// From byte array
message.AddAttachment("report.pdf", pdfBytes, "application/pdf");

// From stream
using var attachStream = File.OpenRead("photo.png");
message.AddAttachment("photo.png", attachStream, "image/png");

// Inline image with Content-ID
message.AddAttachment("logo.png", logoBytes, "image/png", contentId: "logo@cid");

Read attachments from a loaded message:

foreach (var attachment in message.Attachments)
{
    Console.WriteLine($"Filename: {attachment.Filename}");
    Console.WriteLine($"MIME type: {attachment.MimeType}");
    Console.WriteLine($"Size: {attachment.Data.Length} bytes");
    File.WriteAllBytes(attachment.Filename!, attachment.Data);
}

Embedded Message Attachments

Attach one MapiMessage inside another — common in forwarded email chains:

using Aspose.Email.Foss.Msg;

var parent = MapiMessage.Create("Outer", "Parent body");
var child = MapiMessage.Create("Inner", "Child body");
child.SenderEmailAddress = "inner@example.com";
parent.AddEmbeddedMessageAttachment(child, "inner.msg");

parent.Save("outer.msg");

Read embedded message attachments:

var loaded = MapiMessage.FromFile("outer.msg");
foreach (var attachment in loaded.Attachments)
{
    if (attachment.IsEmbeddedMessage)
    {
        Console.WriteLine($"Embedded: {attachment.EmbeddedMessage!.Subject}");
    }
}

EML and MIME Conversion

Load an RFC 5322 .eml file into a MapiMessage for Outlook-compatible storage:

using System.IO;
using Aspose.Email.Foss.Msg;

using var input = File.OpenRead("message.eml");
var message = MapiMessage.LoadFromEml(input);

using var msgOutput = File.Create("message.msg");
message.Save(msgOutput);

Convert back to EML from an MSG file:

var message = MapiMessage.FromFile("message.msg");
using var emlOutput = File.Create("roundtrip.eml");
message.SaveToEml(emlOutput);

LoadFromEml() also accepts a string path or byte[]. SaveToEml() without arguments returns a byte[]. Subject, body, HTML body, sender, recipients, and all attachments (including inline images with Content-ID) are preserved through full EML ↔ MSG round-trips.


MAPI Property Access

MapiPropertyCollection provides typed access to MAPI properties via SetProperty() and GetPropertyValue(). Use CommonMessagePropertyId enum values for standard properties:

using Aspose.Email.Foss.Msg;

var message = MapiMessage.FromFile("sample.msg");

// Named convenience properties
Console.WriteLine(message.Subject);
Console.WriteLine(message.SenderName);
Console.WriteLine(message.MessageDeliveryTime);

// Iterate all MAPI property keys
foreach (var key in message.IterPropertyKeys())
    Console.WriteLine($"0x{(ushort)key.PropertyId:X4} / {key.PropertyType}");

CFB Container Access

Outlook MSG files are built on the Compound File Binary (CFB) format. CfbReader exposes the full directory tree for inspection and data extraction:

using System.Text;
using Aspose.Email.Foss.Cfb;

using var reader = CfbReader.FromFile("message.msg");

// Iterate all top-level children
foreach (var entry in reader.IterChildren(CfbConstants.RootStreamId))
    Console.WriteLine($"{(entry.IsStorage() ? "Storage" : "Stream")}: {entry.Name}");

// Navigate to a specific stream by path
var entry2 = reader.ResolvePath(["__substg1.0_0037001E"]);
if (entry2 is not null)
{
    var data = reader.GetStreamData(entry2.StreamId);
    Console.WriteLine(Encoding.Unicode.GetString(data));
}

Build and serialize a CFB document from scratch with CfbWriter:

using System.Text;
using Aspose.Email.Foss.Cfb;

var document = new CfbDocument();
document.Root.AddStream(new CfbStream("Notes", Encoding.UTF8.GetBytes("hello")));
var storage = document.Root.AddStorage(new CfbStorage("DataStore"));
storage.AddStream(new CfbStream("Payload", Encoding.UTF8.GetBytes("content")));

byte[] bytes = CfbWriter.ToBytes(document);
File.WriteAllBytes("output.cfb", bytes);

Tips and Best Practices

  • Use using or Dispose() on MapiMessage and CfbReader to release file handles
  • Check message.ValidationIssues after loading to detect non-standard MSG formatting
  • The strict parameter in FromFile() / FromStream() controls error tolerance — use strict: false for lenient parsing of files from third-party systems
  • MapiMessage.Create() produces Unicode-string messages by default; set unicodeStrings: false for legacy ANSI compatibility
  • SaveToEml() without arguments returns byte[] — convenient for in-memory workflows

Common Issues

IssueCauseFix
CfbException when loadingNot a valid CFB/MSG fileVerify the file is an Outlook MSG
Empty Body after loadBody stored in HTML onlyCheck message.HtmlBody instead
RecipientType not foundWrong namespaceUse Aspose.Email.Foss.Msg.RecipientType
0 attachments after LoadFromEmlEML has no Content-Disposition: attachment partsInline parts (Content-ID only) also appear in Attachments

API Reference Summary

ClassDescription
MapiMessageHigh-level MSG message representation
MapiAttachmentAttachment or embedded message on a MapiMessage
MapiRecipientRecipient on a MapiMessage
MapiPropertySingle MAPI property entry
MapiPropertyCollectionTyped MAPI property bag
CommonMessagePropertyIdEnum of standard MAPI property identifiers
MsgReaderLow-level MSG structure reader
MsgWriterLow-level MSG structure writer
CfbReaderCFB binary container reader
CfbWriterCFB binary container writer
CfbDocumentIn-memory CFB document for construction
CfbStorageStorage node in a CFB document
CfbStreamStream node in a CFB document