AttachmentGetAttachmentsFromTnef Method (TnefExtractionOptions) |
Namespace: MailBee.Mime
Exception | Condition |
---|---|
MailBeeException | An error occurred and ThrowExceptions is true. |
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).
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(); } }