我们有一个网络应用程序,允许用户查看表格中的电子邮件,然后双击它们以在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 我们无法做到。还有其他解决方案吗?
编辑:更明确我们不能使用付费第三方库。
答案 0 :(得分:0)
如果您能够掏出钱,outlook.exe可以只运行emls而无需进行转换
outlook.exe /eml "path\to\file.eml"
即使电子邮件是可编辑的(X-Unsent= 1
),它也可以使用。
甚至可以采用一种等效的without shell方法。
或者,您可以通过编程方式执行此操作,但需要进行转换;实际上,如果您要让任何可编辑的电子邮件自动插入用户签名(那将是经常的,而不是msg / eml),则必须这样做。
在下面,我有一个Powershell 脚本,该脚本需要一个eml,并有条件地保存一个msg或一个,然后将其打开。
您可以使其仅打开而不转换,或者仅打开而不转换(但是您仍然需要制作临时文件:OOM不接受从内存中制作MailItem
s);我只是介绍后代的所有基础。
我用的是客户端计算机上的eml file extension is associated to open with the script,他们双击一个eml文件,它会在Outlook中打开。
即使它是Powershell,C#也可以互换。我的意思是我只阅读了C#的示例和文档(也许是一些旧CDO的vb)写的。
我不懂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方面做了一件奇妙的整体工作(我见过他的头像A我最近的旅行很多),对您来说会简单得多。
答案 1 :(得分:-1)
当然,您可以使用IConverterSession对象(本机Outlook MIME转换器),但只能在C ++或Delphi中访问。
您也可以创建自己的转换器,并一次创建一个MIME标头的EML文件。
使用Redemption,转换就像
一样简单 set Session = CreateObject("Redemption.RDOSession")
set Msg = Session.GetMessageFromMsgFile("c:\temp\test.msg")
Msg.SaveAs "c:\temp\test.eml", 1031