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.
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"
Send-MailMessage -From "firstname.lastname@example.org" -To "email@example.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
-Credentialsparameter. 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 "firstname.lastname@example.org" -To "email@example.com", "firstname.lastname@example.org" -Credentials (Get-Credential) -SmtpServer "smtp.outlook.com"
- If you want to send the email with an attachment, you can add
Send-MailMessage -From "Nishant@tnt.com" -To "email@example.com", "firstname.lastname@example.org" -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 = "email@example.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|