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
usingorDispose()onMapiMessageandCfbReaderto release file handles - Check
message.ValidationIssuesafter loading to detect non-standard MSG formatting - The
strictparameter inFromFile()/FromStream()controls error tolerance — usestrict: falsefor lenient parsing of files from third-party systems MapiMessage.Create()produces Unicode-string messages by default; setunicodeStrings: falsefor legacy ANSI compatibilitySaveToEml()without arguments returnsbyte[]— convenient for in-memory workflows
Common Issues
| Issue | Cause | Fix |
|---|---|---|
CfbException when loading | Not a valid CFB/MSG file | Verify the file is an Outlook MSG |
Empty Body after load | Body stored in HTML only | Check message.HtmlBody instead |
RecipientType not found | Wrong namespace | Use Aspose.Email.Foss.Msg.RecipientType |
0 attachments after LoadFromEml | EML has no Content-Disposition: attachment parts | Inline parts (Content-ID only) also appear in Attachments |
API Reference Summary
| Class | Description |
|---|---|
MapiMessage | High-level MSG message representation |
MapiAttachment | Attachment or embedded message on a MapiMessage |
MapiRecipient | Recipient on a MapiMessage |
MapiProperty | Single MAPI property entry |
MapiPropertyCollection | Typed MAPI property bag |
CommonMessagePropertyId | Enum of standard MAPI property identifiers |
MsgReader | Low-level MSG structure reader |
MsgWriter | Low-level MSG structure writer |
CfbReader | CFB binary container reader |
CfbWriter | CFB binary container writer |
CfbDocument | In-memory CFB document for construction |
CfbStorage | Storage node in a CFB document |
CfbStream | Stream node in a CFB document |