tags:

views:

72

answers:

3

i have a file which contains email message. i was wondering if there is any built in library or class in .net that would allow me to directly extract contents of email something like this

Library lx= new Library("email_file");

Console.writeline(lx.From ());

Console.writeline(lx.To());

file attachment=lx.attachment();

I have written a program which would do these tasks by scanning file containing email message using regular expression and extracting individual field from email like : To, From, Attachment etc. but, the requirement is to extract these things using some built in library or class.

Any help would be greatly appreciated

A: 

It depends on how the mail is stored in your file. Where does it come from?

If it is an Outlook email, you should take a look at the Outlook interop library.

Gimly
the email comes in a folder from lotus notes client i guess
Ganesh Deo
A: 

I'm not sure if there is a builtin class that will do this, but we do this exact thing using aspnetemail.com's components.

We use their pop component to get the email from the server, their mime component to read it (and attachments), and their aspnetemail component to send email.

I hope I don't sound like an advertisement, but we've been real happy with their service.

Micky McQuade
+3  A: 

There is no direct support for reading email messages in .NET. You have to hack your own (after day or two you'll be probably able to handle 95% of normal eml emails, after several months of hard work you'll be able to handle remaining 5%). Complete email parsing means correct handling of international characters, attachments, several encoding schemes, attachments, email messages sent as attachment, encrypted and signed emails, to be able to handle common errors of popular email clients, etc...

There are two common email file formats:

EML Text-based format which can be found in both *nix and windows. Outlook Express and Thunderbird clients can read/write messages in eml format. If you are sending emails using .NET's SmtpClient using a PickupDirectory this format will be used. EML format is specified in RFC 2045, 2046, 2047, 2048, 2049, 2822.

You can find plenty of third party components for reading eml files. Following code uses our Rebex Secure Mail (but feel free to google your own).

using Rebex.Mail;
using Rebex.Mime.Headers;

// create an instance of MailMessage  
MailMessage message = new MailMessage();

// load the message from a local disk file  
message.Load("c:\\message.eml");

Console.Write(message.From);
Console.Write(message.To)
foreach (Attachment attachment in message.Attachments)
{
    // Save the file 
    Console.WriteLine ("Saving '{0}' ({1}).", 
     attachment.FileName, attachment.MediaType);
    attachment.Save (attachment.FileName);
}

You can download trial here.

MSG Binary format used by Outlook. Microsoft published it's specification recently. There is a codeproject article with c# library which should be able to read it. There are also some third party commercial components but I haven't tried them personally.

Martin Vobr
Looks authoritative to me.
BC