How do I decode email that I retrieve with POP?


Messages more complex that plain text messages are MIME encoded; any attachments, HTML parts, inline images and formatting will be encoded into one entity. These messages will remain in MIME format until decoded by the mail client. You will need to use the MIME object to decode messages retrieved from a POP server into their respective parts. MIME encoding can take on many degrees of encapsulation, so you will want to use a recursive routine that is capable of calling itself when embedded MIME entities are detected. What follows is a general case routine for MIME decoding; you will initially wish to call this on POP's MessageHeaders and MessageText values: Private Sub MimeDecode(ByVal EntityHeaders As String, ByVal Entity As String) Dim Mime1 As New nsoftware.IPWorks.Mime Dim i As Integer Mime1.MessageHeaders = EntityHeaders Mime1.Message = Entity Try Mime1.DecodeFromString() Catch 'If an error is encountered, the entity is not a valid MIME entity Exit Sub End Try For i = 0 To Mime1.PartCount - 1 'If the Content-Type is "Multipart/????", then that part is an encoded entity 'You will want to call this routine again If LCase(Mime1.PartContentType(i)).StartsWith("multipart") Then MimeDecode(Mime1.PartHeaders(i), Mime1.PartDecodedString(i)) End If 'If a part is text/plain and is not an attachment, it is generally 'the plain text body of the message If Mime1.PartFilename(i) = "" And LCase(Mime1.PartContentType(i)) = "text/plain" Then 'Display your text here; you'll want to read from Mime1.PartDecodedString(i) End If 'If you're going to support HTML messages, you'll want to look for this 'part, and if found, use it in place of the plain text If LCase(Mime1.PartContentType(i)) = "text/html" Then 'Again, read from Mime1.PartDecodedString(i) to access End If 'External attachments show up with a content-disposition of "attachment" If LCase(Mime1.PartContentDisposition(i)) = "attachment" Then 'Mime1.PartFilename(i) will contain the name of the file. 'To actually get the file itself, you must read the value of 'Mime1.PartDecodedFile(i). Doing so will return the name of a 'temporary file that contains the file data; this file will not 'be created until PartDecodedFile is queried. Once you have the name 'of a temporary file with the data, simply rename the file to the 'location and name of where you wish to save the file End If 'Inline data that are sent in the email will generally have a 'content-ID header and a content-disposition of "inline" If LCase(Mime1.PartContentID(i)) <> "" Then 'Treat these cases as you would external attachments. The HTML that 'is returned will include CID tags that should correspond with this 'data, or you can simply save the images as you would an attachment 'if you do not wish to support HTML End If Next End Sub

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