Send Email Message using Powershell

Reading Time: 1 minute

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)

$mailer.send($msg)
[/code]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.