Powershell EWS上传eml

时间:2013-05-14 22:10:33

标签: powershell exchange-server exchangewebservices

我有这个代码来上传EML文件进行交换。

#Upload Eml Sample  

$folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)     
$Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)  
#Crete Email Object  
$emUploadEmail = new-object Microsoft.Exchange.WebServices.Data.EmailMessage($service)  
#Read File  
[byte[]]$bdBinaryData1 =  get-content -encoding byte "C:\temp\exportedmail.eml"  
#Set Mime Content in Message  
$emUploadEmail.MimeContent = new-object Microsoft.Exchange.WebServices.Data.MimeContent("us-ascii", $bdBinaryData1);  
#Set Sent Message Flags which means message wont appear as a Draft  
$PR_Flags = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(3591, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);  
$emUploadEmail.SetExtendedProperty($PR_Flags,"1")  
$emUploadEmail.Save($Inbox.Id) 

当我尝试上传带有附件的EML文件时,它会使运行该脚本的计算机崩溃。这需要所有的记忆。如何修改上面的代码以上传eml和附件。

1 个答案:

答案 0 :(得分:0)

编辑解决方案: Glen Scales 在此提供解决方案:

http://gsexdev.blogspot.com/2012/02/ews-managed-api-and-powershell-how-to_22.html#!/2012/02/ews-managed-api-and-powershell-how-to_22.html

替换Get-Content:

[byte[]]$bdBinaryData1 = get-content -encoding byte "C:\temp\exportedmail.eml"

[byte[]]$bdBinaryData1 = [System.IO.File]::ReadAllBytes("C:\temp\exportedmail.eml")