title: Exchange Web Services read e-mail body using powershell
body: <p>I'm trying to read e-mails sent to an account. 
  These e-mails are notifications that our team needs to take action. 
  Right now we go in, read the new e-mails and copy and paste the needed info, delete the e-mails and wait for new ones to come in. </p>
  
  <p>I've been using EWS to get some info, but I need help ...
  1) Retrieving the body as text instead of html
  2) Moving the found emails to the deleted folder. 
          clear-host</p>
  
  <pre><code>## Define UPN of the Account that has impersonation rights
  $AccountWithImpersonationRights = "admin@mycorp.com"
  
  ##Define the SMTP Address of the mailbox to impersonate
  $MailboxToImpersonate = "user@mycorp.com"
  
  ## Load Exchange web services DLL
  ## Download here if not present: http://go.microsoft.com/fwlink/?LinkId=255472
  $dllpath = "C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.1\Microsoft.Exchange.WebServices.dll"
  Import-Module $dllpath
  
  ## Set Exchange Version
  $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
  
  ## Create Exchange Service Object
  $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
  
  #Get valid Credentials using UPN for the ID that is used to impersonate mailbox
  $Service.UseDefaultCredentials = $true 
  
  ## Set the URL of the CAS (Client Access Server)
  $uri=[system.URI] "https://webmail.mycorp.com/ews/exchange.asmx" 
  $service.url = $uri
  
  #$service.AutodiscoverUrl($AccountWithImpersonationRights ,{$true})
  ##Login to Mailbox with Impersonation
  Write-Host 'Using ' $AccountWithImpersonationRights ' to Impersonate ' $MailboxToImpersonate
  $service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SMTPAddress,$MailboxToImpersonate );
  
  #Connect to the Inbox and display basic statistics
  $InboxFolder= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$ImpersonatedMailboxName)
  $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$InboxFolder)
  
  Write-Host 'Total Item count for Inbox:' $Inbox.TotalCount
  Write-Host 'Total Items Unread:' $Inbox.UnreadCount
  Write-Host ""
  
  Write-Host "Scanning remote mailbox: " -ForegroundColor Yellow -NoNewline ; Write-Host $MailboxToImpersonate
  Write-Host ""
  $mailitems = $null
  $mailitems = $inbox.FindItems(2000)
  
  $mailitems | ForEach {$_.Load()}
  </code></pre>
  
  <p>`</p>