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('[email protected]', '[email protected]')
$MailMessage.BodyEncoding = [System.Text.Encoding]::UTF8
$MailMessage.IsBodyHtml = $true
$MailMessage.Subject = 'Subject'
$Body = '

This is a mail sent through Send-MailMessage

' $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 [email protected] -From 'Sender ' -Subject 'Subject' -Body '

This is a mail sent through Send-MailMessage

' -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 = '

This message is being sent through EWS with PowerShell

' $eMail.ToRecipients.Add('[email protected]') | Out-Null

And finally send it:

$eMail.SendAndSaveCopy()

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 = '

This message is being sent through EWS with PowerShell

' $eMail.ToRecipients.Add('[email protected]') | 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.

稿源:It For Dummies (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合技术 » Send email Exchange Web Services PowerShell

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录