POP3 email client with full mime support (. NET 2.0)

Source: Internet
Author: User
Tags html sample mailmessage smtpclient
ArticleDirectory
    • Structure of a simple email
    • Introduction of mime
    • Structure of an email header field
    • Content-Type
    • Content-Type multipart
    • Content-transfer-Encoding
    • Mapping mime to system. net. Mail. mailmessage
    • 11.10.2006 improvements constructor, handling contentdisposition = NULL
    • 8.10.2006 improvements attachment handling
    • 17.9.2006 original post
    • Download source files & Project-25 KB
Introduction

This is Part 2 of my articles about email processing ing with POP3 and mime processing. my first article POP3 email client (. NET 2.0) covered the reliable downloading of emails from POP3 servers, which left us with a pure ASCII representation of the email body. this was the easier part.

In this article I provide the code to split the raw ASCII email into body, attachment, alternate views, etc. this was much harder to do, because while the POP3 specification is simple and specified straight forward in one RFC, there are several mime related rfcs, which provide a multitude of possibilities how simple stuff like an email's actual text can be sent. the mime specification allows for great flexibility, but Microsoft, being Microsoft, of course supports only a subset (for example no recursion of mime parts within mime parts ). the provided Code supports both worlds completely and gives the programmer the flexibility to access information about the specified ed email as needed.

If you wonder why I wrote this article despite the fact that there are varous articles on codeproject for mime support, here are some of the following comings encountered:

    • Some code is not managed
    • Use of DLLs without. net source code
    • Too limited functionality
    • No integrationSystem. net. Mail. mailmessage
    • No Error Reporting
    • No XML documentation, etc.

My code is based on the following work:

    • POP3 email client (. NET 2.0) by Peter Huber
    • Quotedprintable class by Bill Gearhart
Backgroundstructure of a simple email

A simple email in pure ASCII might look like this:

Date: sat, 2 Sep 2006 17:25:15 + 0200 from: Sender@NoSpam.comTo: Receiver@NoSpam.comSubject: simple plain text mail just a plain text email.

The first 4 lines are called the header of the email and they are separated from the body by an empty line. the end of the email is marked with a line containing just one ". "(a period sign ). there will be used more header lines when you look at a real email, some RFC standard ones and others, like this one from Gmail:

X-Gmail-Received: f105c784e77f8b689759558db72ccd07f60387ba

Introduction of mime

in the beginning there were just plain ASCII emails as defined in RFC 2822. plain ASCII was soon not sufficient, though, and the Multipurpose Internet Mail Extensions specification mime was created to support nUS-ASCII texts, multi-part message bodies, Rich Text (HTML), images, sounds and attachments. the specification tried to offer great flexibility and to cater to all kind of possibilities. the result was numerous rfcs (2045,204 6, 2047,204 9, 2231,238 7, 4288,428 9 ,...). as it often happens in big groups, the whole thing became rather complicated and, even worse, left it to the implementer how precisely body text, etc. are implemented.

in order to help you with the extraction of information from MIME Based emails, I'm going to explain to you the basic mime principles. first let's have a look at a complete mime email. it might be a bit confusing, but it gives a good overview of the varous mime elements which I will explain one by one. this email has one email header, followed by the email body text and a . GIF picture. notice the " -- 0 - 494165446 -1157210079 =: 74253 "line, which separates the various parts of the email, called mime entities.

Date: sat, 2 Sep 2006 17:25:15 + 0200 from: Sender@NoSpam.comTo: Receiver@NoSpam.comSubject: simple Gmail mailmime-version: 1.0content-type: multipart/mixed; boundary = "0-494165446-1157210079 =: 74253 "content-transfer-encoding: 8bit -- 0-494165446-1157210079 =: 7%3content-type: text/plain; charset = iso-8859-1Content-Transfer-Encoding: 8bitcontent-disposition: inline this is the email body this email has a smallpic.gif attachment -- 0-494165446-1157210079 =: 7%3content-type: image/GIF; name = "smallpic. GIF "content-transfer-encoding: base64Content-Description: 437081412-smallpic.gifcontent-disposition: inline; filename =" smallpic. GIF "examples/nzwzfpncck/oetuxvudxxdi9/sbdpfs4t +/fwimlh068uphjyjmrx868ufpn0knln069uvwvaqeaow = -- 0-494165446-1157210079 =: 74253 --.

Structure of an email header field

An email header field as defined in RFC 2822 has the following structure:

Field-name ":" [field-body] CRLF <strong> example: </strong> mime-version: 1.0

"Mime-version" isField-name, "1.0" isField-body. The mime-version header field is mandatory for every mime email. All other MIME header fields start"Content -..."

Content-Type

The most powerful MIME header field isContent-TypeAnd is defined in RFC 2046. It can look like this:

