Powershell is a powerful Integrated Scripting Environment (ISE) that can be used to write and debug scripts. This scripting environment can perform a wide range of functions, including sending emails using scripts and cmdlets. This can help the system administrators in automating the process of sending bulk emails.
To use PowerShell to send emails, users must specify the parameters, including the SMTP server that they want to use for pushing the mail. All these whereabouts are discussed in today’s article.
Parameters You Can Use To Send Email From PowerShell
These are some major parameters you can use to send Emails using PowerShell.
-From
: Email address of the sender-To
: Email Address of the mail Receiver-Cc:
Email address of the recipient who gets the Email message’s Carbon Copy-Attachments
: File path of the attachment that is to be sent with the Email-Subject
: Email’s subject-Body:
Body (Main message) of the Email-Credentials
: SMTP server login credentials. This is used to authenticate the SMTP server-SmtpServer
: Address of the SMTP server ( For instance: smtp.gmail.com)-Port
: Port on the SMTP server-Use Ssl
: Use Secure Sockets Layer Protocol.
Send Emails From Powershell
The most common method to send e-mails using PowerShell is by using the Send-MailMessage
cmdlet. There are other popular tools like system.netmail
API, EASendMAil component, and Microsoft Graph API.
The Send-MailMessage
cmdlet, even after being labeled obsolete by Microsoft, still is the most convenient option for sending emails from PowerShell.
- Here is a general syntax for using this cmdlet to send an E-mail.
Send-MailMessage -From "Sender Email" -To Recipients email" -Credentials (Get-Credential) -SmtpServer "SMPT server address"
For instance,
Send-MailMessage -From "yosar72796@covbase.com" -To "tnt@tnt.com" -Subject “E-mail's Subject” -Body “Main message of the E-mail” -Credentials (Get-Credential) -SmtpServer "smtp.outlook.com"
Most of the time, you can use these cmdlets without the
-Credentials
parameter. However, if the server restricts you by giving a message saying,” Client was not authenticated,” then try using the parameter. - If the E-mail is to be sent to multiple users,
Send-MailMessage -From "yosar72796@covbase.com" -To "tnt@tnt.com", "hr@tnt.com" -Credentials (Get-Credential) -SmtpServer "smtp.outlook.com"
- If you want to send the email with an attachment, you can add
-Attachment
parameter.Send-MailMessage -From "Nishant@tnt.com" -To "tnt@tnt.com", "hr@tnt.com" -Attachment "C:\temp\Some random file.txt" -Credentials (Get-Credential) -SmtpServer "smtp.outlook.com"
- To add CC, use the parameter
-CC “E-mail receiving the copy of the mail”
in the cmdlet.
Note: You can even use the value $PSEmailServer
, as the SMTP setting instead of the Smtp-Server. This is the default SMTP configuration on Powershell.
Send Email from PowerShell with Gmail SMTP
Before May 2022, in order to use Gmail SMTP to send email from PowerShell, users had to first Enable the less secure apps from Google’s security control panel.
After this feature was removed by Google, to use the Google SMPT server, users have to disable the two-factor authentication on their Gmail account, create app passwords, and then use those credentials in the PowerShell code.
Once You have set up the app password, you can use the cmdlets to send the E-mail. To use the Gmail SMPT, you must also have SSL enabled. All the required parameters to use the Gmail SMTP for sending an E-mail are used in this script:
$EmailFrom = "Nishant@gmail.com" $EmailTo = "tnt@tnt.com" $Subject = "Email Subject" $Body = "Main Message." $SMTPServer = "smtp.gmail.com" $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) $SMTPClient.EnableSsl = $true $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password"); $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Here is the list of SMTP servers you can use, along with their address and Ports.
Name | SMTP Server Address | Port |
Gmail | smtp.gmail.com | 587 |
Outlook.com | smtp.mail.outlook.com | 587 |
Yahoo | smtp.mail.yahoo.com | 587 |
AOL | smtp.mail.me.com | 587 |
AOL | smtp.aol.com | 465 |