IMAP email attachments and message parts

How to I decode email attachments and message parts with the IMAP component?

Date Entered: 09/26/2007    Last Updated: 06/25/2014

The IMAP component has an AutoDecodeParts property, which if set to true, will instruct the component to automatically decode fetched messages, fire a MessagePart event for each part of the message, and populate the MessageParts collection with information about each part of the message. In order to help speed up the email fetching process, this property is false by default. A common scenario is to ask the server for information about a set of messages without downloading all of the messages yet: imap.MessageSet = "1"; imap.FetchMessageInfo(); foreach (MessagePart part in imap.MessageParts) { imap.FetchMessagePart(part.Id); } Alternatively, you could go through each Part in the Message and download the attachments to disk: imap.MessageSet = "1"; imap.FetchMessageInfo(); foreach (MessagePart part in imap.MessageParts) { //if the part is an attachment, prepare for download if (part.Filename != "") { imap.LocalFile = "C:\\PATH\\TO\\" + part.Filename; imap.AutoDecodeParts = true; } //otherwise, see the part only in memory else { imap.LocalFile = ""; imap.AutoDecodeParts = false; } imap.FetchMessagePart(part.Id); }

In previous versions of IPWorks, you need to pass the message headers and text to the MIME component and decode the message manually. The MIME component will break apart the message into its separate parts.


We appreciate your feedback.  If you have any questions, comments, or suggestions about this entry please contact our support team at kb@nsoftware.com.