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 MsgFile

Example

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

NameDescription
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.cs

Returns 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

data ReadOnlyMemory<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.cs

Reads the item of an .msg file from a stream, until its end.

public static MapiMessage Load(Stream stream, PstReadOptions? options = null)
Parameters
stream Stream
The stream.
options PstReadOptions?
The options, or null for 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.cs

Reads the item of an .msg file. The file is read in memory.

public static MapiMessage Load(ReadOnlyMemory<byte> data, PstReadOptions? options = null)
Parameters
data ReadOnlyMemory<byte>
The content of the file.
options PstReadOptions?
The options, or null for the default options. PstReadOptions.MaxPropertySize limits the size of the streams, and PstReadOptions.MaxEmbeddedMessageDepth the 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.cs

Reads the item of an .msg file.

public static MapiMessage Load(string path, PstReadOptions? options = null)
Parameters
path string
The path of the file.
options PstReadOptions?
The options, or null for 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.cs

Writes 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
item MapiMessageBuilder
The item: a message, a contact, an appointment, a task or a distribution list.
stream Stream
The stream.
template bool
true to write an Outlook template (.oft): Outlook creates a new item from it when it opens it.

Save(MapiMessageBuilder, string, bool)

MsgFile.cs

Writes 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
item MapiMessageBuilder
The item: a message, a contact, an appointment, a task or a distribution list.
path string
The path of the file.
template bool
true to write an Outlook template (.oft): Outlook creates a new item from it when it opens it.

ToByteArray(MapiMessageBuilder, bool)

MsgFile.cs

Returns 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

item MapiMessageBuilder
The item.
template bool
true to write an Outlook template (.oft).

Returns

byte[] The content of the file.