MsgFile Class
Reads and writes Outlook items saved as files (.msg, [MS-OXMSG]) and Outlook templates (.oft), which have the format of the .msg files. The item of a file is read with the types of the items of Outlook data files: a MapiMessage, or a MapiContact, MapiAppointment, MapiTask or MapiDistributionList.
public static class MsgFileExample
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);Methods
| Name | Description |
|---|---|
| IsTemplate(ReadOnlyMemory<byte>) | Returns whether an .msg file is an Outlook template (.oft): the class identifier of its root storage is the one of the templates. |
| Load(Stream, PstReadOptions?) | Reads the item of an .msg file from a stream, until its end. |
| Load(ReadOnlyMemory<byte>, PstReadOptions?) | Reads the item of an .msg file. The file is read in memory. |
| Load(string, PstReadOptions?) | Reads the item of an .msg file. |
| Save(MapiMessageBuilder, Stream, bool) | Writes an item as an .msg file, or as an Outlook template (.oft), with Unicode strings. |
| Save(MapiMessageBuilder, string, bool) | Writes an item as an .msg file, or as an Outlook template (.oft), with Unicode strings. |
| ToByteArray(MapiMessageBuilder, bool) | Returns the content of an .msg file, or of an Outlook template (.oft), of an item. |
IsTemplate(ReadOnlyMemory<byte>)
MsgFile.csReturns whether an .msg file is an Outlook template (.oft): the class identifier of its root storage is the one of the templates.
public static bool IsTemplate(ReadOnlyMemory<byte> data)Parameters
dataReadOnlyMemory<byte>- The content of the file.
Returns
bool true for an Outlook template, false for another item.
Exceptions
InvalidEmailDataException- The file is not a compound file, or its structures are malformed.
Load overloads
Load(Stream, PstReadOptions?)
MsgFile.csReads the item of an .msg file from a stream, until its end.
public static MapiMessage Load(Stream stream, PstReadOptions? options = null)Parameters
streamStream- The stream.
optionsPstReadOptions?- The options, or
nullfor the default options.
Returns
MapiMessage The item.
Exceptions
InvalidEmailDataException- The file is not an .msg file, or its structures are malformed.
EmailResourceLimitException- A stream is larger than
PstReadOptions.MaxPropertySize.
Load(ReadOnlyMemory<byte>, PstReadOptions?)
MsgFile.csReads the item of an .msg file. The file is read in memory.
public static MapiMessage Load(ReadOnlyMemory<byte> data, PstReadOptions? options = null)Parameters
dataReadOnlyMemory<byte>- The content of the file.
optionsPstReadOptions?- The options, or
nullfor the default options.PstReadOptions.MaxPropertySizelimits the size of the streams, andPstReadOptions.MaxEmbeddedMessageDepththe depth of the embedded messages.
Returns
MapiMessage The item.
Exceptions
InvalidEmailDataException- The file is not an .msg file, or its structures are malformed.
EmailResourceLimitException- A stream is larger than
PstReadOptions.MaxPropertySize.
Load(string, PstReadOptions?)
MsgFile.csReads the item of an .msg file.
public static MapiMessage Load(string path, PstReadOptions? options = null)Parameters
pathstring- The path of the file.
optionsPstReadOptions?- The options, or
nullfor the default options.
Returns
MapiMessage The item.
Exceptions
InvalidEmailDataException- The file is not an .msg file, or its structures are malformed.
EmailResourceLimitException- A stream is larger than
PstReadOptions.MaxPropertySize.
Save overloads
Save(MapiMessageBuilder, Stream, bool)
MsgFile.csWrites an item as an .msg file, or as an Outlook template (.oft), with Unicode strings.
public static void Save(MapiMessageBuilder item, Stream stream, bool template = false)Parameters
itemMapiMessageBuilder- The item: a message, a contact, an appointment, a task or a distribution list.
streamStream- The stream.
templatebooltrueto write an Outlook template (.oft): Outlook creates a new item from it when it opens it.
Save(MapiMessageBuilder, string, bool)
MsgFile.csWrites an item as an .msg file, or as an Outlook template (.oft), with Unicode strings.
public static void Save(MapiMessageBuilder item, string path, bool template = false)Parameters
itemMapiMessageBuilder- The item: a message, a contact, an appointment, a task or a distribution list.
pathstring- The path of the file.
templatebooltrueto write an Outlook template (.oft): Outlook creates a new item from it when it opens it.
ToByteArray(MapiMessageBuilder, bool)
MsgFile.csReturns the content of an .msg file, or of an Outlook template (.oft), of an item.
public static byte[] ToByteArray(MapiMessageBuilder item, bool template = false)Parameters
itemMapiMessageBuilder- The item.
templatebooltrueto write an Outlook template (.oft).
Returns
byte[] The content of the file.