关于在 C# 中解析 .eml 文件的建议

发布于 2024-07-23 01:29:35 字数 52 浏览 2 评论 0原文

我有一个包含电子邮件对话的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

独留℉清风醉 2024-07-30 01:29:35

2017 年 8 月添加:查看 MimeKit:https://github.com/jstedfast/MimeKit 。 它支持.NET Standard,因此可以跨平台运行。

原始答案:我发布了一个示例项目来说明这个答案 Github

CDO COM DLL 是Windows/IIS 的一部分,可以在.net 中引用。 它将提供准确的解析和良好的对象模型。 将其与 ADODB.DLL 的引用结合使用。

public CDO.Message LoadEmlFromFile(String emlFileName)
{
    CDO.Message msg = new CDO.MessageClass();
    ADODB.Stream stream = new ADODB.StreamClass();

    stream.Open(Type.Missing, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, String.Empty, String.Empty);
    stream.LoadFromFile(emlFileName);
    stream.Flush();
    msg.DataSource.OpenObject(stream, "_Stream");
    msg.DataSource.Save();

    stream.Close();
    return msg;
}

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.

public CDO.Message LoadEmlFromFile(String emlFileName)
{
    CDO.Message msg = new CDO.MessageClass();
    ADODB.Stream stream = new ADODB.StreamClass();

    stream.Open(Type.Missing, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, String.Empty, String.Empty);
    stream.LoadFromFile(emlFileName);
    stream.Flush();
    msg.DataSource.OpenObject(stream, "_Stream");
    msg.DataSource.Save();

    stream.Close();
    return msg;
}
纸伞微斜 2024-07-30 01:29:35

点击此链接以获得良好的解决方案:

摘要本文共 4 个步骤(文章中缺少下面的第二步,但需要):

  1. 添加对“Microsoft CDO for Windows 2000 Library”的引用,该引用可以在 Visual Studio 的“COM”选项卡上找到“添加参考”对话框。 这将在您的项目中添加对“ADODB”和“CDO”的 2 个引用。

  2. 禁用 2 个引用“ADODB”和“CDO”的互操作类型嵌入。 (参考 -> ADODB -> 属性 -> 将“嵌入互操作类型”设置为 False,并对 CDO 重复相同的操作)

  3. 在代码中添加以下方法:

    protected CDO.Message ReadMessage(String emlFileName) 
      { 
          CDO.Message msg = new CDO.MessageClass(); 
          ADODB.Stream 流 = new ADODB.StreamClass(); 
          流. 打开(类型. 缺失,  
                         ADODB.ConnectModeEnum.adModeUnknown,  
                         ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified,                                                                          
                         字符串.空,  
                         字符串.空); 
          Stream.LoadFromFile(emlFileName); 
          流.Flush(); 
          msg.DataSource.OpenObject(stream, "_Stream"); 
          msg.DataSource.Save(); 
          返回消息; 
      } 
      
  4. 通过传递 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):

  1. 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.

  2. 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)

  3. Add the following method in your code:

    protected CDO.Message ReadMessage(String emlFileName)
    {
        CDO.Message msg = new CDO.MessageClass();
        ADODB.Stream stream = new ADODB.StreamClass();
        stream.Open(Type.Missing, 
                       ADODB.ConnectModeEnum.adModeUnknown, 
                       ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified,                                                                         
                       String.Empty, 
                       String.Empty);
        stream.LoadFromFile(emlFileName);
        stream.Flush();
        msg.DataSource.OpenObject(stream, "_Stream");
        msg.DataSource.Save();
        return msg;
    }
    
  4. 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.

定格我的天空 2024-07-30 01:29:35

LumiSoft 包括 Mime 解析器

Sasa 还包含一个 Mime 解析器。

LumiSoft includes a Mime parser.

Sasa includes a Mime parser as well.

三生一梦 2024-07-30 01:29:35

您可能需要一个电子邮件/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

无所谓啦 2024-07-30 01:29:35

获得一个像样的 MIME 解析器可能是一个不错的选择。 您可以尝试使用免费的 MIME 解析器(例如 codeproject 中的 这个 )但是代码作者的评论是这样的

我大约在同一时间完成了这个工作
我在包装类上工作的时间
对于 MSG 文件。 差异很大
困难。 EML 包装器所在的位置
课程可能需要一天的时间来阅读
规范并正确
,味精包装器
课程花了一周时间。

让我对代码质量感到好奇。 我确信您可以破解一个 mime 解析器,它可以在几天/几小时内正确解析 95% 的电子邮件。 我还确信,解决剩余 5% 的问题需要几个月的时间。 考虑处理 S/MIME(加密和签名的电子邮件)、unicode、由行为不当的邮件客户端和服务器产生的格式错误的电子邮件、多种编码模式、国际化问题,确保故意格式错误的电子邮件不会使您的应用程序崩溃等...

如果向您发送电子邮件需要解析的是来自单一来源的快速& 脏解析器可能就足够了。 如果您需要解析来自野外的电子邮件,可能需要更好的解决方案。

