PowerShell v1.0: Script to send email messages

$smtpServer = "192.68.4.2"
$smtpFrom = “admin@example.com
$smtpTo = "reportuser@example.com"
$messageSubject = “Automation: Output Report ”
$messageBody = “Script Completed successfully without any errors.”

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)

 

If you specify an incorrect SMTP server, you’ll end up receiving the below error message:

PS C:> powershell.exe C:TestSend-Emails.ps1
Exception calling "Send" with "4" argument(s): "Failure sending mail."
At C:TestSend-Emails.ps1:29 char:19
+         $smtp.Send( <<<< $smtpFrom,$smtpTo,$messagesubject,$messagebody)
Mail already sent to [govardhan@testdom.com]

Leave a Reply

Your email address will not be published. Required fields are marked *