25

Is there a class or set of functions built into the .NET Framework (3.5+) to parse raw emails (MIME documents)?

I am not looking for anything fancy or a separate library, it needs to be built-in. I'm going to be using this in some unit tests and need only grab the main headers of interest (To, From, Subject) along with the body (which in this case will always be text and therefore no MIME trees or boundaries). I've written several MIME parsers in the past and if there isn't anything readily available, I'll just put together something from regular expressions. It would be great to be able to do something like:

MailMessage msg = MailMessage.Parse(text);

Thoughts?

Neil C. Obremski
  • 18,696
  • 24
  • 83
  • 112
  • Microsoft has CDO, explained here: http://stackoverflow.com/questions/936422/recommendations-on-parsing-eml-files-in-c-sharp (I know this is old, just posting this for people that find this later like I did) – eselk Mar 23 '13 at 03:48

6 Answers6

27

I know you said no external libraries, but I have a library posted on codeplex:

https://bitbucket.org/otac0n/mailutilities

MimeMessage msg = new MimeMessage(/* string, stream, or Byte[] */);

It has been tested with over 40,000 real-world mail messages.

I'm not too happy with my namespace choice, but... I'm too lazy to change it.


PS:

Internally, my library uses these regexes as a parser:

internal static string FullMessageMatch =
    @"\A(?<header>(?:[^\r\n]+\r\n)*)(?<header_term>\r\n)(?<body>.*)\z";
internal static string HeadersMatch =
    @"^(?<header_key>[-A-Za-z0-9]+)(?<seperator>:[ \t]*)(?<header_value>([^\r\n]|\r\n[ \t]+)*)(?<terminator>\r\n)";
internal static string HeaderSeperator =
    "\r\n";
internal static string KeyValueSeparator =
    @"\A:[ \t]*\z";
John Gietzen
  • 48,783
  • 32
  • 145
  • 190
  • John, instead of using your library perhaps he could use a class or two out of your source? – Paul Sasik Nov 03 '09 at 20:24
  • John, I am using your source, and it is very helpful! However, as make changes, it would be very helpful to have emails to check against. Do you have any emails in a suite that I could use to drive tests? Thanks - Erick – Erick T Feb 20 '11 at 02:52
  • 2
    Thanks John, I was looking for something similar and found your regex very useful. I wrote an extension method to the Outlook MailItem that parses the email header: http://www.lessanvaezi.com/email-headers-from-outlook-mailitem/ – Lessan Vaezi Jul 22 '11 at 05:51
  • Any example of how to get this back into a MailMessage for sending through SmtpClient? – jpierson May 03 '12 at 09:08
  • @NarekMamikonyan The spec requires 7-bit clean ascii. – John Gietzen Dec 08 '18 at 21:51
20

Very impressed with free, open-source (MIT-licensed) and fast MimeKit

Duncan Smart
  • 31,172
  • 10
  • 68
  • 70
4

No, there is no way to do that yet. Microsoft has not created a Text-to-Message convertor just as they haven't created a POP3 or IMAP library. Unfortunate.

James Bailey
  • 1,245
  • 9
  • 6
2

I recommend IMAP and MIME parser libs from Lumisoft. Which I used before and its easy to work with. You can download it from here: http://www.lumisoft.ee/lsWWW/Download/Downloads/Net/ The lib has many other protocols like ftp, pop3, etc and I'm sure the sc is available. Try to google for it, also you can find it on codeproject.com regards

thiagoleite
  • 503
  • 1
  • 3
  • 11
0

Check out Mail.dll .NET mail component, it has build in MIME support, unicode, and multi-national email support:

MailBuilder builder = new MailBuilder();

// Here you get the message class with attachments, visuals
IMail message = builder.CreateFromEml(File.ReadAllText("test.eml"));

// you can access entire MIME document:
MimeDocument document = message.Document;

Please note that Mail.dll is a commercial product that I've created.

You can download it here: https://www.limilabs.com/mail.

Pawel Lesnikowski
  • 6,264
  • 4
  • 38
  • 42
0

check out our Rebex Secure Mail which includes a (IMHO) decent S/MIME parser. Features include:

  • High level MailMessage API (message as seen in common email client)
  • Low level MimeMessage API (access to S/MIME internal tree)
  • Support for both MIME, S/MIME
  • Support for TNEF (winmail.dat) produced by Microsoft Outlook
  • Message encryption
  • Message signing
  • Unicode and internationalization support
  • Linked resources list (used for inline CSS and pictures in HTML mails)
  • IEnumerable<T> support (needed for LINQ)
  • supports all .NET and .NET compact frameworks released until today

The parser is part of Rebex Secure Mail and you can download it here.

Martin Vobr
  • 5,757
  • 2
  • 37
  • 43