1
votes

I have a script already that can download email attachments using EWS to avoid having outlook installed/opened. I'm now wanting to save email content, preferably as a HTML file, but otherwise as .eml or .msg.

My other thread about the attachments is here: Download attachments with multiple subjects from Exchange

I was hoping that the existing script could be adapted somehow, but honestly I have no idea.

$MailboxName = "[email protected]"
$Subjects = @(
          'Subject1',
          'Subject2'
        )

[regex]$SubjectRegex = ‘^(?i)(‘ + (($Subjects |foreach {[regex]::escape($_)}) –join “|”) + ‘)$’

$downloadDirectory = "C:\temp"


Function FindTargetFolder($FolderPath){
$tfTargetidRoot = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)
$tfTargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$tfTargetidRoot)

for ($lint = 1; $lint -lt $pfArray.Length; $lint++) {
    $pfArray[$lint]
    $fvFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1)
    $SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+isEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$pfArray[$lint])
            $findFolderResults = $service.FindFolders($tfTargetFolder.Id,$SfSearchFilter,$fvFolderView)
    if ($findFolderResults.TotalCount -gt 0){
        foreach($folder in $findFolderResults.Folders){
            $tfTargetFolder = $folder               
        }
    }
    else{
        "Error Folder Not Found"
        $tfTargetFolder = $null
        break
    }   
}
$Global:findFolder = $tfTargetFolder
}

$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
[void][Reflection.Assembly]::LoadFile($dllpath)
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)


$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">"
$aceuser = [ADSI]$sidbind

$uri=[system.URI] "https://webmail.com/EWS/Exchange.asmx"
$service.Url = $uri

FindTargetFolder($ProcessedFolderPath)

$folderid = new-object  Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
$InboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
$Sfsub = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject,$Subject[0])
$sfCollection = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::And);
$sfCollection.add($Sfsub)
$sfCollection.add($Sfha)
$view = new-object Microsoft.Exchange.WebServices.Data.ItemView(2000)
$frFolderResult = $InboxFolder.FindItems($sfCollection,$view)

foreach ($miMailItems in $frFolderResult.Items)
 {
$miMailItems.body.text | set-content C:\temp
 }
1
Are you wanting to save the entire email (including all in the message headers), or just the body? - mjolinor
Just the body of the email will do. Preserving formatting doesn't matter, other than line breaks. - Slingy

1 Answers

1
votes

Using the foreach loop from your other script:

foreach ($miMailItems in $frFolderResult.Items)
 {
  $miMailItems.body.text | set-content <filepath>
 }

You'll need to come up with a file naming scheme for the output files, but getting the email body text out to a file is relatively trivial.