TnefAttachment Class

An attachment of a TNEF message.

Definition

Namespace: MimeKit.Tnef
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public sealed class TnefAttachment : IDisposable
Inheritance
Object    TnefAttachment
Implements
IDisposable

Remarks

A TnefAttachment holds the properties and content of a single attachment of a TnefMessage.

The content is buffered in memory when the message is loaded, so it may be read any number of times. If the attachment is an embedded message (see IsEmbeddedMessage), the content is the embedded TNEF message, which can be loaded on demand using LoadEmbeddedMessage(CancellationToken).

Example

C#
                    public static void ExtractAttachments (MimeMessage message, string outputDirectory)
{
    foreach (var part in message.BodyParts.OfType<TnefPart> ()) {
        using (var tnef = part.LoadTnefMessage ())
            SaveAttachments (tnef, outputDirectory);
    }
}

static void SaveAttachments (TnefMessage tnef, string outputDirectory)
{
    foreach (var attachment in tnef.Attachments) {
        if (attachment.IsEmbeddedMessage) {
            // An embedded message is a TNEF message in its own right, which may have its own attachments.
            using (var embedded = attachment.LoadEmbeddedMessage ())
                SaveAttachments (embedded, outputDirectory);
            continue;
        }

        if (!attachment.HasContent)
            continue;

        // Note: The file name comes from an untrusted source, so sanitize it before using it as a path.
        var fileName = Path.GetFileName (attachment.FileName);

        if (string.IsNullOrEmpty (fileName))
            fileName = "attachment.dat";

        using (var content = attachment.OpenRead ())
        using (var output = File.Create (Path.Combine (outputDirectory, fileName)))
            content.CopyTo (output);
    }
}
                  

Properties

ContentId Get the content identifier of the attachment.
FileName Get the file name of the attachment.
Flags Get the attachment flags.
HasContent Get whether the attachment has any content.
IsEmbeddedMessage Get whether the attachment is an embedded TNEF message.
Length Get the length of the attachment's content.
Method Get the attachment method.
MimeType Get the MIME type of the attachment.
Properties Get the attachment's MAPI properties.

Methods

Dispose Release all resources used by the TnefAttachment object.
Equals Determines whether the specified object is equal to the current object.
(Inherited from Object)
GetHashCode Serves as the default hash function.
(Inherited from Object)
GetType Gets the Type of the current instance.
(Inherited from Object)
LoadEmbeddedMessage Load the embedded message.
LoadEmbeddedMessageAsync Asynchronously load the embedded message.
OpenRead Open a stream for reading the attachment's content.
ToString Returns a string that represents the current object.
(Inherited from Object)

See Also