Content-Type: text/plain; charset = ISO-8859-1Content-Type: text/plain; charset = US-asciicontent-type: text/plain; charset = utf-8Content-Type: text/html; Content-Type: text/html; charset = ISO-8859-1Content-Type: text/csscontent-type: image/GIF; name1_image004.gif Content-Type: image/JPEG; name = "image005.jpg" Content-Type: Message/delivery-statuscontent-type: Message/rfc822Content-Type: Audio/X-mpegcontent-type: Video/mpeg-2Content-Type: Application/mswordcontent-type: application/mspowerpointcontent-type: Application/zip Content-Type: multipart/mixed; boundary = "---- = _ part_3431_12384933.1139387792352" Content-Type: multipart/alternative; boundary = "---- = _ part_4088_29304219.1115464258628" Content-Type: multipart/related; boundary = "---- = _ blank" Content-Type: multipart/digest; boundary = "---- = Next message 15543233913938263541" Content-Type: multipart/report; report-type = delivery-status; boundary = "k04g6hj9025016.1136391237/carbon.singnet.com. SG "Content-Type: multipart/parallel

TheContent-TypeField is used to specify the nature of the data in the body of a mime entity, by indicating media type and subtype identifiers, and by providing auxiliary information that may be required for certain media types. some of the media types are:

    • Text
    • Image
    • Message
    • Audio
    • Application
    • Multipart

Each of the media type defines its own set of subtypes, which might be followed by a set of parameters, each specified in an attribute = value pair. For example:

Content-Type: text/plain; charset = ISO-8859-1; format = flowed

TheMedia typeIs"Text",SubtypeIs"Plain",AttributeIs"Charset"AndAttributeValueIs"Iso-8859-1". There cocould be more attribute = value pairs like"Format = flowed".

Content-Type multipart

the media type "multipart" provides the flexibility to split an email into several parts, like plain text, HTML text and attached files. there are versions of multipart (subtypes), but all have the same attribute "boundary ". its value is a string which is unique in the whole email and is used for marking the boundary delimiter lines of the various parts. let's look at the previous example again, this time only with Content-Type information lines:

 headerlinescontent-type: multipart/mixed; boundary = "0-494165446-1157210079 =: 74253" headerlines -- 0-494165446-1157210079 =: 7%3content-type: text/plain; charset = iso-8859-1Other mime part header lines the plain text email body -- 0-494165446-1157210079 =: 7%3content-type: image/GIF; name = "smallpic. GIF "Other mime part header lines the attachment coded in base64 -- 0-494165446-1157210079 =: 74253 --. 

the first 3 lines are part of the email header. the end of the header is marked by an empty line. all other lines are part of the email body, which ends with the line having only a ". "(period ). the boundary delimiter line breaks the body itself into the email text and the file attachment. this line always starts with " -- " followed by the boundary string. the last boundary delimiter line is followed by trailing " -- ".

Each mime entity has a entity-header and a entity-body separated by an empty line. since emails and mime entities use the same structure and the same kind of header lines, it is possible that whole emails can become a mime entity, which is useful for mail systems (Content-Type: Message). But of course having an email in another email leads to publish complications, and so it is no wonder that most mail program use a different solution for forwarding an email, they just merge it with the email text body. this has the advantage that even mail clients not supporting mime can handle forwarding. similarly, even the mime specification is recursive, Microsoft'sSystem. net. Mail. mailmessageIs not! More about this later.

Content-Type: multipart/mixed

Often the top most multipart subtype is"Mixed". It indicates that the email consists of several mime entities, without specifying anything more about the kind of entities ."Multipart/mixed"Is used as default, if the actual subtype is not recognized by the email client.

Content-Type: multipart/alternative

The subtype"Alternative"Is used, if the same email is sent in plain text and HTML. both have the same content, but in alternative coding. the email client is supposed to display to the user the last alternative part understood by the client. if an email consist of a plain text entity followed by an HTML Entity, the email client is supposed to display the HTML text, even if it also knows how to display plain text, because the html version came last. an email with plain text and HTML can look like this:

Some header linesmime-version: 1.0content-type: multipart/mixed; boundary = "---- _ = _ nextpart_00000001c6cea2.ef9becf8" ------ _ = _ NextPart_001_01C6CEA2.EF9BECF8Content-Type: multipart/alternative; boundary = "---- _ = _ nextpart_002_01c6cea2.ef9becf8" ------ _ = _ NextPart_002_01C6CEA2.EF9BECF8Content-Type: text/plain; charset = "iso-8859-1" HTML Sample email with bold text and attachment. ------ _ = _ nextpart_002_01c6c Ea2.ef9becf8content-type: text/html; charset = "iso-8859-1" & lt ;! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en" & gt; & lt; HTML & gt; & lt; Head & gt; & lt; style & gt; Div {font-size: 10pt; font-family: verdana, Arial, Helvetica, sans-serif} & lt;/Style & gt; & lt; /Head & gt; & lt; Body & gt; & lt; Div & gt; html Sample email with & lt; strong & gt; bold & lt;/strong & gt; text and attachment. & lt;/Div & gt; & lt;/Body & gt; & lt;/html & gt; ------ _ = _ nextpart_002_01c6cea2.ef9becf8 -- ------ _ = _ NextPart_001_01C6CEA2.EF9BECF8Content-Type: image/GIF; name = "smallpic. GIF "samples/nzwzfpncck/oetuxvudxxdi9/sbdpfs4t +/samples = ------ _ = _ nextpart_00000001c6cea2.ef9becf8 --.