我会推荐我们的 Rebex Secure Mail 组件,但我确信您会得到使用其他供应商的组件也能得到不错的结果。

确保您选择的解析器能够正确处理由 Mike Crispin(MIME 和 IMAP RFC 的合著者)准备的臭名昭著的“Mime Torture Sample message”。 测试消息显示在 MIME Explorer 示例中并且可以在安装包中下载

以下代码展示了如何读取和解析 EML 文件:

using Rebex.Mail;

MailMessage message = new MailMessage();
message.Load("file.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

I worked on this at about the same
time that I worked on a wrapper class
for MSG files. Big difference in
difficulty. Where the EML wrapper
class maybe took a day to read the
spec and get right
, the MSG wrapper
class took a week.

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:

using Rebex.Mail;

MailMessage message = new MailMessage();
message.Load("file.eml");
请恋爱 2024-07-30 01:29:35

我刚刚开始使用 Papercut 的 Mime 部分。 乍一看似乎很不错而且很简单。

    public void ProcessRawContents(string raw)
    {
        // NB: empty lines may be relevant for interpretation and for content !!
        var lRawLines = raw.Split(new []{"\r\n"}, StringSplitOptions.None);
        var lMailReader = new MimeReader(lRawLines);
        var lMimeEntity = lMailReader.CreateMimeEntity();
        MailMessageEx Email = lMimeEntity.ToMailMessageEx();
        // ...
    }

MailMessageEx 当然是从 MailMessage 派生的。)

I just started using the Mime-part of Papercut for this. It seems decent and simple at first sight.

    public void ProcessRawContents(string raw)
    {
        // NB: empty lines may be relevant for interpretation and for content !!
        var lRawLines = raw.Split(new []{"\r\n"}, StringSplitOptions.None);
        var lMailReader = new MimeReader(lRawLines);
        var lMimeEntity = lMailReader.CreateMimeEntity();
        MailMessageEx Email = lMimeEntity.ToMailMessageEx();
        // ...
    }

(MailMessageEx is, of course, derived from MailMessage.)

无名指的心愿 2024-07-30 01:29:35

尝试:

  • febootimail
  • SmtpExpress
  • LinkWS Newsletter Turbo
  • emlBridge - 将 eml 文件导入 Outlook 和几乎任何其他电子邮件客户端
  • Newsletter 2.1 Turbo
  • ThunderStor (emlResender)
  • Ruby(使用 eml2mbox)。 请参阅 jimbob 方法
  • Evolution - 创建新消息,附加 eml 文件,

编写程序:

解决方法:

  • $ cat mail.eml | 邮件-s-c
    但标头不会被解析,附件也不会被解析。
  • 将它们放入您的 GMail(Firefox 会将它们另存为附件)

Try:

  • febootimail
  • SmtpExpress
  • LinkWS Newsletter Turbo
  • emlBridge - importing eml files into Outlook and virtually any other email client
  • Newsletter 2.1 Turbo
  • ThunderStor (emlResender)
  • Ruby (using eml2mbox). See jimbob method.
  • Evolution - create new message, attach the eml file,

Write a program:

Workarounds:

  • $ cat mail.eml | mail -s -c
    But headers won't be parsed, neither attachments.
  • drop them into your GMail (Firefox will save them as attachments)
夏见 2024-07-30 01:29:35

Aspose.Email for .NET

Aspose.Email for .NET 是一个用于使用的组件集合
来自 .NET 应用程序内的电子邮件。 它让工作变得轻松
具有多种电子邮件格式和消息存储文件
(PST/OST) 以及消息发送和接收功能。

Aspose.Email 可以轻松创建、阅读和操作许多电子邮件
消息格式,例如 MSG、EML、EMLX 和 MHT 文件 ,无需
安装 Microsoft Outlook
。 您不仅可以更改消息
内容,还可以操作(添加、提取和删除)附件
来自消息对象。 您可以通过添加或自定义消息标题
删除收件人、更改主题或其他属性。 它也是
通过提供访问权限,您可以完全控制电子邮件
其 Mapi 属性。

C# Outlook MSG 文件阅读器,无需 Outlook

MSGReader 是一个 C# .NET 4.0 库,用于读取 Outlook MSG 和 EML (Mime
1.0) 文件。 几乎支持 Outlook 中的所有常见对象。

Aspose.Email for .NET

Aspose.Email for .NET is a collection of components for working with
emails from within your .NET applications. It makes it easy to work
with a number of email message formats and message storage files
(PST/OST) along with message sending and receiving capabilities.

Aspose.Email makes it easy to create, read and manipulate a number of
message formats such as MSG, EML, EMLX and MHT files without the need
of installing Microsoft Outlook
. You can not only change the message
contents, but also manipulate (add, extract and remove) attachments
from a message object. You can customize message headers by adding or
removing recipients, changing the subject or other properties. It also
gives you complete control over an email message by providing access
to its Mapi properties.

C# Outlook MSG file reader without the need for Outlook

MSGReader is a C# .NET 4.0 library to read Outlook MSG and EML (Mime
1.0) files. Almost all common object in Outlook are supported.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文