Click here to Skip to main content
11,412,833 members (72,692 online)
Click here to Skip to main content

Easily Retrieve Email Information from .EML Files -- Revised

, 30 Apr 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
C# classes that upload an EML into a System.Net.Mail.MailMessage

Introduction

This article demonstrates how to upload an EML file into a Microsoft .NET MailMessage. This supercedes other work I have written on this. I had trouble with the other method not decoding the message's HTML body properly and was looking for an alternative when I found Peter Huber's article "POP3 Email Client (.NET 2.0)". I discovered that with a little modification, his RxMailMessage and Pop3MimeClient classes will also read EML files. I contacted him and told him that I would be changing some of his code and updating my article and he was obligated to that idea. Pop3MimeClient was also renamed to MimeReader since its purpose does not necessarily have anything to do with Pop3.

Background

My intent was to have an SMTP service I could send messages to that would process those messages. The recipient email name was an identifier but it was difficult to predict what it might be. The host name was the server. Sending to id@server.com would create an EML file in the SMTP services Drop area (for Microsoft SMTP, usually c:\inetpub\mailroot\drop). I could then pull off all the mail recipients (To, CC and BCC) and process the body message as needed. To use Pop3, I would have had to log in to each Pop3 account or use a Pop3 catch-all account. I was willing to go this route until I found out that Microsoft was going to remove the Pop3 Server from all Microsoft Server platforms after Windows Server 2003. It also seemed a little silly to use Pop3 if I could just find a way to read the EML files directly. Then I found Peter's article.

Using the Code

Let's keep this simple. I'm sure you all know how to get file paths from a directory so I won't bother showing that. It's just a simple matter of creating a MimeReader and using it to get the an RxMailMessage object by specifying the EML path.

MimeReader mime = new MimeReader();     // this class processes the .EML mime content

// this get's the MailMessage into Peter's RxMailMessage class
// which is derived from the MS MailMessage class
RxMailMessage mm = mime.GetEmail(sEmlPath);

I had to look through the MailMessage AlternateViews to find the message body in some cases:

private string GetPlainText(RxMailMessage mm)
{
    // check for plain text in body
    if (!mm.IsBodyHtml && !string.IsNullOrEmpty(mm.Body))
        return mm.Body;
        
    string sText = string.Empty;
    foreach (AlternateView av in mm.AlternateViews)
    {
        // check for plain text
        if (string.Compare(av.ContentType.MediaType, "text/plain", true) == 0)
            continue;// return StreamToString(av.ContentStream);
            
        // check for HTML text
        if (string.Compare(av.ContentType.MediaType, "text/html", true) == 0)
            sText = StreamToString(av.ContentStream);
    }
    
    // HTML is our only hope
    if (sText == string.Empty && mm.IsBodyHtml && !string.IsNullOrEmpty(mm.Body))
        sText = mm.Body;
        
    if (sText == string.Empty)
        return string.Empty;
        
    // need to convert the HTML to plaintext
    return PgUtil.StripHTML(sText);
}

private static string StreamToString(Stream stream)
{
    string sText = string.Empty;
    using (StreamReader sr = new StreamReader(stream))
    {
        sText = sr.ReadToEnd();
        stream.Seek(0, SeekOrigin.Begin);   // leave the stream the way we found it
        stream.Close();
    }
    
    return sText;
}

Points of Interest

Some parts of Peter's original Pop3MimeClient (a.k.a. MimeReader) just didn't seem to work for me, in particular, the setting of content-disposition header field in ProcessHeaderField and the ConvertToMailAddress method.

History

This supercedes the original Easily retrieve email information from .EML files article I had written.

License

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

Share

About the Author

BillJam11
Software Developer (Senior) Lockheed Martin Services Inc
United States United States
No Biography provided

