【问题标题】:Reading an .eml (outlook email) file using c#使用 c# 读取 .eml(Outlook 电子邮件)文件
【发布时间】:2018-04-26 01:03:57
【问题描述】:

我正在编写一个通过删除所有恶意内容来清理文件的服务。我正在使用这样的 Interop Excel 和 Word api:

Excel

    var excelApp = new Microsoft.Office.Interop.Excel.Application();
    excelApp.Visible = false;
    excelApp.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;
    try
    {
        var workbook = excelApp.Workbooks.Open(fileToClean.InputFileName);

单词

    var wordApp = new Microsoft.Office.Interop.Word.Application
    {
        Visible = false,
        AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable
    };

    try
    {
        var wordDoc = wordApp.Documents.Open(fileToClean.InputFileName, false, true);

我正在尝试找到一种类似的方法来打开 .eml Outlook 文件。 我找不到任何使用 Outlook 互操作打开 .eml 文件的方法。

【问题讨论】:

    标签: c# outlook office-interop eml


    【解决方案1】:

    EML 文件格式不是 Outlook 原生的 - MSG 格式是(您可以使用 Namespace.GetSharedItem 打开一个 MSG 字段)。即使您在 Windows 资源管理器中双击它,Outlook 也会很乐意为您打开 EML 文件。

    要读取 EML 文件,您需要在代码中显式解析它,或者使用数百个 MIME 处理组件之一。如果使用Redemption 是一个选项,您可以创建一个临时的MSG 文件并将EML 文件导入其中。

    以下代码将创建一个 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) 访问它的各种属性(主题、正文等)

    另外请记住,不能从 Windows 服务(例如 IIS)使用任何 Office 应用程序(Excel、Word 或 Outlook)。

    【讨论】:

    • 谢谢德米特里,我会试试这个。为了通过不允许作为服务运行的办公应用程序,我创建了一个 exe,它在服务器上运行。它监视一个特定的文件夹,当一个文件被转储到该文件夹​​中时,它会拾取它,清理它并将其保存在另一个文件夹中,因此调用系统从输出文件夹中获取清理后的文件..
    • 但这意味着必须始终有一个交互式用户登录,对吧?
    【解决方案2】:

    感谢 Dmitry,我设法找到了解决方案。我最终使用了 Independentsoft.Msg nuget 包。代码在这里:

    Message message = new Message(fileToClean.InputFileName);
    var attachmentList = new List<Attachment>();
    
    // First check all attachments, and add the clean ones to the attachment list
    foreach (BodyPart bodyPart in message.BodyParts)
    {
        if ((bodyPart.ContentDisposition != null) &&
            (bodyPart.ContentDisposition.Type == ContentDispositionType.Attachment))
        {
            foreach (Attachment attachment in message.GetAttachments())
            {
                if (attachment.GetFileName() == bodyPart.ContentDisposition.Parameters[0].Value)
                {
                    if (IsClean(attachment, fileToClean))
                    {
                        attachmentList.Add(attachment);
                    }
                    break;
                }
            }
        }
    }
    
    // Remove all attachements
    message.BodyParts.RemoveAll(bp =>
                           (bp.ContentDisposition != null) &&
                           (bp.ContentDisposition.Type == 
    ContentDispositionType.Attachment));
    
    // Attach Cleaned attachments
    foreach (Attachment attachment in attachmentList)
    {
        message.BodyParts.Add(new BodyPart(attachment));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 2011-03-15
      • 1970-01-01
      • 2020-05-30
      • 2017-09-05
      相关资源
      最近更新 更多