是否有.NET Framework方法来解析电子邮件(MIME)? [英] Are there .NET Framework methods to parse an email (MIME)?

查看:190
本文介绍了是否有.NET Framework方法来解析电子邮件(MIME)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一类或一组构建在.NET Framework中(3.5+)函数可解析原始电子邮件(MIME文件)?

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

我不想找任何幻想或独立的库,它需要内置。我打算在一些单元测试可以使用本和只需抓住感兴趣的主标题(给,发件人,主题)与主体(在此情况下将总是文本,因此没有MIME树木或边界)。我已经写了几个MIME解析器在过去,如果没有什么现成的,我就放在一起经常EX pressions东西。这将是巨大的,能够做这样的事情:

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);

思考?

推荐答案

我知道你说没有外部库,但我有一个图书馆张贴在codePLEX:

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

HTTP://mailutilities.$c$cplex.com/

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

它已经过测试与超过40,000现实世界的邮件。

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 users 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";

这篇关于是否有.NET Framework方法来解析电子邮件(MIME)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