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

查看:52
本文介绍了是否有 .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 解析器,如果没有现成的东西,我会从正则表达式中组合一些东西.能够做这样的事情会很棒:

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:

https://bitbucket.org/otac0n/mailutilities

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.

<小时>附注:

在内部,我的库使用这些正则表达式作为解析器:

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

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

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