The structure of this email is:

Multipart/mixed | multipart/alternative | text/plain; format = flowed; charset = ISO-8859-1 | text/html; charset = ISO-8859-1 | image/GIF; name = smallpic. gif

Notice that the picture is partMultipart/mixed, NotMultipart/alternative.

Content-Type: multipart/related

Multipart-relatedCan be used to send HTML text and graphics or other related material in the same email. It is beyond the scope of this article to explain the details of any other of the multipart mediatypes.

Content-transfer-Encoding

POP3 defines that the body of an email is 7bit us ASCII code. since the text displayed to the user can be any Unicode and file attachments are usually array of bytes, the email sender must encode this content to ASCII and we, the recipient of the email, need to decode it. if the value is "7bit", no encoding was used. "8bit", or "binary" has the same meaning, but is not supported by. net Framework. I treat "8bit" like "7bit", I. e. take the content as it is, whereas "binary" is illegal in POP3, because some character sequences like CRLF ". "CRLF have a special meaning in POP3, but might occur in random binary.

Content-transfer-encoding: quoted-printable

If a mime entity consists mostly of us ascii characters, it is enough to encode just some special characters and all bytes not covered by the us ascii characterset. "quoted-printable" does this by sending"="And the Hex Value of the byte as ASCII characters. A carriage return (HEX: 0d) Becomes :"= 0d". There are a number of rules dealing with special circumstances. I couldn't find a decoder for quoted-printable in. net, So I copied the quotedprintable class, by Bill Gearhart source code from ASP emporium.

Content-transfer-encoding: base64

Base64Uses a limited set of characters ("A"-"Z", "a"-"Z", "0"-"9", "+ ","/") to express a 6 bit value. any 3 bytes can be expressed with 4 encoding characters. as an example, let's take the first 4 ASCII characters"R0lg"Of the Graphic File in our example Email:

R 0 l g001001 110100 100101 000110 resulting 3 bytes: 00100111 01001001 01000110

Details can be found at RFC 1421, 4.3.2.4 Step 4: printable Encoding

Using the code

The best way to get an understanding of a library is to use it.MainFunction in the downloadable Code does just that. it connects to an POP3 server (don't forget to provide the proper server name, user name and password) and downloads at most 5 emails. the code will not delete the emails from the server, but the server might delete them anyway, depending on its settings. the structure of the 5 emails will be displayed on the console."Program. CS"Also contains the MethodSendtestmail ()To generate some sample emails.

Emails are already edPop3mimeclientDerived fromPop3mailclient, Which is described in POP3 email client (. NET 2.0), by Peter Huber and offers all the functionality to interact with the POP3 server.Pop3mimeclientAdds the MethodGetemailWhich fetches one pays email from the POP3 server and returns it decodedRxmailmessage.

Mapping mime to system. net. Mail. mailmessage

TheSystem. net. Mail. mailmessageClass is usedSystem. net. Mail. smtpclientFor sending emails with SMTP.MailmessageExcept des only the information needed to send an email. Signing ing an email creates some additional information. Therefore, a new classRxmailmessageIs inherited deriving fromMailmessageAnd adding properties likeDeliverydateOrDeliveredto.

TheSmtpclientConvertsMailmessageTo a mime conformant email,MailmessageProvides hardly access to any mime related functionality. When authentication ing an email, We wowould like to store the complete information.Pop3mimeclientStarting es the first mime entity by mime entity and stores them as a mime entity tree in the newEntitiesCollection PROPERTYRxmailmessage. If possible, the info is also copied to the properties inherited fromMailmessage. This gives the user the freedom to choose if the complete email in mime form is used for further processing or just the simpler, but possibly incompleteBody,AlternateviewsOrAttachmentsAs definedMailmessage. The methodDecodeentityCan be used as an example how to loop through all mime entities of an email.

History
  • 11.10.2006 improvements constructor, handling contentdisposition = NULL
    • Proper HandlingUsesslInPop3mimeclientConstructor
    • Prevent exception whenContentdispositionIsNull
  • 8.10.2006 improvements attachment handling
    • Detecting content-Disposition Header field and creating an attachment if it looks like :"C-disp: attachment"
    • Only mime entities fromMultipart/alternativeParents become alternative views
    • Added end markers for multiparts inRxmailmessage. mailstructure()
  • 17.9.2006 original post
    • I was not too sure how to map the various multipart entities to email body, etc. I analyzed probably thousands of emails I have Ed and populated the rxmailmessage properties as appropriate, but it is very likely that you might receive a different formatted email. please provide some feedback here, if you do so or find any bugs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.