I have a directory of .eml files that contain email conversations. Is there a recommended approach in C# of parsing files of this type?
Here's a CodeProject link that provides a simple wrapper: http://www.codeproject.com/KB/office/EML_Reader.aspx
What you probabaly need is a email/mime parser. Parsing all the header field is not very hard, but separating out various mime types like images, attachments, various text and html parts etc. can get very complex.
We use a third party tool but there are many c# tools/library out there. Search for free C# email mime parser in google. Like I got this one:
http://www.codeproject.com/KB/cs/mime_project.aspx
Getting a decent MIME parser would be probably a way to go. You may try to use a free MIME parser (such as this one from codeproject) but comments from code author like this
I worked on this at about the same time that I worked on a wrapper class for MSG files. Big difference in difficulty. Where the EML wrapper class maybe took a day to read the spec and get right, the MSG wrapper class took a week.
made me curious about the code quality. I'm sure that you can hack a mime parser which parses 95% of email correctly in a few days/hours. I'm also sure that getting right the remaining 5% will take months. Consider handling S/MIME (encrypted and signed email), unicode, malformed emails produced by misbehaving mail clients and servers, several encoding schemas, internationalization issues, making sure that intentionally mallformed emails will not crash your app, etc...
If email you need to parse are comming from single source the quick & dirty parser may be enough. If you need to parse emails from the wild a better solution could be needed.
I would recommend our Rebex Secure Mail component, but I'm sure that you get decent result with components from other vendors as well.
Making sure that the parser of your choice is working correctly on the infamous "Mime Torture Sample message" prepared by Mike Crispin (co-author of MIME and IMAP RFCs). The testing message is displayed in MIME Explorer sample and can be downloaded in the installation package.
Following code shows how to read and parse EML file:
using Rebex.Mail;
MailMessage message = new MailMessage();
message.Load("file.eml");
LumiSoft includes a Mime parser.
Sasa includes a Mime parser as well.
The CDO COM DLL is part of Windows/IIS and can be referenced in .net. It will provide accurate parsing and a nice object model. Use it in conjuction with a reference to ADODB.DLL.
CDO.Message msg = new CDO.MessageClass();
ADODB.Stream stream = new ADODB.StreamClass();
stream.Open(Type.Missing, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, String.Empty, String.Empty);
stream.LoadFromFile(emlFileName);
stream.Flush();
msg.DataSource.OpenObject(stream, "_Stream");
msg.DataSource.Save();
return msg;