AttachmentGetAttachmentsFromTnef Method (TnefExtractionOptions)
Extracts items of the specified kinds from TNEF container and returns them as AttachmentCollection.

Namespace: MailBee.Mime
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.5.0 build 687 for .NET 4.5
Syntax
public AttachmentCollection GetAttachmentsFromTnef(
	TnefExtractionOptions options
)

Parameters

options
Type: MailBee.MimeTnefExtractionOptions
Specifies which kinds of items (attachments and/or RTF message body) to extract from TNEF.

Return Value

Type: AttachmentCollection
An AttachmentCollection object representing the collection of the requested items extracted from TNEF, or a null reference (Nothing in Visual Basic) if the attachment is not a valid TNEF container.
Exceptions
ExceptionCondition
MailBeeExceptionAn error occurred and ThrowExceptions is true.
Remarks

Prior to calling this method, check IsTnef property value to determine whether the attachment is TNEF or not.

If winmail.dat contains nested winmail.dat files, they will be decoded as well as "message/rfc822" attachments. To process them, check IsMessageInside property and GetEncapsulatedMessage method to get internal MailMessage object. For that object, you can then iterate through Attachments collection to get its contents. You no longer need to call GetAttachmentsFromTnef for nested objects. Single GetAttachmentsFromTnef call extracts contents from all winmail.dat files on all levels. See TnefParser topic for example (it, however, extracts data from a standalone winmail.dat file but it's all the same for a winmail.dat attachment to an e-mail).

Examples
This example downloads the last e-mail in IMAP inbox, extracts all attachments and RTF body from the TNEF (if any), saves the attachments to disk and displays the contents of the RTF body in the console.
using System;
using System.Collections;
using System.Text;
using MailBee;
using MailBee.ImapMail;
using MailBee.Mime;

class Sample
{
    static void Main(string[] args)
    {
        Imap imp = new Imap();

        // Connect to the server, login and select inbox.
        imp.Connect("imap.domain.com");
        imp.Login("user", "password");
        imp.SelectFolder("INBOX");

        // Download the last e-mail in the inbox.
        // We assume that e-mail includes winmail.dat attachment.
        MailMessage msg = imp.DownloadEntireMessage(imp.MessageCount, false);

        bool tnefFound = false;
        bool rtfFound = false;

        foreach (Attachment attach in msg.Attachments)
        {
            // Check if it's MS-TNEF (e.g. winmail.dat).
            if (attach.IsTnef)
            {
                tnefFound = true;

                // Extract all the attachments and RTF body.
                AttachmentCollection tnefItems = attach.GetAttachmentsFromTnef(
                    TnefExtractionOptions.ExtractAttachments | TnefExtractionOptions.ExtractRtfBody);

                // If RTF body exists, it's the last item in the collection with name "richbody.rtf".
                if (tnefItems.Count > 0 && tnefItems[tnefItems.Count - 1].Filename == "richbody.rtf")
                {
                    rtfFound = true;
                    Attachment rtfBody = tnefItems[tnefItems.Count - 1];

                    // Print the RTF contents to the console.
                    Console.WriteLine(Global.DefaultEncoding.GetString(rtfBody.GetData()));

                    // Remove the RTF from the collection. But if you wish to have the RTF body saved
                    // with all the attachments, comment this line out.
                    tnefItems.RemoveAt(tnefItems.Count - 1);
                }

                // Save everything in a folder. If you commented out the removal of RTF (see above),
                // RTF will be saved as well, with "richbody.rtf" filename.
                tnefItems.SaveAll(@"C:\Temp");
                Console.WriteLine();
                Console.WriteLine(tnefItems.Count.ToString() + " item(s) saved");
            }
        }

        if (!tnefFound)
        {
            Console.WriteLine("No TNEF found");
        }
        else if (!rtfFound)
        {
            Console.WriteLine("No RTF found in TNEF");
        }

        imp.Disconnect();
    }
}
See Also