I've seen several solutions that use PowerShell to automate the local Outlook client but I want this to run server-side: Reach out to the server with a given account, check for unread messages, save any attachments to a file share and mark the message read.
2
votes
2 Answers
1
votes
This requires the Exchange Managed Services API to be installed.
$ewsPath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
Add-Type -Path $ewsPath
$ews = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$cred = (Get-Credential).GetNetworkCredential()
$ews.Credentials = New-Object System.Net.NetworkCredential -ArgumentList $cred.UserName, $cred.Password, $cred.Domain
$ews.AutodiscoverUrl( "[email protected]", {$true} )
$results = $ews.FindItems(
"Inbox",
( New-Object Microsoft.Exchange.WebServices.Data.ItemView -ArgumentList 100 )
)
$MailItems = $results.Items | where hasattachments
foreach ($MailItem in $MailItems){
$MailItem.Load()
foreach($Attachment in $MailItem.Attachments){
$Attachment.Load()
$File = new-object System.IO.FileStream(("C:\Temp\” + $attachment.Name.ToString()), [System.IO.FileMode]::Create)
$File.Write($attachment.Content, 0, $attachment.Content.Length)
$File.Close()
}
}