Developing a C# POP3 client: Part 06 - Parsing MIME Messages

This is number six in a series of posts on developing a POP3 client in C#. These are the previous posts on this topic:

Two possibilities

There are actualy two options when writing a MIME parser. The first one is to download the whole message and then parse the content with regular expressions, the second option is to parse the message line by line while downloading. I'm not sure if there are some major advantages of one of the two possibilities. I could imagine that regarding performance the line by line method would be better, but I decided to take the regex approach because I think, it's easier to handle faulty MIME messages.

Parsing the message

First I shortly outline how the message parsing will happen. A MIME message consists of different MIME entities (as mentioned in my last post) which can hold some other entities, which are separated by a "boundary". This boundary string is defined in the header section of each entity (if it's a multipart entity). So first we have to parse the boundary string, then split the message so we get each part separated. We repeat this step with each part that has a content type of "multipart...".

Parsing Headers

MIME message headers are always formatted in the following way:

HeaderName: Header Value

There are some headers which hold more than one value, for example the "Content-Type" header what gives then the following format:

Content-Type: multipart/mixed;
      boundary="----=_NextPart_000_001A_034CE23.234EB34

As mentioned in my previous post, there are some standard headers, but there could also be headers appended by mail clients or spam filters. This means that there are some headers where we only want to get the value because we just know the name of the header. But there are also headers, we have no clue about. So there are two methods:

// parsing a "known" header 
public string ParseHeader(string mimeMessage, string headerName)
{
	if (String.IsNotNullOrEmpty(mimeMessage) && String.IsNotNullOrEmpty(headerName))
	{
		Regex r = new Regex(headerName + @":\s+(?<HeaderValue>.*\n");
		Match m = r.Match(mimeMessage);
		if (m.Groups["HeaderValue"] != null)
		return m.Groups["HeaderValue"].Value.Trim();
	}
	return "";
}

// parsing all headers
// the regex matches single line and double line headers
public Dictionary<string, string> ParseHeaders(string mimeMessage)
{
	Dictionary<string, string> headers = new Dictionary<string, string>();
	if (String.IsNotNullOrEmpty(mimeMessage))
	{
		Regex r = new Regex(@"(?<HeaderName>[^\r\n:]+):\s+(?<HeaderValue>(.+[\r\n][\t\x20]+.+)|(.+))");
		MatchCollection m = r.Matches(rawMessage);
		foreach (Match match in m)
		{
			headers.Add(match.Groups["HeaderName"].Value.Trim(), match.Groups["HeaderValue"].Value.Trim());
		}
	}
	return headers;
} 

Parsing the MIME entities

To show you how basically works I'll give you a simplified "MimePart". In the code I provide for download in my next post, there are some other properties and methods, but for this example this works fine:

public class MimePart
{
	public string ContentType { get; set; }
	public string Content { get; set; }
	public Dictionary<string, string> Headers { get; set; }
	public List<MimePart> Parts { get; set; }
}

Let's assume we get the raw MIME message data from the POP3 client. After recieving we iterate recursively through the MIME entities:

string rawMimeData = Pop3.GetMessage(32);

MimePart message = GetMimePart(rawMimeData);

private MimePart GetMimePart(string rawData)
{
	MimePart part = new MimePart();
	part.Parts = new List<MimePart>();
	part.ContentType = ParseContentType(rawData);
	if (part.ContentType.StartsWith("multipart"))
	{
		// split into parts
		string boundary = GetBoundary(mimeData); 
		Regex r = new Regex(@"[\s]*--" + boundary + @"[\s.]*\n", RegexOptions.IgnoreCase | RegexOptions.Multiline); 
		string[] rawParts = r.Split(mimeData);
		part.Headers = parseHeaders(part[0]);
		for (i = 1; i < rawParts.Length; i++)
		{
			if (part[i].Trim() != "")
			part.Parts.Add(GetMimePart(part[i]));
		}
	}
	else
	{
		part.Headers = parseHeaders(rawData);
		part.Content = rawData.Substring(rawData.IndexOf("\r\n\r\n"));
	}
	return part;
} 

That's basically all we have to do. Sure, this MimePart is not very comfortable to use, there's some need to decode encoded parts like attachments or get the plain text or html version of the body. Also basic data like the sender's email address or the subject of the message are not easily accessable. But at least the MIME message has now a structure which we can work with.


Posted by: Dave
Posted on: 5/18/2008 at 8:12 PM
Tags: , Categories: Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (2) | Post RSSRSS comment feed
Administration:

Comments (2) -

bali France

Sunday, June 19, 2011 6:24 AM

bali

Hello! I just would like to give a huge thumbs up for the great info you have here on this post. I will be coming back to your blog for more soon.

Evangeline Pandanarol France

Saturday, July 16, 2011 10:00 PM

Evangeline Pandanarol

Outstanding stuff from you, guy. I�ve examine your items before and you also are just also great. I adore what you�ve received correct right here. You make it entertaining so you still handle to keep it wise.That is actually a fantastic blog site thanks for sharing

Pan danarol France

Thursday, July 28, 2011 5:32 PM

Pan danarol

{

chevalier du zodiaque episode 18 France

Monday, September 19, 2011 4:20 AM

chevalier du zodiaque episode 18

One care for this page. I savor everyone own blog related to St . Seiya.

coloring hello kitty sheets France

Tuesday, September 20, 2011 12:25 AM

coloring hello kitty sheets

Probably the the majority of satisfying kind of scrapbooking you possibly can make is a which you plan to offer like a gift. Scrapbooks make special gifts for nearly each and every celebration, as there handmade touches display that most people plan about the individual. When somebody gets your scrapbook as a gift, he or she may possibly be inspired to form a scrapbook as well, plus using this method you possibly can distributed the particular scrapbooking design annoy for you to friends.

Archibald United States

Sunday, September 25, 2011 5:45 AM

Archibald

Heya excellent article , Many thanks for giving these details

business culture of japan France

Sunday, September 25, 2011 1:28 PM

business culture of japan

Probably the many satisfying kind of japon you can create is a which you want to offer like a reward. japon projects help to make carefully selected presents for nearly each and every celebration, for the reason that right now there hand crafted details display which most people attention with regards to this student. Any time people obtains the japon while any present, this individual or perhaps the girl may well always be driven for you to produce a new japon design also, as well as in in this way it is possible to distribute the japon irritate to be able to family and friends.

burton backpacks on sale United States

Monday, October 03, 2011 11:44 AM

burton backpacks on sale

Wonderful write-up. I had been checking this web site and I’m impressed! Useful details specially the final aspect :) I attend to this kind of information lots. I was looking for this specific data for a very long time. Thanks and best of luck.

Tam Knuth France

Wednesday, October 05, 2011 12:15 PM

Tam Knuth

Venerate to paper scribe , few renowned particular news . “When you suspend to nightmare you suspend to experience.” by Malcolm Stevenson Forbes.

reserver riad France

Wednesday, October 05, 2011 8:19 PM

reserver riad

Good blog! I actually fancy how it is susceptible on my oculars further the information are well written. I’m wondering how I muscle be notified whenever a unfamiliar advise has been made. I receive subscribed to your RSS which need do the stunt! Receive a monumental time! “Reputation is what else tribe apprehend about you. Kudos is what you apprehend about yourself.” by Lois McMaster Bujold.

sejour Marrakech France

Monday, November 07, 2011 9:16 AM

sejour Marrakech

This is too diligent-grabbing, You are an overly pro blogger. I’ve joined your supply moreover restrain up for looking for also of your impressive notify. Too, I acquire mutual your maze scene in my societal structures!

riad Marrakech France

Wednesday, November 09, 2011 2:56 AM

riad Marrakech

Truly what I was searching for, realize it for posting . “There are several victories worse than a quell.” by George Eliot.

Guide Marrakech France

Sunday, November 13, 2011 8:47 AM

Guide Marrakech

I uniform this website its a overpower serenity ! Happy I detected this on google. “Observe your enemies, for they alpha judge away your neglects.” by Antisthenes.

immobilier maroc location France

Wednesday, November 16, 2011 2:58 AM

immobilier maroc location

I like what you guys are up plus. Such artful process besides reporting! Guard up the famous processs mans I’ve incorporated you mans to my blogroll. I expect it bequeath benefit the estimate of my location Sourire . “A enclosure is a install where what was foggy previous betides further undecided than always.” by Henry Waldorf Francis.

Marrakech tourisme France

Thursday, November 17, 2011 3:44 PM

Marrakech tourisme

Yay google is my monarch aided me to judge this huge website ! . “Don’t yardstick away busy among your ovations. It does hardly debar using your cranium.” by Freaky Rooney.

Drusilla Reichman France

Saturday, November 26, 2011 3:33 AM

Drusilla Reichman

<strong>7-11 franchise...</strong>\n\nThe other day there's this site...|

My Homepage United States

Tuesday, January 31, 2012 10:09 PM

My Homepage

This site   is my  breathing in,  real   wonderful  style  and perfect content material . 762080

3ds France

Sunday, March 11, 2012 9:57 AM

3ds

Wonderful write-up, I have book marked this web page so ideally I will discover much more on this topic in the foreseeable future!

code de reduction new look France

Saturday, April 21, 2012 3:54 PM

code de reduction new look

Sp�cialiste reconnu de la faire le point sur th�me code de reduction de themes flux texte du communiqu� issu des de leur montant.

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading