To send email messages through an SMTP server, you can use the Send-MailMessage PowerShell cmdlet. You can use this built-in cmdlet to send emails in PowerShell version 2.0 and newer (previously you can use the .Net System.Net.Mail
class to send emails). In this article we’ll show how to use Send-MailMessage
to send emails from PowerShell scripts.
To get the syntax of the cmdlet, run this command:
get-help Send-MailMessage
Send-MailMessage [-To] <String[]> [-Subject] <String> [[-Body] <String>] [[-SmtpServer] <String>] [-Attachments <String[]>] [-Bcc <String[]>] [-BodyAsHtml] [-Cc <String[]>] [-Credential <PSCredential>] [-DeliveryNotificationOption {None | OnSuccess | OnFailure | Delay | Never}] [-Encoding <Encoding>] -From <String> [-Port <Int32>] [-Priority {Normal | Low | High}] [-UseSsl] [<CommonParameters>] The Send-MailMessage cmdlet sends an email message from within Windows PowerShell.
Here are the main options:
- From is a sender address (If the SMTP server doesn’t check the sender’s address and allows to send email anonymously, you don’t need to provide a real smtp address. You can send email message on behalf of any email address);
- To – recipient email address;
- SMTPServer –the address of the SMTP server through which you want to send the email.
$PSEmailServer
environment variable, you don’t need to specify the SMTP server address in the Send-MailMessage cmdlet.
The following simple PowerShell command will send an email with the specified subject and body to multiple recipients.
Send-MailMessage -From '[email protected]' -To '[email protected]','[email protected]' -Subject "Test Email Alert" -Body "This is email body text" –SmtpServer 'smtp.woshub.com'
To make it easier to edit the attributes of a cmdlet, the send email command can be represented as follows:
Send-MailMessage `
-SmtpServer smtp.woshub.com `
-To '[email protected]','[email protected]' `
-From '[email protected]' `
-Subject "Test" `
-Body "Sending email using PowerShell" `
-Encoding 'UTF8'
Note that in the last command we additionally set the UTF8 encoding for the email. Otherwise, if the email subject or body contains non ANSI characters, they will be displayed incorrectly.
By default, ANSI and ASCII encodings are used in Windows PowerShell. If you’ve updated your PS version to PowerShell Core, keep in mind that this version already uses UTF-8 encoding by default.By default, the Send-MailMessage cmdlet tries to send an email via the standard SMTP port TCP 25. If your SMTP server allows to send email only using an encrypted protocol, you can specify the port number (the most often it is 465 or 587) and the UseSsl option:
-SmtpServer 'smtp.woshub.com' -Port 465 –UseSsl
Name | SMTP Server Address | Port | Encryption Type |
Gmail | smtp.gmail.com | 587
25 465 |
TLS
TLS SSL |
Office 365 | smtp.office365.com | 587 | TLS |
Outlook.com | smtp-mail.outlook.com | 587 | TLS |
Yahoo | smtp.mail.yahoo.com | 587 | TLS |
iCloud mail | smtp.mail.me.com | 587 | TLS |
AOL | smtp.aol.com | 465 | SSL |
If the SMTP server prohibits sending emails anonymously (relay is denied), you will see this error:
5.7.1 Client was not authenticated.
Then you can authenticate on the SMTP server using the –Credential option.
You can interactively request user credentials to authenticate:
Send-MailMessage …… -Credential (Get-Credential)
Also, you can specify the account to be used for authentication in the variable:
$cred = Get-Credential
Send-MailMessage ... -Credential $cred
If you want to save a password to connect to the SMTP server directly in the PowerShell script, use this syntax:
$mypasswd = ConvertTo-SecureString "smP@ssdw0rrd2" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("[email protected]", $mypasswd)
Send-MailMessage ... –Credential $mycreds
If you want to add an attachment to your email, use the –Attachments
option. In the example below, we will send an email in the HTML format and attach file1.txt and install.log from the local disk. We will use the Gmail SMTP server (first you need to create an app password in Gmail and use it for smtp authentication instead of your Gmail password):
$MailMessage = @{
To = "[email protected]"
Bcc = "[email protected]", "[email protected]"
From = "[email protected]"
Subject = "DC Server Report"
Body = "<h1>Welcome!</h1> <p><strong>Generated:</strong> $(Get-Date -Format g)</p>”
Smtpserver = "smtp.gmail.com"
Port = 587
UseSsl = $true
BodyAsHtml = $true
Encoding = “UTF8”
Attachment = “C:\Logs\file1.txt”, “C:\Logs\install.log”
}
Send-MailMessage @MailMessage -Credential $cred
Here is how the email in the HTML format with attachments looks like in the Gmail interface.
You can configure a delivery notification (read receipts) for an email using -DeliveryNotificationOption
. A delivery notification allows you to be notified if the recipient receives an email.
The available notification types are:
- OnSuccess (Notify if the delivery is successful)
- OnFailure (Notify if the delivery is unsuccessful)
- Delay (Notify if the delivery is delayed)