PowerShell: Sending Email With Send-MailMessage (Gmail example)

It’s late on a Friday night. Like most people, you have somewhere else you would rather be. Instead, you are stuck at
work because you completely forgot about some scheduled maintenance that has to be done that night.

You’re familiar enough with PowerShell to make some scripts to take care of the maintenance, but you’re worried about the off-chance that one of the scripts fails or you’re worried that something could go wrong.

Wouldn’t it be great if you could start your scripts and configure them to email you the results? You could still get out, enjoy your Friday night and rest assured that your scripts are doing their job without you losing yours!

(Disclaimer: We do not condone the shirking of any responsibilities that you may have…especially on Friday nights.)

Sending Email with PowerShell (Send-MailMessage)

There are a multiple ways to send an email with PowerShell. This is a native cmdlet option that is simple and easy to use. It uses the cmdlet Send-MailMessage.

Send-MailMessage <parameters>

List of available parameters

Attachments string
The full path and filenames to be attached to the email.

Bcc string
Email addresses that you would like to bcc.

Body string
The body of the email message.

BodyAsHtml
Indicates that the value of the Body parameter contains HTML.

Cc string
Email addresses that you would like to cc.

Credential PSCredential
An account that has permission to send the email. The default is the current user.

DeliveryNotificationOption Option
Delivery notifications will be sent to the email address specified in the –From parameter.

None – No notification (default)
OnSuccess – Notify if the delivery is successful.
OnFailure – Notify if the delivery is unsuccessful.
Delay – Notify if the delivery is delayed.
Never – Never notify.

Encoding Encoding
The encoding used for the body and subject.

From string
The address from which the mail is sent.

Priority Priority
The priority of the email message. Valid values: Low, Normal (default), High

SmtpServer string
The name of the SMTP server that sends the email message.

Subject string
The subject of the email message.

To string
The addresses you wish to send email to.

UseSsl
Use the Secure Sockets Layer (SSL) protocol to establish a connection to the SMTP server to send email. SSL is not used by default.

Examples of Usage:

Example 1 – Sending a simple email

Send-MailMessage -From “email@yourdomain.com” -To “AnotherEmail@yourdomain.com” -Credentials (Get-Credential) -SmtpServer “smtp.yourdomain.com”

Example 2 – Sending an email from Gmail

Here’s a full example of sending an email via Gmail’s SMTP servers. It will prompt you for a username and password thanks to (Get-Credential). The username needs to be and password needs to be , though you could always pipe in a PSCredential and get the same effect.

##############################################################################
$From = "YourEmail@gmail.com"
$To = "AnotherEmail@YourDomain.com"
$Cc = "YourBoss@YourDomain.com"
$Attachment = "C:\temp\Some random file.txt"
$Subject = "Email Subject"
$Body = "Insert body text here"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential (Get-Credential) -Attachments $Attachment
##############################################################################

Best of luck to you in your emailing endeavors!