如何使用PowerShell和EWS将邮件导出到EML或MSG文件 [英] How to export mail message to EML or MSG file with PowerShell and EWS
问题描述
我目前正在使用PowerShell脚本,该脚本需要将特定邮箱中的所有邮件提取为.eml或.msg文件,并将其保存在备份服务器上.我正在将Powershell版本5与Exchange 2010管理控制台模块(EWS)结合使用.
I'm currently working on a PowerShell script that need to extract all mail messages from a specific mailbox as .eml or .msg files and save them on a backup server. I'm using powershell version 5 with the Exchange 2010 management console module (EWS).
当前,我的脚本能够使用其属性(例如,正文,主题,附件等)访问收件箱文件夹中的所有邮件.但是,我找不到导出消息(及其附件)的简单方法或方法.所以我的问题是,Exchange 2010中的EWS是否提供一种从邮箱中提取/保存邮件的方法?
Currently, my script is able to access all messages in the inbox folder with their properties such as Body, Subject, attachments and so on. However, I couldn't find an easy way or method to export the messages (with their attachment(s)). So my question is, does EWS in Exchange 2010 provide a method to extract/save a message from a mailbox?
这是我的剧本:
add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll"
[void][Reflection.Assembly]::LoadFile($dllpath)
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)
$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">"
$aceuser = [ADSI]$sidbind
$service.AutodiscoverUrl($aceuser.mail.ToString())
$MailboxName = get-mailbox -Identity myMailBox@myWorkPlace.com
$folderidcnt = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName.PrimarySmtpAddress.ToString())
$rootfolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, $folderidcnt)
$offset = 0;
$view = new-object Microsoft.Exchange.WebServices.Data.ItemView(10000, $offset)
$response = $service.LoadPropertiesForItems($results, [Microsoft.Exchange.WebServices.Data.PropertySet]::FirstClassProperties)
foreach ($mail in $results){
if ($mail.ToString() -eq "Microsoft.Exchange.WebServices.Data.EmailMessage"{
**"Function to export this message an an .eml or .msg file on a remote shared folder"**
}
}
推荐答案
代码的最后一部分需要是:
The last part of your code needs to be:
if ($mail.ToString() -eq "Microsoft.Exchange.WebServices.Data.EmailMessage") {
$mailSubject = $mail.Subject
$mailProps = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.ItemSchema]::MimeContent)
$mail.Load($mailProps)
#TODO: clean up $mailSubject so it's filesystem friendly
$fStream = New-Object System.IO.FileStream("C:\Temp\$mailSubject.eml", [System.IO.FileMode]::Create)
$fStream.Write($mail.MimeContent.Content, 0, $mail.MimeContent.Content.Length)
$fStream.Close()
}
$mailSubject = $mail.Subject
抓住了主题,然后才加载电子邮件(然后主题丢失").
$mailSubject = $mail.Subject
grabs the subject, before we load the email (subject is then 'lost').
$mail.Load($mailProps)
加载电子邮件&哑剧内容.
$mail.Load($mailProps)
loads the email & mime content.
最后3条$fStream
行将MIME内容写入流中.
The last 3 $fStream
lines write the mime content to a stream.
您需要添加一些规则来清理主题,或者用不同的方式命名电子邮件.
You'll need to add some rules to clean up the subject, or name the email differently, of course.
这篇关于如何使用PowerShell和EWS将邮件导出到EML或MSG文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!