TnefMessageAttachments Property

Get the attachments of the message.

Definition

Namespace: MimeKit.Tnef
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public IReadOnlyList<TnefAttachment> Attachments { get; }

Property Value

IReadOnlyListTnefAttachment
The attachments.

Remarks

Gets the attachments of the message, in the order in which they appeared in the TNEF stream.

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);
    }
}
                  

See Also