Xmindz
Xmindz

Reputation: 1282

How to parse the raw email string in to a System.Net.Mail.MailMessage object?

I have written a program in C# which connects to a POP Server and retrieves raw email message strings from the server using POP3 command RETR. Since the email message being retrieved by the program is in plain text format with all the headers and message body with in the same, its too difficult to extract each header and mail body from the raw string.

Could anybody tell me a solution by which I can parse the entire raw text to a System.Net.Mail.MailMessage object?

Following is a sample email raw string:

+OK 1281 octets
Return-Path: <[email protected]>
Delivered-To: samplenet-sample:[email protected]
X-Envelope-To: [email protected]
Received: (qmail 53856 invoked from network); 22 Sep 2012 06:11:46 -0000
Received: from mailwash18.pair.com (66.39.2.18)
MIME-Version: 1.0
From: "Deepu"
 <[email protected]>
To: [email protected]
Date: 22 Sep 2012 11:41:39 +0530
Subject: TEST Subject
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
Message-Id: <[email protected]>


TEST Body

.

Upvotes: 4

Views: 7653

Answers (2)

Xmindz
Xmindz

Reputation: 1282

After a bit research I could find the OpenPop.Net open source library which has almost all the short hand methods to communicate with a POP3 server.

It is highly useful since it will wrap up the raw email message to a strongly typed Message object and the different parts of an email message including any of its attachments can also be accessed separately. It is really awesome!

Here is the link:

http://hpop.sourceforge.net/

Upvotes: 6

CLRBTH
CLRBTH

Reputation: 522

I am currently investigating the same thing. This article contains a method to parse an email string to a custom email object:

http://dotnetslackers.com/articles/aspnet/Creating-a-Dynamic-Email-Drop-Box-Part2.aspx

It needs a lot of work to be able to properly parse all emails. For example, the format of the sent date field can be different and this script only catches one format. But this project is a good start.

Upvotes: 1

Related Questions