You can use the Send-MailMessage cmdlet to send e-mail notifications through an SMTP server. The most common use of Send-MailMessage in PowerShell scripts is when you need to send notifications to your or to a user’s mailbox. In this article, we’ll look at some examples of how to use the Send-MailMessage cmdlet to send e-mails.
If you want to send a letter, you must specify values in the following parameters:
- From — sender SMTP address
- To — recipient e-mail
- Subject — subject of email
- SmtpServer — SMTP server host name or IP address
So, the minimum PowerShell command you need to send an e-mail message is as follows:
Send-MailMessage -From "admin@theitbros.com" -To "info@theitbros.com" -Subject "Test PowerShell Message" -Body "Message body" -SmtpServer smtp.theitbros.com
This command allows you to send emails only through an SMTP server with anonymous authentication (internal or open relay).
Note. AWS SES SMTP relay configuration example.
If your server requires Basic Authentication, you can specify the credentials. Add the following option to the previous command:
-Credential theitbros\admin1
If you need to send email to multiple recipients, you can:
- Enter them separated by a comma as follows:
-To 'user1@theitbros.com', 'user2@theitbros.com'
- You can send a message copy by using the option:
-Сс 'user3@theitbros.com'
- Send a blind carbon copy:
-Bcc 'user4@theitbros.com'
All three recipient types can be specified in a single Send-MailMessage command.
You can specify a list of files to attach to the email:
-Attachments 'C:\Reports\2023april.log','C:\Report\2023april_users.csv'
You can attach all of the files in the specified directory to the letter in a single operation:
Get-ChildItem -Path 'C:\reports' | Send-MailMessage …
Note. The statement containing the Get-ChildItem and For-Each commands allows you to loop through all files in the specified folder.
In the real world, most SMTP servers require you to use SSL to connect, so you will need to specify additional parameters -UseSSL and -Port 587.
In the following example, we’ll show you how to use PowerShell to send emails through Gmail’s SMTP server.
First, you need to set up your Google account:
- Sign in to your Google Account;
- Navigate to https://myaccount.google.com/security and enable “2-Step Verification”;
- Then scroll down to the App passwords section;
- Create a new password: type its name (PowerShell_SMTP) and click Generate;
- Copy your 16-character application password. This is the password that we will use to authenticate on Gmail SMTP from PowerShell.
Hint. Previously, we showed you how to connect Gmail mailbox in Outlook with using an App Password.
You can now send an email through Gmail’s SMTP:
$Sender='itbros@gmail.com' $AppPassword = "gmail_app_password" | ConvertTo-SecureString -AsPlainText –Force $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Sender, $AppPassword Send-MailMessage -Credential $credential -UseSSL -SmtpServer 'smtp.gmail.com' -Port 587 -From $Sender -To 'info@theitbros.com' -Subject 'Send Messahe via Gmail SMTP' -Body "This message has been sent from the host $($env:computername)"
Open the recipient’s mailbox and check if the email was delivered successfully.