将EML文件转换为MSG文件

3
我们有一个Web应用程序,允许用户在表格中查看电子邮件,并双击电子邮件以在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)找到答案,但我们不能使用该解决方案。是否有其他解决方案可用?

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

2个回答

0

如果您能够使用shell,outlook.exe可以直接运行eml文件而无需进行转换,方法如下:

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

即使邮件是可编辑的(X-Unsent= 1),这个方法也能奏效。
甚至可能有一种不使用 shell 的等效方式,这将非常好。

或者你可以通过编程实现这个功能,但需要进行转换;实际上,如果你想要任何可编辑的电子邮件自动插入用户签名(因为那将是一个oft而不是msg/eml),你必须这样做。
下面我有一个powershell脚本,它接受一个eml文件,有条件地保存一个msg或oft文件,并打开它

你可以只进行转换而不打开,或者只打开而不进行转换(但你仍然需要创建临时文件:OOM不接受从内存中创建MailItem);我只是涵盖了所有情况以备后人所需。
我用它来关联电子邮件文件(.eml)扩展名使其在客户端机器上用该脚本打开,他们双击一个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 文本。
这就是我所需要的,虽然添加其他支持并不难,但如果您是私人用户或可以使用付费第三方服务,Dmitry 似乎已经通过 Redemption 做了一件非常全面的事情(我最近的旅行中经常看到他的头像),对您来说会更简单。


0

您可以使用IConverterSession对象(原生的Outlook MIME转换器),但只能在C++或Delphi中访问。另外请注意,从Outlook 2016开始,该对象的实例只能在outlook.exe地址空间内运行时创建(例如从COM插件中)。

您还可以创建自己的转换器,并使用第三方库(我过去使用过Lumisoft)逐个MIME头创建EML文件。

如果使用Redemption(我是其作者)是一个选项,它可以在服务中运行(不像Outlook对象模型),并且转换非常简单:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接