C# 使用c读取.eml(outlook电子邮件)文件#

C# 使用c读取.eml(outlook电子邮件)文件#,c#,outlook,office-interop,eml,C#,Outlook,Office Interop,Eml,我正在编写一个通过删除所有恶意内容来清理文件的服务。我使用的Interop Excel和Word api如下: 胜过 话 我正试图找到一种类似的方法来打开.eml Outlook文件。 我找不到任何使用Outlook Interop打开.eml文件的方法 EML文件格式不是Outlook的本机格式-MSG格式是(您可以使用Namespace.GetSharedItem打开MSG文件)。即使在Windows资源管理器中双击EML文件,Outlook也会乐意为您打开该文件 要读取EML文件,您需要在

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

胜过

我正试图找到一种类似的方法来打开.eml Outlook文件。
我找不到任何使用Outlook Interop打开.eml文件的方法

EML文件格式不是Outlook的本机格式-MSG格式是(您可以使用
Namespace.GetSharedItem
打开MSG文件)。即使在Windows资源管理器中双击EML文件,Outlook也会乐意为您打开该文件

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

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

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


还要记住,Windows服务(如IIS)不能使用任何Office应用程序(Excel、Word或Outlook)。

多亏了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));
}
Message Message=新消息(fileToClean.InputFileName);
var attachmentList=新列表();
//首先检查所有附件,并将干净的附件添加到附件列表中
foreach(消息中的BodyPart BodyPart.BodyParts)
{
if((bodyPart.ContentDisposition!=null)&&
(bodyPart.ContentDisposition.Type==ContentDispositionType.Attachment))
{
foreach(message.GetAttachments()中的附件)
{
if(attachment.GetFileName()==bodyPart.ContentDisposition.Parameters[0].Value)
{
if(IsClean(附件,文件清理))
{
附件列表。添加(附件);
}
打破
}
}
}
}
//拆下所有附件
message.BodyParts.RemoveAll(bp=>
(bp.ContentDisposition!=null)&&
(bp.ContentDisposition.Type==
ContentDispositionType.Attachment));
//连接清洁的附件
foreach(附件列表中的附件)
{
message.BodyParts.Add(新的BodyPart(附件));
}

谢谢Dmitry,我将尝试一下。为了通过不允许作为服务运行的office应用程序,我创建了一个exe,它正在服务器上运行。它监视一个特定的文件夹,当一个文件被转储到该文件夹中时,它会将其拾取、清理并保存到另一个文件夹中,因此调用系统会从输出文件夹中获取清理后的文件。但这意味着必须始终有一个交互式用户登录,对吗?
    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);
  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
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));
}