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):


# 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)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s