Comments and Discussions

 
QuestionEMl to HTMl page Pin
RohitKabadi at 1-Sep-14 23:49
memberRohitKabadi1-Sep-14 23:49 
QuestionException while reading Outlook eml files -The Date is in invalid format Pin
jagnyadatta at 4-Dec-13 20:09
memberjagnyadatta4-Dec-13 20:09 
AnswerRe: Exception while reading Outlook eml files -The Date is in invalid format Pin
BillJam11 at 5-Dec-13 4:23
memberBillJam115-Dec-13 4:23 
GeneralRe: Exception while reading Outlook eml files -The Date is in invalid format Pin
jagnyadatta at 7-Dec-13 5:35
memberjagnyadatta7-Dec-13 5:35 
QuestionFake code Pin
glebbest at 21-Nov-13 3:48
memberglebbest21-Nov-13 3:48 
Questiongetting warning on build in VS2010; ReplyTo is obsoleted for this type Pin
kp52r11 at 7-Nov-12 10:16
memberkp52r117-Nov-12 10:16 
AnswerRe: getting warning on build in VS2010; ReplyTo is obsoleted for this type Pin
Member 8995227 at 10-May-13 7:15
memberMember 899522710-May-13 7:15 
QuestionI am new to .Net and C#: Does anyone have a solution built that i can just open in VS2010? Pin
kp52r11 at 7-Nov-12 8:45
memberkp52r117-Nov-12 8:45 
QuestionHow to parse and read an eml file? Pin
Member 8721713 at 17-Apr-12 4:47
memberMember 872171317-Apr-12 4:47 
AnswerFor save the the attachment pass the attachment to the Function Pin
arventh at 8-Mar-12 23:26
memberarventh8-Mar-12 23:26 
QuestionAttachment Pin
arventh at 8-Mar-12 20:20
memberarventh8-Mar-12 20:20 
Questionhow to save the Attachment in the eml on physical path Pin
udayakumard at 8-Mar-12 19:48
memberudayakumard8-Mar-12 19:48 
how to extract the file from the eml file and save on the physical path
QuestionRequest for the EML messages parser functionality reuse. Pin
Rodion Melnichenko at 18-Jan-12 1:46
memberRodion Melnichenko18-Jan-12 1:46 
AnswerRe: Request for the EML messages parser functionality reuse. Pin
Andreas Gieriet at 18-Jan-12 4:52
memberAndreas Gieriet18-Jan-12 4:52 
GeneralMessage Subject Pin
Member 7834911 at 12-Apr-11 7:30
memberMember 783491112-Apr-11 7:30 
GeneralPlain text message with attachment problem. Pin
tkendall at 10-Mar-11 3:34
membertkendall10-Mar-11 3:34 
GeneralRe: Plain text message with attachment problem. Pin
saurabh.pundir at 23-Nov-11 22:28
membersaurabh.pundir23-Nov-11 22:28 
GeneralRe: Plain text message with attachment problem. Pin
Member 8721713 at 30-Apr-12 23:08
memberMember 872171330-Apr-12 23:08 
GeneralRe: Plain text message with attachment problem. Pin
vp_cz at 28-Jan-13 3:44
membervp_cz28-Jan-13 3:44 
GeneralRe: Plain text message with attachment problem. Pin
Shah Qais at 18-Feb-15 9:25
memberShah Qais18-Feb-15 9:25 
Generalimages in eml file Pin
Noel Buenaventura at 20-Jan-11 20:38
memberNoel Buenaventura20-Jan-11 20:38 
GeneralRe: images in eml file Pin
Member 8721713 at 30-Apr-12 23:11
memberMember 872171330-Apr-12 23:11 
GeneralPgUtil.StripHTML Pin
Noel Buenaventura at 18-Jan-11 5:35
memberNoel Buenaventura18-Jan-11 5:35 
GeneralRe: PgUtil.StripHTML Pin
BillJam11 at 18-Jan-11 5:54
memberBillJam1118-Jan-11 5:54 
Generalinline images and .css Pin
tryingOut at 28-Sep-10 15:20
membertryingOut28-Sep-10 15:20 
GeneralMy vote of 5 Pin
dmdj at 27-Sep-10 8:07
memberdmdj27-Sep-10 8:07 
Generalattachment Pin
deoganesh_007 at 1-Jul-10 11:02
memberdeoganesh_0071-Jul-10 11:02 
GeneralRe: attachment Pin
BillJam11 at 2-Jul-10 6:43
memberBillJam112-Jul-10 6:43 
GeneralRe: attachment Pin
dmdj at 27-Sep-10 8:06
memberdmdj27-Sep-10 8:06 
GeneralRe: attachment Pin
jagnyadatta at 4-Dec-13 20:10
memberjagnyadatta4-Dec-13 20:10 
GeneralRe: attachment Pin
BillJam11 at 5-Dec-13 4:17
memberBillJam115-Dec-13 4:17 
GeneralRe: attachment Pin
udayakumard at 7-Mar-12 22:49
memberudayakumard7-Mar-12 22:49 
GeneralRe: attachment Pin
AlbertONL at 9-Feb-14 8:36
memberAlbertONL9-Feb-14 8:36 
GeneralAnother better tool Pin
gokul78 at 4-May-10 12:50
membergokul784-May-10 12:50 
GeneralRe: Another better tool Pin
BillJam11 at 5-May-10 4:26
memberBillJam115-May-10 4:26 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150427.1 | Last Updated 30 Apr 2010
Article Copyright 2010 by BillJam11
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid