Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
125 views
Welcome To Ask or Share your Answers For Others

1 Answer

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>(?:[^
]+
)*)(?<header_term>
)(?<body>.*)z";
internal static string HeadersMatch =
    @"^(?<header_key>[-A-Za-z0-9]+)(?<seperator>:[ ]*)(?<header_value>([^
]|
[ ]+)*)(?<terminator>
)";
internal static string HeaderSeperator =
    "
";
internal static string KeyValueSeparator =
    @"A:[ ]*z";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...