【问题标题】:Converting EML's to MSG's将 EML 转换为 MSG
【发布时间】:2015-06-12 03:45:12
【问题描述】:

我们有一个网络应用程序,允许用户在表格中查看电子邮件并双击它们以在 Outlook 中打开它们。

为此,我们使用(简化的)代码:

 var email = Session.OpenSharedItem(filename) as MailItem;

这适用于 .msg 消息,但表中也列出了 .eml 文件。 OpenSharedItem 方法无法打开 .eml 文件 (https://msdn.microsoft.com/en-us/library/bb176433(v=office.12).aspx)

所以我们想将那些 .eml 文件转换为 .msg 文件。

到目前为止,我们只能在 Redemption 这样的付费第三方库中找到答案我们无法做到。有没有其他可用的解决方案?

编辑:更清楚地表明我们不能使用付费的第三方库。

【问题讨论】:

    标签: c# outlook msg eml


    【解决方案1】:

    如果你能够脱壳,outlook.exe 可以直接运行 emls 而无需像这样进行转换

    outlook.exe /eml "path\to\file.eml"
    

    即使电子邮件是可编辑的 (X-Unsent= 1),这也有效。
    甚至可能有一种方法可以做一个等效的without shell,这会很好。

    或者,您可以通过编程方式执行此操作,但需要转换;事实上,如果您希望任何可编辑的电子邮件自动插入用户的签名(因为那将是经常,而不是 msg/eml),您必须这样做。
    下面我有一个 powershell 脚本,它接受一个 eml 并有条件地保存一个 msg 或一个oft,然后打开它。

    你可以让它只转换而不打开,或者只打开而不转换(但你仍然需要制作临时文件:OOM 不接受从内存中制作MailItems);我只是为后代覆盖所有基础。
    我用它是客户端机器上的eml file extension is associated to open with the script,他们双击一个eml文件,它在outlook中打开。

    尽管它是 powershell,但 C# 还是可以互换的;我的意思是我写它时只阅读了 C# 的示例和文档(可能是旧 CDO 的一些 vbs)。
    我不懂 C#,我觉得把它放在这里对看到这篇文章的人来说比不写任何东西更有帮助(因为我不得不搜索互联网并从头开始)。
    如果有人移植它,我很乐意接受任何编辑

    $location = $PSScriptRoot
    Start-Transcript "$location\LOG.txt" -Append
    
    #ADODB only works if MIME is at the top
    . {
        'MIME-Version: 1.0'
        Get-Content $args[0] <#-AsByteStream#>
    } $args[0] | Set-Content "$location\tmp" <#-AsByteStream#>
    
    #parse the eml
    $eml = New-Object -ComObject ADODB.Stream
    $eml.Open()
    $eml.LoadFromFile("$location\tmp")
    $eml.Flush()
    $email = New-Object -ComObject "CDO.Message"
    $email.DataSource.OpenObject($eml, "_Stream")
    $email.DataSource.Save()
    $eml.Close()
    
    #!moved this to the bottom to demonstrate no-shellingout and msg conversion
    #if the email is not editable, just open it normally
    #if ($email.Fields('urn:schemas:mailheader:X-Unsent').Value -ne '1') {
    #   & "${env:ProgramFiles}\Microsoft Office\root\Office16\OUTLOOK.EXE" /eml $args[0]
    #   exit
    #}
    
    #build the template
    $outlook = New-Object -ComObject Outlook.Application
    $output = $outlook.CreateItem(<#olMailItem#>0)
    $output.Sender = $email.From
    $output.To = $email.To
    $output.CC = $email.CC
    $output.BCC = $email.BCC
    $output.Subject = $email.Subject
    if ($email.ReplyTo) {
        $output.ReplyRecipients.Add($email.ReplyTo) | Out-Null
    }
    $output.BodyFormat = <#olFormatHTML#>2
    $output.HTMLBody = $email.HTMLBody
    
    #for each of the attachments
    . {for ($part = $email.HTMLBodyPart; $part = $part.Parent) {
        $part.BodyParts | Where-Object {
            $_.Fields('urn:schemas:httpmail:content-disposition-type').Value -match '^(inline|attachment)$'
        }
    }} | %{
        #get the name
        $name = ($_.FileName -replace '^.*[/\\]','').trim('.')
        #make one if it didnt have one
        if (!$name) {
            $name = (
                'Untitiled attachment' +
                ($_.Fields('urn:schemas:httpmail:content-media-type').Value `
                    -replace '[/\\]'    ,'.' `
                    -replace '^\.+|\.+$','' `
                    -replace '^(.)'     ,' $1'
                )
            )
        }
        #save the attachment to file
        $_.
            GetDecodedContentStream().
            SaveToFile("$location\$name", <#adSaveCreateOverWrite#>2)
    
        #TODO unicode,bigendianunicode,utf8,utf7,utf32,ascii,default,oem
        if ($_.Charset -imatch 'UTF-8') {
            Get-Content "$location\$name" | Out-File 'tmp' -encoding utf8
            Move-Item 'tmp' "$location\$name" -Force
        }
        #pull it into the email
        $attachment = $output.Attachments.Add("$location\$name").PropertyAccessor
        Remove-Item "$location\$name"
    
        #set up its properties
        if ($_.Fields('urn:schemas:mailheader:content-id').Value) {
            $attachment.SetProperty(
                <#PR_ATTACH_CONTENT_ID(unicode)#>'http://schemas.microsoft.com/mapi/proptag/0x3712001F',
                $_.Fields('urn:schemas:mailheader:content-id').Value.trim('<>')
            )
        }
        if ($_.Fields('urn:schemas:httpmail:content-media-type').Value) {
            $attachment.SetProperty(
                <#PR_ATTACH_MIME_TAG(unicode)#>'http://schemas.microsoft.com/mapi/proptag/0x370E001F',
                $_.Fields('urn:schemas:httpmail:content-media-type').Value
            )
        }
    }
    
    #save and open the email
    if ($email.Fields('urn:schemas:mailheader:X-Unsent').Value -eq '1') {
        $output.SaveAs("email.oft", <#olTemplate#>2)
        $output.Close(<#olDiscard#>1)
        $outlook.CreateItemFromTemplate("email.oft").Display()
    }
    else {
        $output.SaveAs("email.msg", <#olMSG#>3)
        $output.Close(<#olDiscard#>1)
        $outlook.Session.OpenSharedItem("email.msg").Display()
    }
    #!as per the above #!; I would normally not have the x-unset test here
    #!and would only save the template, but calling it simply 'tmp', so no leftover files are made
    Remove-Item "$location\tmp"
    

    我想我已经涵盖了所有内容。这适用于附件、嵌入式图像和 css,但假定 HTML 电子邮件和 utf8 用于文本附件。
    这就是我所需要的,虽然添加对其他东西的支持并不难,但如果你是私人的,或者可以使用付费的 3rd 方,Dmitry 似乎在 Redemption 上做了一件非常全面的事情(我见过他的头像 A很多在我最近的旅行中),对你来说会简单得多。

    【讨论】:

      【解决方案2】:

      当然,您可以使用IConverterSession 对象(本机 Outlook MIME 转换器),但它只能在 C++ 或 Delphi 中访问。

      您还可以创建自己的转换器并一次创建一个 MIME 标头的 EML 文件。

      使用Redemption(可以在服务中运行,与 Outlook 对象模型不同),转换很简单

        set Session = CreateObject("Redemption.RDOSession")
        set Msg = Session.GetMessageFromMsgFile("c:\temp\test.msg")
        Msg.SaveAs "c:\temp\test.eml", 1031
      

      【讨论】:

      • Dmitry 反过来呢,例如给定一个 EML 文件,创建一个 MSG 文件?
      • 你可以使用 RDOSession.CreateMessageFromMsgFile / RDOMail.Import / RDOMail.Save
      猜你喜欢
      • 2018-06-23
      • 2023-03-14
      • 2018-11-22
      • 1970-01-01
      • 2016-11-13
      • 1970-01-01
      • 2015-10-27
      • 2011-09-18
      • 2013-02-14
      相关资源
      最近更新 更多