Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / C#

Easily Retrieve Email Information from .EML Files

Rate me:
Please Sign up or sign in to vote.
4.37/5 (16 votes)
30 Apr 2010CPOL1 min read 232.7K   10.7K   61   34
A wrapper C# class that extracts email fields from EML files

Introduction

This article is not the best way to do this. Please see EML_ReaderEx.aspx for a better way.

I know parsing .EML files is easy enough to do, but here it is in a wrapper class, all nice and tidy. This comes in real handy if you have a service that needs information that is emailed to the box. It has the usual "To", "CC" fields, along with a collection of x-receiver and many other fields. It will give you both plain text and HTML bodies, provided they are in the EML. Dates are converted to DateTime.

Using the Code

To use this class, just get the FileStream for the .EML file and put it in the constructor of the EMLReader class.

C#
FileStream fs = File.Open(sFile, FileMode.Open, 
                          FileAccess.ReadWrite);
EMLReader reader = new EMLReader(fs);
fs.Close();

Then, you just retrieve the email properties through the properties of the wrapper class.

C#
foreach(string xReceiver in reader.X_Receivers) 
{
   // do something with xReceiver
}

I covered all the email fields that I was concerned about, but there may be some that were left out. However, adding support for new fields in EMLReader is fairly straightforward.

Points of Interest

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. Probably, most of that time was due to the rather large learning curve on understanding Compound Documents and how MSG uses them. What a mess, and all that for such a legacy format. If Microsoft were to do it today, I'm sure it would all be in XML (attachments might make it a little lengthy though).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Lockheed Martin Services Inc
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: How to extract attchments Pin
BroxCJ10-Aug-09 7:36
BroxCJ10-Aug-09 7:36 
GeneralMessage when using the EML Class Pin
Member 23322539-Oct-08 5:32
Member 23322539-Oct-08 5:32 
GeneralRe: Message when using the EML Class Pin
BillJam119-Oct-08 5:59
BillJam119-Oct-08 5:59 
GeneralRe: Message when using the EML Class Pin
BillJam119-Oct-08 6:20
BillJam119-Oct-08 6:20 
GeneralRe: Message when using the EML Class Pin
yetibrain17-Mar-09 14:42
yetibrain17-Mar-09 14:42 
QuestionHow about .msg Pin
dbujak19-Sep-08 8:32
dbujak19-Sep-08 8:32 
AnswerRe: How about .msg Pin
philmee9522-Sep-08 8:07
philmee9522-Sep-08 8:07 
GeneralRe: How about .msg Pin
BillJam119-Oct-08 5:52
BillJam119-Oct-08 5:52 
That's fine for most applications but I was actually using the to read EML files that were not part of Exchange (actually, I thought the exported message files from Outlook were MSG files). They were SMTP messages sent to a server that I was processing.

I used to do a CDO thing with them also at one time because you can still do a MAPI login to get to them but I had problems with the dropped EML file not being completely finished writing while I was accessing it... I could have fixed it but I thought an EML reader was more straight forward that a dependecy on CDO.
AnswerRe: How about .msg Pin
BillJam119-Oct-08 6:04
BillJam119-Oct-08 6:04 
AnswerRe: How about .msg Pin
BillJam112-Jun-09 11:42
BillJam112-Jun-09 11:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.