PowerShell EWS Save as for e-mails

2k Views Asked by At

How is it possible to save an e-mail on disk with the help of EWS in PowerShell? I've searched the internet and found some answers, but that's all for C# or VB.

The code I have now does everything I need, copy the e-mail to the correct folder in MS Outlook and so on, but I can't seem to figure out how to save the message in a folder ($ENV:Temp) on disk.

This can be in the format EML or MSG, that doesn't matter to me, but it needs to be saved with everything it has (body, attachments, From, To, ..).

I've tried $Mail | Out-File "$env:TEMP\test.eml", and it does indeed generate a 15 KB file but it appears to be empty when I open it with MS Outlook.

Thank you for your help.

1

There are 1 best solutions below

2
On

In the meantime, I found the solution:

$Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList $ExchangeVersion
$Service.Credentials = $Credentials.GetNetworkCredential()
$Service.AutodiscoverUrl($BNLMailbox)

Try {
    $PowerShellPathId = Find-MailFolderIDHC @FindMailParams -Path $BNLMailboxInbox
    $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Service,$PowerShellPathId)
}
Catch {
    # Exchange version not correct or path not found
    throw "Move-MailsHC $($Global:Error[0].Exception.Message)"
}

$Props = [Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties
$PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet($Props)
$PropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text
$PropertySet.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::MimeContent)

$Date = [Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived
$TimeSpan = (Get-Date).AddHours(-$HoursAgo)
$Filter = New-Object -TypeName Microsoft.Exchange.WebServices.Data.SearchFilter+IsGreaterThan -ArgumentList $Date,$TimeSpan

$View =  New-Object Microsoft.Exchange.WebServices.Data.ItemView(100)
$View.OrderBy.add([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived,
    [Microsoft.Exchange.WebServices.Data.SortDirection]::Ascending)

 foreach ($Mail in $Mails.Items) {
    $TmpFolder = Join-Path $env:TEMP 'Move-MailsHC'
    if (-not(Test-Path $TmpFolder)) {
        New-Item $TmpFolder -ItemType Directory | Out-Null
    }
    Write-Verbose "Save original e-mail in temp '$TmpFolder'"
    $TmpMail = Join-Path $TmpFolder 'Mail.eml'
    $IoFile = New-Object System.IO.FileStream($TmpMail, [System.IO.FileMode]::Create) 
    $IoFile.Write($Mail.MimeContent.Content, 0, $Mail.MimeContent.Content.Length)
    $IoFile.Close()

    Write-Verbose "Download e-mail attachments to temp '$TmpFolder'"
    foreach ($A in $Mail.Attachments){
        $A.Load()
        $fiFile = New-Object System.IO.FileStream((Join-Path $TmpFolder $A.Name.ToString()), [System.IO.FileMode]::Create)
        $fiFile.Write($A.Content, 0, $A.Content.Length)
        $fiFile.Close()
        Write-Verbose "Downloaded Attachment: $($A.Name.ToString())"
    }
}