Click here to Skip to main content
15,860,859 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.5K   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

 
QuestionBroken link in article Pin
Member 161538464-Dec-23 9:52
Member 161538464-Dec-23 9:52 
QuestionError on DateTime.Parse(saHdr[1]) Pin
Member 1499384315-Nov-20 19:26
Member 1499384315-Nov-20 19:26 
QuestionNice code Pin
_SlowArrow_13-Mar-16 12:52
_SlowArrow_13-Mar-16 12:52 
QuestionIssue in getting code Pin
Dipti Dhiman 37-Jun-15 21:24
Dipti Dhiman 37-Jun-15 21:24 
AnswerRe: Issue in getting code Pin
Member 1346134412-Oct-17 3:22
Member 1346134412-Oct-17 3:22 
GeneralRe: Issue in getting code Pin
Member 1278518031-Dec-23 6:14
Member 1278518031-Dec-23 6:14 
GeneralThanks Pin
Kayetyusha3-Aug-11 15:58
Kayetyusha3-Aug-11 15:58 
GeneralThanks Pin
Member 393560325-Jun-10 1:11
Member 393560325-Jun-10 1:11 
GeneralSend Eml File Pin
lascuola_marimo15-Oct-09 6:40
lascuola_marimo15-Oct-09 6:40 
Generaldrag and drop and then read .eml Pin
IrishWizKid18-Jul-09 7:34
IrishWizKid18-Jul-09 7:34 
GeneralRe: drag and drop and then read .eml Pin
BillJam1120-Jul-09 2:49
BillJam1120-Jul-09 2:49 
GeneralMy vote of 2 Pin
gfoidl12-Jun-09 0:01
gfoidl12-Jun-09 0:01 
GeneralRe: My vote of 2 Pin
BillJam1119-Apr-10 12:42
BillJam1119-Apr-10 12:42 
QuestionHow can you create an EML file Pin
treb06910-Mar-09 8:27
treb06910-Mar-09 8:27 
AnswerRe: How can you create an EML file Pin
BillJam1110-Mar-09 12:12
BillJam1110-Mar-09 12:12 
GeneralRe: How can you create an EML file Pin
Golden102429-Mar-09 22:46
Golden102429-Mar-09 22:46 
GeneralRe: How can you create an EML file Pin
Kalzon18-Sep-09 19:45
Kalzon18-Sep-09 19:45 
GeneralRe: How can you create an EML file Pin
Member 1340592124-Oct-17 0:17
Member 1340592124-Oct-17 0:17 
GeneralGreat! Pin
dextrous119-Feb-09 11:37
dextrous119-Feb-09 11:37 
QuestionHow to extract attchments Pin
DharmeshChauhan5-Nov-08 2:00
DharmeshChauhan5-Nov-08 2:00 
AnswerRe: How to extract attchments Pin
BillJam115-Nov-08 3:57
BillJam115-Nov-08 3:57 
AnswerRe: How to extract attchments Pin
yetibrain17-Mar-09 14:52
yetibrain17-Mar-09 14:52 
GeneralRe: How to extract attchments Pin
IrishWizKid15-Jul-09 20:43
IrishWizKid15-Jul-09 20:43 
GeneralRe: How to extract attchments Pin
BillJam1116-Jul-09 2:46
BillJam1116-Jul-09 2:46 
GeneralRe: How to extract attchments Pin
BroxCJ10-Aug-09 7:36
BroxCJ10-Aug-09 7:36 

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.