Send Email Message using Powershell

We use this script to send email notifications from within another powershell script.  This is useful if you are running scripts thru scheduled tasks and you want to be notified of failure (or success):

[code language=”powershell” light=”true”]

# Name:  SendEmail.ps1

# To invoke this script from another powershell script:
# & ".\SendEmail.ps1"  -Subject ‘Email’ -SendFrom ‘FromAddress’ -SendTo ‘ToAddress’ -Body $emailbody

param($Subject="Unknown",$SendFrom="FromAddress",$SendTo="ToAddress",$Body="No Body")

$SMTPServer = "SMTPServerName"
$from = $SendFrom
$to = $SendTo
$subject = $Subject
$emailBody = $Body
$mailer = New-Object Net.Mail.SMTPclient($SMTPServer)
$msg = New-Object Net.Mail.MailMessage($from, $to, $subject, $emailBody)


Leave a Reply