Tuesday, 19 May 2020

Send Email using PowerShell script

Windows PowerShell is a Windows command-line shell designed for system administrators. We can use Send-MailMessage cmdlet to send an email from PowerShell using SMTP protocol.

Copy below script in notepad, change required parameter and save it as SendMail.ps1

[System.Net.ServicePointManager]::SecurityProtocol = 'Tls,TLS11,TLS12'
$From = "Sender email address"
$To = "recipient’s email address"
$Cc = "cc recipient’s email address"
$Subject = "Your Subject"
$Body = "Message Body"
$SMTPServer = "SMTP Server address"
$SMTPPort = "SMTP Server port"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential (Get-Credential)

Post saving file, Open PowerShell and run command .\SendMail.ps1


It will prompt you for the login credentials.



Note : 

- This script is 100% working with gmail. You must enable less secure app or need to create app password to use this script.

How to enable less secure App in Gmail. - Click Here.
How to create App Password in Gmail. - Click Here.