PDQ.com mobilePDQ.com desktop
Support

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

Kris PowellKris Powell
·

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

ParameterTypeDescription
–AttachmentsstringThe full path and filenames to be attached to the email.
–BccstringEmail addresses that you would like to bcc.
–BodystringThe body of the email message.
–BodyAsHtmlbooleanIndicates that the value of the Body parameter contains HTML.
–CcstringAddresses that you would like to cc.
–CredentialPSCredentialAn account that has permission to send the email. The default is the current user.
-DeliveryNotificationOptionDeliveryNotificationOptionsDelivery 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.
–EncodingEncodingThe encoding used for the body and subject.
–FromstringThe address from which the mail is sent.
-PortintegerSpecifies an alternate port on the SMTP server. The default value is 25, which is the default SMTP port.
–PriorityMailPriorityThe priority of the email message. Valid values: Low, Normal (default), High
–SmtpServerstringThe name of the SMTP server that sends the email message.
–SubjectstringThe subject of the email message.
–TostringThe addresses you wish to send email to.
–UseSslbooleanUse 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 protected].com” -To “[email protected].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 = "[email protected]"
$To = "[email protected]"
$Cc = "[email protected]"
$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.

  • Did you know that PDQ Deploy has a PowerShell step you can use to deploy your scripts?


This blog post is part of a series on PowerShell:

Ready to put the PowerShell scanner through its paces?

Take our 14-day Free Trial. Feel the power of the PowerShell scanner.
Start a Trial

Don't miss the next post!

Using PowerShell to Install Printers

When installing printers, we will need to do the four things; Add Driver to the Store, Install the Driver, Create Printer Port, and Install the Printer