Thursday, May 10, 2007

Receive and parse email in .NET

How to: Receive email through pop3 and parse mime message into From, To, Subject and Body parts

This task is not as trivial as it seemed to be.
Reasons:
1) .NET framework does not have classes which handle POP3
2) Email is sent in MIME format, which is quite tricky. In particular, email can have recursive attachments embedded into raw MIME email.

So far my research recommends two approaches:

Approach 1: Use an open source project.

It seems that the best open source MIME project is
LumiSoft .NET MIME parser.
You may browse all content here
Lumisoft Forum

Pop3 client retrieval & parsing sample
using(POP3_Client c = new POP3_Client()){
c.Connect("ivx",110);
c.Authenticate("test","test",true);

POP3_MessagesInfo mInf = c.GetMessagesInfo();

// Get first message if there is any
if(mInf.Count > 0){
byte[] messageData = c.GetMessage(mInf[0]);

// Parse message
Mime m = Mime.Parse(messageData);
string from = m.MainEntity.From;
string subject = m.MainEntity.Subject;
// ...
}
}



Approach 2: Buy a component

It seems that the best .NET Mime component available on the market so far is:
MIME4NET "True .Net Mime Parser".
The cost: $50

No comments:

Followers

About Me

My photo
Email me: blog@postjobfree.com