How to export mail message to EML or MSG file with PowerShell and EWS

旧城冷巷雨未停 提交于 2019-12-11 04:11:50

问题


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).

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?

Here's my script:

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"**
     }
}

回答1:


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 grabs the subject, before we load the email (subject is then 'lost').

$mail.Load($mailProps) loads the email & mime content.

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.



来源:https://stackoverflow.com/questions/42653778/how-to-export-mail-message-to-eml-or-msg-file-with-powershell-and-ews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!