Download attachment from user mailbox (Specific Folder) using EWS Managed API

Download Attachments from user Mailbox using EWS Managed API, you can define a specific folder to download the attachments from.

set the $itemsview to download more no of items from user mailbox default is set to 10
# Usage Example: # .\Download-Attachments-from-Email-Ews.ps1 -mailbox "test@xyz.com" -downloadDirectory "\\Lab-01\Downloads" ` # -folderName "Reports" -AdminuserName admin@xyz.com -password Mypass -ItemsView 100

As we are going to use the EWS make sure you met all the prerequisites before you plan to run this script.
  • Admin account should have application impersonation rights, you can follow this msdn post to setup the permissions. 
  • Install EWS managed API on system where you plan to run the script from, if API is not installed, download the same from here
Note: Please install the latest version of EWS API, version 2.2 is currently being used in my script.
Download From GitHub

#Download Attachment From User Mailbox Programmatically
#This script can be used to download attachments Programmatically from a user Mailbox

# Usage Example: 
# .\Download-Attachments-from-Email-Ews.ps1 -mailbox "test@xyz.com" -downloadDirectory "\\Lab-01\Downloads" `
# -folderName "Reports" -AdminuserName admin@xyz.com -password Mypass

param (
       $mailbox="sunil.chauhan@xyz.com",
       $downloadDirectory="\\Lab-host\Attachments",
       $folderName="Daily Report",
       $AdminuserName,
       $password,
       $itemsView=10
      )
$uri=[system.URI] "https://outlook.office365.com/ews/exchange.asmx"
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
Import-Module $dllpath

## Set Exchange Version
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $AdminuserName, $password
$service.url = $uri

$service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId `
([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SMTPAddress,$mailbox);

$Folderid=new-object Microsoft.Exchange.WebServices.Data.FolderId `
([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$Mailbox)

$MailboxRoot=[Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
$FolderList = new-object Microsoft.Exchange.WebServices.Data.FolderView(100)
$FolderList.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
$findFolderResults = $MailboxRoot.FindFolders($FolderList)
$allFolders=$findFolderResults | ? {$_.FolderClass -eq "IPF.Note"}
$FolderToSearchForAttachment=$allFolders | ? {$_.DisplayName -eq $folderName} 

$ItemsWithAttachments=new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo `
([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::HasAttachments, $true)

$ItemView=New-Object Microsoft.Exchange.WebServices.Data.ItemView($itemsView)
$ItemsWithAttachments = $FolderToSearchForAttachment.FindItems($ItemsWithAttachments,$ItemView)

Write-host "Downloading..."

foreach($MailItems in $ItemsWithAttachments.Items)
 {
  $MailItems.Load()
  foreach($attach in $MailItems.Attachments)
      {
       $Name=("AD-" +(($attach.lAstModifiedTime).ToShortDateString()) + "-" + $attach.Name.ToString())
       $Name=$Name.replace("/","-")
       write-host "Attachment saved to Path:"(($downloadDirectory + "\" + $name))
       $attach.Load()
       $File = new-object System.IO.FileStream(($downloadDirectory + "\" + $Name), [System.IO.FileMode]::Create)
       $File.Write($attach.Content, 0, $attach.Content.Length)
       $File.Close()   
      }
  }

Comments