Aspose.Email FOSS for .NET 是一个基于 MIT 许可证、无依赖的 C# 库,用于处理 Microsoft Outlook .msg 文件、复合文件二进制(CFB)容器和 EML 消息。只需添加一个 NuGet 包,即可立即开始读取、创建和处理电子邮件,而无需安装 Microsoft Outlook 或任何专有运行时。
该库提供两层访问方式。低层次上,CfbReader 和 CfbWriter 完全控制 CFB 二进制容器——遍历目录条目、读取和写入存储节点及流数据,并检查原始扇区布局。MsgReader 和 MsgWriter 在 CFB 之上处理 MSG 格式,公开 MAPI 属性流、收件人表和附件子存储。高层次上,MapiMessage 允许您从头创建新邮件,读取主题、正文、发件人和收件人,管理附件,并通过内置的 MIME 实现实现 MSG 与 EML 格式之间的转换。
该库面向 .NET 8.0 或更高版本,且没有本机依赖,使其适用于 Windows、Linux、macOS、Docker 容器以及无服务器函数。
CfbReader.FromFile(), CfbReader.FromStream(), or new CfbReader(data).IterStorages(), IterStreams(), IterChildren(), and IterTree().ResolvePath(names) to locate a specific stream by name chain.CfbDocument and serialize to bytes or file via CfbWriter.ToBytes() or CfbWriter.WriteFile().CfbStorage and CfbStream nodes to any storage node before serialization..msg files — direct CFB access enables forensic inspection and repair.MsgReader.FromFile() or MsgReader.FromStream() and access the underlying MAPI property streams, recipient records, and attachment sub-storages.MsgDocument with MsgWriter.ToBytes() or MsgWriter.WriteFile().MsgStorage, MsgStream, and MsgReader.ParseTopLevelPropertyStream().MsgStorage.FindStream() and MsgStorage.FindStorage() to locate named sub-entries..msg files from archive directories and extract metadata or attachments..msg files programmatically and deliver them to Outlook-based mail systems.MapiMessage.Create(subject, body), set SenderName, SenderEmailAddress, and HtmlBody.To, Cc, and Bcc recipients via AddRecipient() with email address and display name.AddAttachment() or construct MapiAttachment objects directly with Filename, MimeType, ContentId, and Data.message.Save() or message.Save(stream) and reload with MapiMessage.FromFile() or MapiMessage.FromStream().message.Attachments, and save each attachment.Data to disk.message.Subject, message.Body, message.SenderEmailAddress, and message.Recipients without COM automation..eml file (RFC 5322 / MIME) into a full MapiMessage object via MapiMessage.LoadFromEml(stream).MapiMessage back to MIME format with message.SaveToEml() or message.SaveToEml(stream)..eml and .msg files to a single format..eml fixtures in tests and verify they survive MSG round-trip without data loss.Open an Outlook MSG file from a stream and print the subject — no Microsoft Outlook required.
using System.IO;
using Aspose.Email.Foss.Msg;
using var stream = File.OpenRead("sample.msg");
var message = MapiMessage.FromStream(stream);
Console.WriteLine(message.Subject);
Build a complete email with sender, recipient, and attachment, then write it to an MSG file.
using System.IO;
using Aspose.Email.Foss.Msg;
var message = MapiMessage.Create("Hello", "Body");
message.SenderName = "Alice";
message.SenderEmailAddress = "alice@example.com";
message.AddRecipient("bob@example.com", "Bob");
using var attachmentStream = new MemoryStream("abc"u8.ToArray());
message.AddAttachment("note.txt", attachmentStream, "text/plain");
using var output = File.Create("hello.msg");
message.Save(output);
Load a standard .eml file and save it as an Outlook .msg file using the built-in MIME parser.
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);
using var emlOutput = File.Create("roundtrip.eml");
message.SaveToEml(emlOutput);