Gmail allows the use of SMTP, we can use SMTP for sending e-mail notifications from within PowerShell.
The first step is to register an e-mail address at Gmail. This is a very straight forward step so this will not be included in this blog post.
So, when you have registered with Gmail we can add the following code to our PowerShell script:
$username = “[email protected]“ $password = ConvertTo-SecureString ‘yourpassword‘ –asplaintext –force $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password Send-MailMessage -From [email protected] -Subject Test -To [email protected] -Body ` “ The script has finished. And this is the second line. ” ` -Credential $cred -Port 587 -SmtpServer smtp.gmail.com -UseSsl | out-null
Now, when you execute this PowerShell code it will send an e-mail with the notification that the script has finished.
The first time you use PowerShell to send an e-mail it could be that Gmail does not allow this. This is due to the fact that the security settings are set to high.
When this is the case, Gmail will send you an e-mail where you can allow the use of SMTP by decreasing the security settings.