关于在 C# 中解析 .eml 文件的建议
我有一个包含电子邮件对话的 .eml 文件目录。 C# 中是否有解析此类文件的推荐方法?
I have a directory of .eml files that contain email conversations. Is there a recommended approach in C# of parsing files of this type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
2017 年 8 月添加:查看 MimeKit:https://github.com/jstedfast/MimeKit 。 它支持.NET Standard,因此可以跨平台运行。
原始答案:我发布了一个示例项目来说明这个答案 Github
CDO COM DLL 是Windows/IIS 的一部分,可以在.net 中引用。 它将提供准确的解析和良好的对象模型。 将其与 ADODB.DLL 的引用结合使用。
Added August 2017: Check out MimeKit: https://github.com/jstedfast/MimeKit. It supports .NET Standard, so will run cross-platform.
Original answer: I posted a sample project to illustrate this answer to Github
The CDO COM DLL is part of Windows/IIS and can be referenced in .net. It will provide accurate parsing and a nice object model. Use it in conjuction with a reference to ADODB.DLL.
点击此链接以获得良好的解决方案:
摘要本文共 4 个步骤(文章中缺少下面的第二步,但需要):
添加对“Microsoft CDO for Windows 2000 Library”的引用,该引用可以在 Visual Studio 的“COM”选项卡上找到“添加参考”对话框。 这将在您的项目中添加对“ADODB”和“CDO”的 2 个引用。
禁用 2 个引用“ADODB”和“CDO”的互操作类型嵌入。 (参考 -> ADODB -> 属性 -> 将“嵌入互操作类型”设置为 False,并对 CDO 重复相同的操作)
在代码中添加以下方法:
通过传递 eml 文件的完整路径来调用此方法,它返回的 CDO.Message 对象将包含您需要的所有解析信息,包括 To、 From、Subject、Body。
Follow this link for a good solution:
The summary of the article is 4 steps(The second step below is missing in the article but needed):
Add a reference to "Microsoft CDO for Windows 2000 Library", which can be found on the ‘COM’ tab in the Visual Studio ‘Add reference’ dialog. This will add 2 references to "ADODB" and "CDO" in your project.
Disable embedding of Interop types for the 2 reference "ADODB" and "CDO". (References -> ADODB -> Properties -> Set 'Embed Interop Types' to False and repeat the same for CDO)
Add the following method in your code:
Call this method by passing the full path of your eml file and the CDO.Message object it returns will have all the parsed info you need including To,From, Subject, Body.
LumiSoft 包括 Mime 解析器。
Sasa 还包含一个 Mime 解析器。
LumiSoft includes a Mime parser.
Sasa includes a Mime parser as well.
您可能需要一个电子邮件/MIME 解析器。 解析所有标头字段并不是很困难,但是分离出各种 MIME 类型(例如图像、附件、各种文本和 html 部分等)可能会变得非常复杂。
我们使用第三方工具,但有很多 C# 工具/库。 在 Google 中搜索免费的 C# 电子邮件 MIME 解析器。 就像我得到这个:
http://www.codeproject。 com/Articles/11882/Advanced-MIME-Parser-Creator-Editor
http://www.lumisoft.ee/lswww/download/downloads/网络/info.txt
What you probably need is an email/MIME parser. Parsing all the header field is not very hard, but separating out various MIME types like images, attachments, various text and html parts etc. can get very complex.
We use a third party tool but there are many C# tools/libraries out there. Search for free C# email MIME parser in Google. Like I got this one:
http://www.codeproject.com/Articles/11882/Advanced-MIME-Parser-Creator-Editor
http://www.lumisoft.ee/lswww/download/downloads/Net/info.txt
获得一个像样的 MIME 解析器可能是一个不错的选择。 您可以尝试使用免费的 MIME 解析器(例如 codeproject 中的 这个 )但是代码作者的评论是这样的
让我对代码质量感到好奇。 我确信您可以破解一个 mime 解析器,它可以在几天/几小时内正确解析 95% 的电子邮件。 我还确信,解决剩余 5% 的问题需要几个月的时间。 考虑处理 S/MIME(加密和签名的电子邮件)、unicode、由行为不当的邮件客户端和服务器产生的格式错误的电子邮件、多种编码模式、国际化问题,确保故意格式错误的电子邮件不会使您的应用程序崩溃等...
如果向您发送电子邮件需要解析的是来自单一来源的快速& 脏解析器可能就足够了。 如果您需要解析来自野外的电子邮件,可能需要更好的解决方案。
我会推荐我们的 Rebex Secure Mail 组件,但我确信您会得到使用其他供应商的组件也能得到不错的结果。
确保您选择的解析器能够正确处理由 Mike Crispin(MIME 和 IMAP RFC 的合著者)准备的臭名昭著的“Mime Torture Sample message”。 测试消息显示在 MIME Explorer 示例中并且可以在安装包中下载。
以下代码展示了如何读取和解析 EML 文件:
Getting a decent MIME parser would be probably a way to go. You may try to use a free MIME parser (such as this one from codeproject) but comments from code author like this
made me curious about the code quality. I'm sure that you can hack a mime parser which parses 95% of email correctly in a few days/hours. I'm also sure that getting right the remaining 5% will take months. Consider handling S/MIME (encrypted and signed email), unicode, malformed emails produced by misbehaving mail clients and servers, several encoding schemas, internationalization issues, making sure that intentionally mallformed emails will not crash your app, etc...
If email you need to parse are comming from single source the quick & dirty parser may be enough. If you need to parse emails from the wild a better solution could be needed.
I would recommend our Rebex Secure Mail component, but I'm sure that you get decent result with components from other vendors as well.
Making sure that the parser of your choice is working correctly on the infamous "Mime Torture Sample message" prepared by Mike Crispin (co-author of MIME and IMAP RFCs). The testing message is displayed in MIME Explorer sample and can be downloaded in the installation package.
Following code shows how to read and parse EML file:
我刚刚开始使用 Papercut 的 Mime 部分。 乍一看似乎很不错而且很简单。
(
MailMessageEx
当然是从MailMessage
派生的。)I just started using the Mime-part of Papercut for this. It seems decent and simple at first sight.
(
MailMessageEx
is, of course, derived fromMailMessage
.)尝试:
编写程序:
请参阅:源代码。
解决方法:
但标头不会被解析,附件也不会被解析。
Try:
Write a program:
See: Source code.
Workarounds:
But headers won't be parsed, neither attachments.
Aspose.Email for .NET
http://www.aspose.com/docs/display/emailnet/Licensing
C# Outlook MSG 文件阅读器,无需 Outlook
Aspose.Email for .NET
http://www.aspose.com/docs/display/emailnet/Licensing
C# Outlook MSG file reader without the need for Outlook