IMAP email attachments and message parts


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.RetrieveMessageInfo(); foreach (MessagePart part in imap.MessageParts) { imap.RetrieveMessagePart(part.Id); } Alternatively, you could go through each Part in the Message and download the attachments to disk: imap.MessageSet = "1"; imap.RetrieveMessageInfo(); 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.RetrieveMessagePart(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 article please contact our support team at support@nsoftware.com.