Send Mails From PowerShell Using Office 365 SMTP Server

Introduction

We have the capability to send mails from PowerShell. We can either send body as text or even as attachments in the mail. In order to facilitate the mail sending functionality we can make use of the various SMTP Servers available. In this demo we will see how to make use of Office 365 SMTP Servers to relay mails to business users. As part of the walk through we will see how to send plain text as mail body.

Prerequisite

In the demo, we will be reading JSON data from a REST service and we will be using Office 365 SMTP Server to relay the mail to the business users. Before heading over to the mail sending functionality let’s have a look at the REST Service that we will be using for the demo.

REST Web Service

Though we can create a custom REST service, for the time being, we can try out an already existing one. We will be making use of the Free RESTful web services available here which we will consume from PowerShell. We shall make use of the specific REST web service  that gets the states and territories of the US.

The REST end point that we will consume is,

http://services.groupkt.com/state/get/USA/all

PowerShell

PowerShell Version 3.0

We are using the cmdlet ‘Invoke-WebRequest’, which was introduced in V3.0, to invoke REST Web service and get data from the web. So we have to ensure that the system has PowerShell version 3.0 or above. We can check the version by running the below command.

(Get-Host).Version

PowerShell

Invoke REST Web Service from PowerShell

REST web service endpoint can be invoked from PowerShell using the ‘Invoke-WebRequest’ cmdlet. Once we have the returned data, it will be in JSON format but to work with it we have to convert it to PowerShell object.The ConvertFrom-Json cmdlet will be used to convert the JavaScript Object Notation (JSON) formatted string to a custom PSCustomObject object that has a property for each field in the JSON string. Similarly, to generate a JSON string from any object we can use the ConvertTo-Json cmdlet.

PowerShell

Since the properties (country,capital,largest_city) are stored in the ‘RestResponse’ and ‘result’ property as embedded objects, we need to expand those properties using ‘-expand’ attribute and then choose the required properties from the embedded object. Once the JSON data has been retrieved, we will use ‘ConvertFrom-Json’ to convert the data from JSON to PowerShell Custom Objects

Invoking the Web Request and retrieving the required properties after running the ConvertFrom-JSON command will fetch us the below results,

PowerShell

Code

  1. $request = 'http://services.groupkt.com/state/get/USA/all'
  2. $result = Invoke-WebRequest $request
  3. $JSONResult = $result | ConvertFrom-Json | select -expand RestResponse | select -expand result
  4. $JSONResult | Select country,capital,largest_city| Sort-Object capital

Convert data to HTML

In order to send the above table as mail body, we will convert it to HTML using the ConvertTo-HTML command.

  1. $Body = $JSONResult | Select country,name,capital,largest_city| Sort-Object name | ConvertTo-HTML

Mail from PowerShell using Office 365 SMTP

In order to send an email from PowerShell, we will need to specify an SMTP Server. We will be using Office 365 SMTP to relay the mails. We will have to fill out couple of parameters before triggering the ‘Send-MailMessage’ command which will relay the email.

  • $SmtpServer = 'smtp.office365.com' – This is the address of the Office 365 SMTP server we will be using.
  • $SmtpUser = '[email protected]' – Specify your Office 365 User ID
  • $smtpPassword = ‘<Input Office 365 Password Here>’ – Specify your Office 365 Password
  • $MailtTo = '[email protected]' – Specify the users to which the mails should be sent
  • $MailFrom = '[email protected]' – Specify the Source Email ID
  • $MailSubject = "Test using $SmtpServer" – This is the Mail Subject line

Finally, create a credential object using the User name and the password we have entered above. Since we are using Office 365 SMTP server, we have specified the related credentials. If you are using Gmail SMTP, make sure that you use Gmail credentials.

$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $SmtpUser, $($smtpPassword | ConvertTo-SecureString -AsPlainText -Force)

PowerShell

Ensure that the credentials are correct else we will get the above error. Thus we have set up the parameters. Now we can call the Send-MailMessage command to send the mail to users as shown below :

Send-MailMessage -To "$MailtTo" -from "$MailFrom" -Subject $MailSubject -Body "$Body" -SmtpServer $SmtpServer -BodyAsHtml -UseSsl -Credential $Credentials

Full Code

  1. $request = 'http://services.groupkt.com/state/get/USA/all'  
  2. $result = Invoke-WebRequest $request -UseBasicParsing  
  3. $JSONResult = $result | ConvertFrom-Json | select -expand RestResponse | select -expand result  
  4. $Body = $JSONResult | Select country,name,capital,largest_city| Sort-Object name | ConvertTo-HTML  
  5. $SmtpServer = 'smtp.office365.com'  
  6. $SmtpUser = '[email protected]'  
  7. $smtpPassword = ‘<Input Office 365 Password Here>’  
  8. $MailtTo = '[email protected]'  
  9. $MailFrom = '[email protected]'  
  10. $MailSubject = "Test using $SmtpServer"  
  11. $Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $SmtpUser, $($smtpPassword | ConvertTo-SecureString -AsPlainText -Force)  
  12. Send-MailMessage -To "$MailtTo" -from "$MailFrom" -Subject $MailSubject -Body "$Body" -SmtpServer $SmtpServer -BodyAsHtml -UseSsl -Credential $Credentials  
  13. write-Output "Custom Message : REST Service JSON Data parsed and Email Sent to Business Users"  

Let's save the above code to a ps1 file and run it from the console.


PowerShell

We can see that the mail has been triggered and has reached the inbox.

PowerShell

Summary

Thus, we saw how to send mails from PowerShell using Office 365 SMTP Server.

Next Recommended Readings