Send email Exchange Web Services PowerShell

Send email Exchange Web Services PowerShell

Hello,

Since PowerShell version 1 there is a way to send mail with it. In v1, this was not strait forward, you needed to create a SMTP object from .net, and then use it to send an email based on an MailMessage object. In v2, you get the Send-MailMessage cmdlet. Those two methods use an SMTP server, there is a third one, with Exchange Web Services. There isn’t a real benefit for using those beside learn how to use EWS for doing greater things. Indeed, you can do a lot more with EWS than just send mail.

Send email with PowerShell 1

This was not as easy as in v2:

$MailMessage = New-Object System.Net.Mail.MailMessage('Sender@ItForDummies.net', 'Anakin.Skywalker@itfordummies.net')
$MailMessage.BodyEncoding = [System.Text.Encoding]::UTF8
$MailMessage.IsBodyHtml = $true
$MailMessage.Subject = 'Subject'
$Body = '<h1>This is a mail sent through Send-MailMessage</h1>'
$HtmlBody = [Net.Mail.AlternateView]::CreateAlternateViewFromString($Body, 'text/html')
$MailMessage.AlternateViews.Add($HtmlBody)         
$SMTPClient = New-Object System.Net.Mail.SmtpClient( 'Smtp.itfordummies.net' , 25 )
$SMTPClient.Send($MailMessage)

Send email with PowerShell 2

This is quite easy with this cmdlet:

Send-MailMessage -SmtpServer Smtp.itfordummies.net -To Anakin.Skywalker@itfordummies.net -From 'Sender <Sender@ItForDummies.net>' -Subject 'Subject' -Body '<h1>This is a mail sent through Send-MailMessage</h1>' -BodyAsHtml

Send email Exchange Web Services PowerShell

Now, let’s see with Exchange Web Services, this is more complicated than is v2, but we’re here to learn !

First, you need to install a piece of software on your host, the Exchange Web Services needs a dll to be loaded.

You can find it here:

https://www.microsoft.com/en-us/download/details.aspx?id=42951

Then, import it:

Import-Module -Name 'C:Program FilesMicrosoftExchangeWeb Services2.2Microsoft.Exchange.WebServices.dll'

Note: You may need to change the path to fits your needs.

Once imported, you have access to a very lot new objects related to Exchange. The one we need is:

$exchService = New-Object -TypeName Microsoft.Exchange.WebServices.Data.ExchangeService

And then, connect it to your Web Services:

$Credential = Get-Credential
$exchService.Credentials = New-Object -TypeName Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $Credential.UserName, $Credential.GetNetworkCredential().Password
$exchService.AutodiscoverUrl($Credential.UserName, {$true})

Note: You need to specify an account with a mailbox.

Note: You can force the URL if you already know it (Office 365 for example):

$exchService.Url = 'https://outlook.office365.com/EWS/Exchange.asmx'

Once connected, you can start creating an email message:

$eMail = New-Object -TypeName Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $exchService
$eMail.Subject = 'Test is a test'
$eMail.Body = '<h1>This message is being sent through EWS with PowerShell</h1>'
$eMail.ToRecipients.Add('Anakin.Skywalker@itfordummies.net') | Out-Null

And finally send it:

$eMail.SendAndSaveCopy()
Send email Exchange Web Services PowerShell - Mail Sent

Send email Exchange Web Services PowerShell – Mail Sent

The full script:

Import-Module -Name 'C:Program FilesMicrosoftExchangeWeb Services2.2Microsoft.Exchange.WebServices.dll'
$Credential = Get-Credential
$exchService = New-Object -TypeName Microsoft.Exchange.WebServices.Data.ExchangeService
$exchService.Credentials = New-Object -TypeName Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $Credential.UserName, $Credential.GetNetworkCredential().Password
$exchService.AutodiscoverUrl($Credential.UserName, {$true})

#eMail
$eMail = New-Object -TypeName Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $exchService
$eMail.Subject = 'Test is a test'
$eMail.Body = '<h1>This message is being sent through EWS with PowerShell</h1>'
$eMail.ToRecipients.Add('Anakin.Skywalker@itfordummies.net') | Out-Null
$eMail.SendAndSaveCopy()

As you can see, I used the SendAndSaveCopy method, this will save a copy of the send mail in the “Sent Item” of the mailbox you use to connect to the Exchange Web Services. You can choose a different folder with an overload:

$eMail.SendAndSaveCopy('Inbox')

Note: You can get the list of well known folders name with:

[System.Enum]::GetValues('Microsoft.Exchange.WebServices.Data.WellKnownFolderName')

Or, you can don’t save a copy at all:

$eMail.Send()

I like the fact that you can save a copy of mail being sent with this method, this can help for tracking and logging.

One thought on “Send email Exchange Web Services PowerShell

  1. Pingback: Meeting Request Exchange Web Services PowerShell - It for DummiesIt for Dummies

Leave a Reply