Advertisement
mariussm

Getting mail items with attachments from Exchange WS

Jan 29th, 2015
3,255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Destination folder
  2. $destinationFolder = "C:\Users\marius\Downloads\Attachment Downloader"
  3.  
  4. # replace with your email address
  5. $email    = "username@mytenant.onmicrosoft.com"
  6. $username = "username@mytenant.onmicrosoft.com"
  7. $password = "Password123!"
  8.  
  9. # File extensions to download
  10. $extensions = "pdf","pdfa","doc","docx","dot","dotx","xls","xlsx","ppt","pptx"
  11.  
  12. # load the assembly
  13. Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
  14.  
  15. # Create Exchange Service object
  16. $s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1)
  17. $s.Credentials = New-Object Net.NetworkCredential($username, $password)
  18. # $s.TraceEnabled = $true
  19. Write-Host "Trying AutoDiscover... "
  20. $s.AutodiscoverUrl($email, {$true})
  21.  
  22. if(!$s.Url) {
  23.     Write-Error "AutoDiscover failed"
  24.     return;
  25. } else {
  26.     Write-Host -ForegroundColor Green "AutoDiscover succeeded - $($s.Url)"
  27. }
  28.  
  29. # Create destination folder
  30. $destinationFolder = "{0}\{1}" -f $destinationFolder, (Get-Date -Format "yyyyMMdd HHmmss")
  31. mkdir $destinationFolder | Out-Null
  32.  
  33. # get a handle to the inbox
  34. $inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
  35.  
  36. #create a property set (to let us access the body & other details not available from the FindItems call)
  37. $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
  38. $psPropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;
  39.  
  40. # Find the items
  41. $inc = 0;
  42. $maxRepeat = 50;
  43. do {
  44.     $maxRepeat -= 1;
  45.  
  46.     Write-Host "Searching for items in mailbox... " -NoNewline
  47.     $items = $inbox.FindItems(100)
  48.     Write-Host -ForegroundColor Green "found $($items.items.Count)"
  49.  
  50.     foreach ($item in $items.Items)
  51.     {
  52.         # Create mail folder
  53.         $inc += 1
  54.         $mailFolder = "{0}\{1}" -f $destinationFolder, $inc;
  55.         mkdir $mailFolder | Out-Null
  56.  
  57.         # load the property set to allow us to get to the body
  58.         try {
  59.             $item.load($psPropertySet)
  60.             Write-Host ("$inc - $($item.Subject)") -ForegroundColor Yellow
  61.  
  62.             # save the metadata to a file
  63.             $item | Export-Clixml ("{0}\metadata.xml" -f $mailFolder)
  64.  
  65.             # save all attachments
  66.             foreach($attachment in $item.Attachments) {            
  67.                 if(($attachment.Name -split "\." | select -last 1) -in $extensions) {
  68.                     Write-Host " - $($attachment.Name) - $([Math]::Round($attachment.Size / 1024))KB"
  69.                     $fileName = ("{0}\{1}" -f $mailFolder, $attachment.Name) -replace "/",""
  70.                     $attachment.Load($fileName)
  71.                 }
  72.             }
  73.  
  74.             # delete the mail item
  75.             $item.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete, $true)
  76.         } catch [Exception] {
  77.             Write-Error "Unable to load item: $($_)"
  78.         }
  79.     }
  80. } while($items.MoreAvailable -and $maxRepeat -ge 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement