6

我有一个读取 .msg 文件并提取正文和附件的 C# 应用程序。但是当我尝试加载 .eml 文件时,应用程序崩溃了。我正在加载这样的文件:

MailItem mailItem = (MailItem)outlookApp.CreateItemFromTemplate(msgFileName);
mailItem.SaveAs(fullFilename, OlSaveAsType.olHTML); // save body in html format
for(int i = 0; i < mailItem.Attachments.Count; i++)
    mailItem.Attachments[i].SaveAsFile(filename); // save attachments

这适用于 .msg 文件,但不适用于 .eml 文件。我不明白为什么 .eml 文件不起作用,因为我可以在 Outlook 2010 中打开 .eml 文件。

如何使用 Outlook主互操作程序集加载 .eml 文件?

4

4 回答 4

8

试试这个示例代码轻松从 .EML 文件中检索电子邮件信息

于 2011-05-20T16:26:51.773 回答
5

CreateItemFromTemplate仅适用于 MSG/OFT 文件。获取 EML 文件,您需要在代码中显式解析文件或使用第三方库(例如 Redemption):

以下代码将创建一个 MSG 文件并使用RedemptionRDOSession对象)将 EML 文件导入其中:

  set Session = CreateObject("Redemption.RDOSession")
  Session.MAPIOBJECT = outlookApp.Session.MAPIOBJECT
  set Msg = Session.CreateMessageFromMsgFile("C:\Temp\temp.msg")
  Msg.Import "C:\Temp\test.eml", 1024
  Msg.Save
  MsgBox Msg.Subject

然后,您可以使用消息 ( RDOMail ) 访问它的各种属性(主题、正文等)

于 2011-05-18T21:51:52.867 回答
0

为了从 .eml 文件创建 MailItem,您可以使用以下两个步骤:首先打开 Outlook 流程实例,然后使用 Outlook API 创建 MailItem。

  string file = @"C:\TestEML\EmlMail.eml";
  System.Diagnostics.Process.Start(file);
  Outlook.Application POfficeApp = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");  // note that it returns an exception if Outlook is not running
  Outlook.MailItem POfficeItem = (Outlook.MailItem)POfficeApp.ActiveInspector().CurrentItem; // now pOfficeItem is the COM object that represents your .eml file
于 2015-05-29T08:26:55.680 回答
0

尽管Outlook 可以打开 EML 文件,但无法仅使用 VBA以编程方式进行。所以我创建了这个 VBA 宏,它遍历某个文件夹并使用SHELL EXEC打开每个 EML 文件。Outlook 打开 EML 文件可能需要几毫秒,因此 VBA 会一直等待,直到在 ActiveInspector 中打开某些内容。最后,这封电子邮件被复制到某个选定的文件夹中,并且(在成功的情况下)原始 EML 文件被删除。

在此处查看我的完整答案(和代码): https ://stackoverflow.com/a/33761441/3606250

于 2015-11-17T16:02:23.397 回答