How to send Emails using PowerShell

In this tutorial we will see how we can send emails using the tools provided by PowerShell.

We are going to use Send-MailMessage utility provided by PowerShell to send emails in this tutorial.

Objective -

Send email to multiple recipients along with an attachment. Details of the emails we have to send are specified below:

The usage of PowerShell Send-MailMessage command to send emails is as shown below. We are sending the email using Gmail SMTP server, but the details of any other SMTP server could be specified in the similar manner.

Send-MailMessage `
-To "test_recipient@gmail.com" `
-CC "test_cc@gmail.com" `
-Subject "Hello from Poweshell" `
-Body "Hello Receiver" `
-SmtpServer "smtp.gmail.com" `
-From "test_sender@gmail.com" `
-Port 587 `
-UseSsl `
-Credential "test_password" `
-Attachments "mail.ps1"

In this example we hard-coded our gmail password in the script directly. This is not ideal as the password will be visible to the anybody who has the access to the source code. To solve this problem, now we will see how we can store our password in a file in an encrypted manner, which we can then use in our script.

We are now going to use following commands to take the password as input from the user and store that securely in an XML file.

CommandDescriptionGet-CredentialsThis command take password as input from the user and create a credential object from that.Export-ClixmlCommand to create an XML based representation of an object and store that in a fileImport-ClixmlThis command imports the CLIXML file created using Export-Clixml and create object in PowerShellWe will combine the commands described above to take the password from user and store that in an XML file. Later on we will use the Import-Clixml command to read our password from the file.

Following code will store the credentials in credentials.xml file. Note that the Get-Credentials command store the password as a secured system string which is encrypted using the Windows user information and hence is intelligible only to the windows user which was used to input the password.

Get-Credential | Export-Clixml credentials.xml

Enter the username and password when prompted for.

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
User: test_sender@gmail.com
Password for user test_sender@gmail.com: *************

Now we will modify the Send-MailMessage command used before to take the password from the credentials.xml file created before.

Send-MailMessage `
-To "test_recipient@gmail.com" `
-CC "test_cc@gmail.com" `
-Subject "Hello from Poweshell" `
-Body "Hello Receiver" `
-SmtpServer "smtp.gmail.com" `
-From "test_sender@gmail.com" `
-Port 587 `
-UseSsl `
-Credential (Import-Clixml .\credentials.xml) `
-Attachments "mail.ps1"

Send-MailMessage cmdlet provide several other useful functionalities related to sending emails, like sending HTML emails, keeping blind carbon copies in the mails etc. I would suggest you use Get-Help command in PowerShell to know more about the commands used in this tutorial.

Get-Help Send-MailMessage

This article was originally written on my blog at www.amritanshu.in