Send Email With Powershell 2

This is an extended Powershell script that will allow you to send an email based on my original article Send Email With Powershell. With that said, this script has been extended to allow for input parameters and SMTP authentication. The input parameters allow the script to be called from other powershell scripts.

########################################################
#
# EmailAdmin.ps1
# Author: Matthew Marable
# Updated: Apr 12, 2013
#
# Description:
# Send Email from powershell using SMTP Authentication
#
########################################################

param(
[string]$to,
[string]$subject,
[string]$body,
[string]$attachment = "")

$emailSmtpServer = "10.10.15.25"
$emailSmtpServerPort = "25"

$emailFrom = "SRV Automation <your@from.email>"
$emailTo = @($to)

$emailAttachment = New-Object System.Net.Mail.Attachment($attachment, 'text/plain')

$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
$emailMessage.Subject = $subject
$emailMessage.IsBodyHtml = $false
$emailMessage.Body = $body
$emailMessage.Attachments.Add($emailAttachment)

$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("your_smtp_user", "your_smtp_pass");
$SMTPClient.Send( $emailMessage )
Note: this script can called from any powershell script using the following code below.
'C:\Admin Tools\EmailAdmin.ps1' "tech@youremail.com" "MySQL Backup Alert: Job Success (Server: DB01)" "Completed Successfully."
Did you find this article useful? Why not share it with your friends?

Leave a Reply

Your email address will not be published. Required fields are marked *

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