Overview
editAn .msg file is an Outlook item saved as a file (MS-OXMSG): an email, a contact, an appointment, a task... in a compound file (MS-CFB), the container of the documents of the old versions of Office. It stores the same MAPI properties as an Outlook data file, so the library reads and writes it with the same types.
Read a file
MsgFile.Load reads the item of a file, from a path, a stream or bytes. The item is a MapiMessage, or the type its class gives: MapiContact for IPM.Contact, MapiAppointment for IPM.Appointment, MapiTask for IPM.Task and MapiDistributionList for IPM.DistList. Everything the pages on the items describe applies: the recipients, the bodies, the attachments and the embedded messages, the properties and the named properties.
The file is read in memory, and its streams when they are accessed. PstReadOptions.MaxPropertySize limits the size of a stream, and MaxEmbeddedMessageDepth the depth of the embedded messages. A file that is not a compound file, or whose structures are malformed, throws an InvalidEmailDataException.
The strings of a file are in Unicode, or in the code page of the item in the files of old versions of Outlook (the "ANSI" files): the code page of the message (PidTagMessageCodepage), else its internet code page (PidTagInternetCodepage), else Windows-1252. The recipients and the attachments use the code page of their message.
Write a file
MsgFile.Save writes the item a builder describes, with the builders of the writer of Outlook data files: MapiMessageBuilder, MapiContactBuilder, MapiAppointmentBuilder... The strings are written in Unicode.
var message = new MapiMessageBuilder
{
Subject = "Quarterly report",
Body = "The report is attached.",
SenderName = "Alice",
SenderEmailAddress = "alice@example.com",
SenderAddressType = "SMTP",
};
message.Recipients.Add(new MapiRecipientBuilder("Bob", "bob@example.com"));
message.Attachments.Add(MapiAttachmentBuilder.FromContent("report.csv", "Quarter,Total\nQ1,42\n"u8.ToArray(), "text/csv"));
MsgFile.Save(message, path);
var item = MsgFile.Load(path);
Assert.Equal("Quarterly report", item.Subject);
Assert.Equal("bob@example.com", item.Recipients.Single().EmailAddress);
Assert.Equal("report.csv", item.Attachments.Single().Name);Outlook templates
An Outlook template (.oft) is an .msg file whose root storage has the class identifier of the templates: Outlook creates a new item from it when it opens it. MsgFile.Load reads it as an .msg file, MsgFile.IsTemplate tells it from an .msg file, and the template parameter of MsgFile.Save writes one:
var template = new MapiMessageBuilder
{
Subject = "Weekly status",
Body = "Done:\r\nNext:\r\n",
};
MsgFile.Save(template, path, template: true);
var bytes = File.ReadAllBytes(path);
Assert.True(MsgFile.IsTemplate(bytes));
Assert.Equal(EmailFileFormat.OutlookTemplate, EmailFormatDetector.Detect(bytes));
Assert.Equal("Weekly status", MsgFile.Load(bytes).Subject);Convert between .msg and PST files
MapiMessageBuilder.FromMessage copies an item, with all its properties, recipients and attachments. The copy of an item of an Outlook data file can be saved as an .msg file, and the item of an .msg file can be added to an Outlook data file with PstWriterFolder.AddItem:
using (var file = PstFile.Open(pstPath))
{
var item = file.GetFolder("Inbox")!.EnumerateItems().First();
MsgFile.Save(MapiMessageBuilder.FromMessage(item), msgPath);
}
var message = MsgFile.Load(msgPath);
Assert.Equal("Welcome to Microsoft Office Outlook 2003", message.Subject);
Assert.HasCount(11, message.Attachments);EmailMessage.FromMapi converts the item of an .msg file to the common model, to save it as an .eml file.