C# 是否有 .NET Framework 方法来解析电子邮件 (MIME)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1669797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are there .NET Framework methods to parse an email (MIME)?
提问by Neil C. Obremski
Is there a class or set of functions built into the .NET Framework (3.5+) to parse raw emails (MIME documents)?
.NET Framework (3.5+) 中是否内置了一个类或一组函数来解析原始电子邮件(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:
我不是在寻找任何花哨的东西或单独的库,它需要内置。我将在一些单元测试中使用它,只需要获取感兴趣的主要标题(收件人、发件人、主题)和正文(在这种情况下将始终是文本,因此没有 MIME 树或边界)。我过去写过几个 MIME 解析器,如果没有现成的东西,我会从正则表达式中组合一些东西。能够执行以下操作会很棒:
MailMessage msg = MailMessage.Parse(text);
Thoughts?
想法?
采纳答案by John Gietzen
I know you said no external libraries, but I have a library posted on codeplex:
我知道你说没有外部库,但我在 codeplex 上发布了一个库:
https://bitbucket.org/otac0n/mailutilities
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.
它已经用超过 40,000 条真实世界的邮件消息进行了测试。
I'm not too happy with my namespace choice, but... I'm too lazy to change it.
我对我的命名空间选择不太满意,但是......我懒得改变它。
PS: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";
回答by James Bailey
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.
不,目前还没有办法做到这一点。Microsoft 没有创建文本到消息转换器,就像他们没有创建 POP3 或 IMAP 库一样。不幸的。
回答by Pawel Lesnikowski
Check out Mail.dll .NET mail component, it has build in MIMEsupport, unicode, and multi-national email support:
查看Mail.dll .NET 邮件组件,它内置了MIME支持、unicode 和多国电子邮件支持:
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.
请注意,Mail.dll 是我创建的商业产品。
You can download it here: https://www.limilabs.com/mail.
你可以在这里下载:https: //www.limilabs.com/mail。
回答by Martin Vobr
check out our Rebex Secure Mailwhich includes a (IMHO) decent S/MIME parser. Features include:
查看我们的Rebex Secure Mail,其中包括(恕我直言)体面的 S/MIME 解析器。功能包括:
- 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
- 高级 MailMessage API(在普通电子邮件客户端中看到的消息)
- 低级 MimeMessage API(访问 S/MIME 内部树)
- 支持 MIME、S/MIME
- 支持 Microsoft Outlook 生成的 TNEF (winmail.dat)
- 消息加密
- 消息签名
- Unicode 和国际化支持
- 链接资源列表(用于内联 CSS 和 HTML 邮件中的图片)
- IEnumerable<T> 支持(LINQ 需要)
- 支持直到今天发布的所有 .NET 和 .NET 紧凑型框架
The parser is part of Rebex Secure Mail and you can download it here.
解析器是 Rebex Secure Mail 的一部分,您可以在此处下载。
回答by thiagoleite
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
我推荐 Lumisoft 的 IMAP 和 MIME 解析器库。我以前用过,很容易使用。您可以从这里下载它:http: //www.lumisoft.ee/lsWWW/Download/Downloads/Net/该库有许多其他协议,如 ftp、pop3 等,我确定 sc 可用。尝试用谷歌搜索它,你也可以在 codeproject.com 上找到它
回答by Duncan Smart
Very impressed with free, open-source (MIT-licensed) and fast MimeKit
对免费、开源(MIT 许可)和快速的MimeKit印象深刻
- Nuget: https://www.nuget.org/packages/MimeKit
- "Lite" version without the crypto stuff: https://www.nuget.org/packages/MimeKitLite
- Nuget:https://www.nuget.org/packages/MimeKit
- 没有加密内容的“精简版”版本:https: //www.nuget.org/packages/MimeKitLite