TnefFile Class

Reads and writes TNEF streams ([MS-OXTNEF]): the winmail.dat attachments (application/ms-tnef) Outlook and Exchange send with a message. The item of a stream is read with the types of the items of Outlook data files: a MapiMessage, or a MapiContact, MapiAppointment, MapiTask or MapiDistributionList.

public static class TnefFile

Example

var message = TnefFile.Load(path);

// The class of the old versions of Microsoft Mail (IPM.Microsoft Mail.Note) is read as its MAPI class
Assert.Equal("IPM.Note", message.MessageClass);
foreach (var attachment in message.Attachments)
{
    File.WriteAllBytes(Path.Combine(directory.FullName, Path.GetFileName(attachment.Name ?? "attachment")), attachment.GetContent());
}

Methods

NameDescription
HasSignature(ReadOnlySpan<byte>)Gets whether data starts with the signature of a TNEF stream.
Load(Stream, PstReadOptions?)Reads the item of a TNEF stream, until its end.
Load(ReadOnlySpan<byte>, PstReadOptions?)Reads the item of a TNEF stream.
Load(string, PstReadOptions?)Reads the item of a TNEF file, such as a winmail.dat file.
Save(MapiMessageBuilder, Stream)Writes an item as a TNEF stream, with Unicode strings.
Save(MapiMessageBuilder, string)Writes an item as a TNEF file, such as a winmail.dat file.
ToByteArray(MapiMessageBuilder)Returns the TNEF stream of an item.

HasSignature(ReadOnlySpan<byte>)

TnefFile.cs

Gets whether data starts with the signature of a TNEF stream.

public static bool HasSignature(ReadOnlySpan<byte> data)

Parameters

data ReadOnlySpan<byte>
The data.

Returns

bool true when the data starts with the signature.

Load overloads

Load(Stream, PstReadOptions?)

TnefFile.cs

Reads the item of a TNEF 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 data is not a TNEF stream, or its structures are malformed.
EmailResourceLimitException
A property is larger than PstReadOptions.MaxPropertySize.

Load(ReadOnlySpan<byte>, PstReadOptions?)

TnefFile.cs

Reads the item of a TNEF stream.

public static MapiMessage Load(ReadOnlySpan<byte> data, PstReadOptions? options = null)
Parameters
data ReadOnlySpan<byte>
The content of the stream.
options PstReadOptions?
The options, or null for the default options. PstReadOptions.VerifyChecksums checks the checksums of the attributes, PstReadOptions.MaxPropertySize limits the size of a property, and PstReadOptions.MaxEmbeddedMessageDepth the depth of the embedded messages.
Returns

MapiMessage The item.

Exceptions
InvalidEmailDataException
The data is not a TNEF stream, or its structures are malformed.
EmailResourceLimitException
A property is larger than PstReadOptions.MaxPropertySize.

Load(string, PstReadOptions?)

TnefFile.cs

Reads the item of a TNEF file, such as a winmail.dat 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 data is not a TNEF stream, or its structures are malformed.
EmailResourceLimitException
A property is larger than PstReadOptions.MaxPropertySize.

Save overloads

Save(MapiMessageBuilder, Stream)

TnefFile.cs

Writes an item as a TNEF stream, with Unicode strings.

public static void Save(MapiMessageBuilder item, Stream stream)
Parameters
item MapiMessageBuilder
The item: a message, a contact, an appointment, a task or a distribution list.
stream Stream
The stream.

Save(MapiMessageBuilder, string)

TnefFile.cs

Writes an item as a TNEF file, such as a winmail.dat file.

public static void Save(MapiMessageBuilder item, string path)
Parameters
item MapiMessageBuilder
The item.
path string
The path of the file.

ToByteArray(MapiMessageBuilder)

TnefFile.cs

Returns the TNEF stream of an item.

public static byte[] ToByteArray(MapiMessageBuilder item)

Parameters

item MapiMessageBuilder
The item.

Returns

byte[] The content of the stream.