Follow sqlserver-dba.com

Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions

SQLServer-DBA.com Links

Dba_db2_button

dba-ninja.com

SQL Server DBA Jobs

SQL Server – Send email using Powershell

31 January,2012 by Tom Collins

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 = "[email protected]"
$emailTo = "[email protected]"
$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: Tom Collins (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on SQL Server – Send email using Powershell


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer