0

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.

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

## 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()}

`

Empyre
  • 21
  • 1
  • 5
  • What have you tried? Your existing code doesn't extract the body as anything (HTML or text). I've just been doing some work with EWS through powershell so I may be able to help. – Nick.Mc Mar 11 '16 at 04:46
  • I did a search, found this page: http://mikepfeiffer.net/2011/04/powershell-script-cmdlets-for-managing-e-mail-items-using-the-exchange-web-services-managed-api/ which has a bunch of handy libraries (not what you're after). WIth regards to deleting emails, you just need to identify the email you want and call the .Delete method on it. – Nick.Mc Mar 11 '16 at 04:59
  • I'm just looking at what is stored in the $mailitems variable and will search as needed for specific items. but would like to be able to have the body show as test so I can search using some regex – Empyre Mar 11 '16 at 05:12
  • Looks like there is an example here: http://stackoverflow.com/questions/11243911/ews-body-plain-text – Nick.Mc Mar 11 '16 at 07:17
  • @Nick.McDermaid - I've tried the PowerShell solution in the linked question, and it's still returning the HTML body (confirmed that the `PropertySet.RequestedBodyType` is `Text`)... if you come across any other thoughts or questions, I'd be interested... – Code Jockey Jun 08 '16 at 16:50
  • 1
    Well I guess text is just HTML with the tags removed right?. Here's some powershell to do that: http://stackoverflow.com/questions/29930191/powershell-remove-html-tags-in-string-content. It seems like a a workaround but I suspect if an email is sent as HTML, there no 'automatic' way to turn it into text. I don't know enough about it really. – Nick.Mc Jun 09 '16 at 22:44

0 Answers0