Send Email With Powershell

An extended version of this script can be found at Send Email With Powershell 2.
This is a basic Powershell script that will allow you to send an email. With that said, I want to point out that the top of the enclosed script has a comments section that includes filename, author, created date and a basic description of the script. While the name of the script is typically indicative of its function, using a comments block will help define any details that may need to be pointed out to others reading you work. This becomes even more important as you begin to build a script library. I cant ever say it enough, comment, comment, comment!!!!

####################################################
#
# filename: Email.ps1
# author: Matthew Marable
# created: 09/08/2012
#
# Basic email routine with attachment
#
####################################################

# Send email with log file
$emailSmtpServer = "smtp.yourserver.com"
$emailSmtpServerPort = "25"

$emailFrom = "COMPANY Automation <admin@youremail.com>"
$emailTo = "admin@youremail.com"
$emailAttachment = New-Object System.Net.Mail.Attachment("C:\\your-log.txt", "text/plain")

$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
$emailMessage.Subject = "Backup Alert: Job $JobOutput"
$emailMessage.IsBodyHtml = $false
$emailMessage.Body = "Your Body Text Here"
$emailMessage.Attachments.Add($emailAttachment)

$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.Send( $emailMessage )
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.