http://www.sqlservercentral.com/blogs/sql_server_dba/2012/01/31/sql-server-send-email-using-powershell/

Printed 2014/07/22 09:44PM

SQL Server – Send email using Powershell

2012/01/31

This article explains how to send an email through Powershell. 

 Very useful if you have a requirement to automate your scripts and send notifications .

 The example is calling the SmtpClient Class , which is part of the .Net Framework.

You can call a Function –  which makes your scripting   more modular.

 

#variables
$emailFrom = "JackVamvas@sqlserver-dba.com"
$emailTo = "dba@sqlserver-dba.com"
$subject = "SQLServer-DBA.com: Powershell Function calling an SMTP server"
$body = "SQLServer-DBA.com : Send an email through SMTP in Powershell"
$smtpServer = "aservername"

#create a function
Function sendEmail([string]$emailFrom, [string]$emailTo, [string]$subject,[string]$body,[string]$smtpServer)
{
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom,$emailTo,$subject,$body)
}

#call the function 
sendEmail $emailFrom $emailTo $subject $body $smtpServer

 

See Also

Powershell - run script on all sql servers

 

Author: Jack Vamvas (http://www.sqlserver-dba.com)
Copyright © 2002-2014 Simple Talk Publishing. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.