[June 27, 2014] Update: I’ve made several updates, including multiple recipients and sending attachments.
If you’ve created a PowerShell script that runs as a scheduled task, you may want to have it send you an e-mail with a log file or other notification about what the script did or didn’t do (i.e. error notification). PowerShell version 2 provides the
Send-MailMessage cmdlet to do this, but in some cases the older
System.Net.Mail .NET namespace is a more desirable approach (
System.Net.Mail also works in PowerShell version 1).
The Send-MailMessage Method
Here is an example of the
$emailSmtpServer = "mail.somewhere.com" $emailFrom = "John Smith &lt;firstname.lastname@example.org&gt;" $emailTo = "email@example.com" $emailSubject = "Testing e-mail" $emailBody = @" Here is a message From your friendly neighborhood IT guy "@ Send-MailMessage -To $emailTo -From $emailFrom -Subject $emailSubject -Body $emailBody -SmtpServer $emailSmtpServer
- On line 1,
$emailSmtpServeris the mail server that you will be using to send the message.
- On line 2,
$emailFromcontains a friendly name (i.e. “John Smith”) followed by John’s e-mail addressed, enclosed in <>.
You can use HTML formatting in the
$emailBody if you specify the
-BodyAsHTML flag in the Send-MailMessage command, like this:
$emailBody = @" &lt;p&gt;Here is a message that is &lt;strong&gt;HTML formatted&lt;/strong&gt;.&lt;/p&gt; &lt;p&gt;From your friendly neighborhood IT guy&lt;/p&gt; "@ Send-MailMessage -To $emailTo -From $emailFrom -Subject $emailSubject -Body $emailBody -BodyAsHTML -SmtpServer $emailSmtpServer
That message will come out looking something like this:
Here is a message that is HTML formatted.
From your friendly neighborhood IT guy
If you need to send an attachment, change your
Send-MailMessagecommand to something like this:$attachment = "C:\myfile.txt" #Or do multiple attachments like this: $attachment = "C:\myfile1.txt","C:\myfile2.txt" Send-MailMessage -To $emailTo -From $emailFrom -Subject $emailSubject -Body $emailBody -BodyAsHTML -Attachments $attachment -SmtpServer $emailSmtpServer
The System.Net.Mail Method
Send-MailMessagemethod works well, if you have an SMTP server that does not require authentication and works on the standard SMTP port (port 25). If you need to send the message through an e-mail system such as GMail or Exchange Online which requires authentication and TLS on port 587, then using the
System.Net.Mail.NET namespace is a good way to go.$emailSmtpServer = "mail.somewhere.com" $emailSmtpServerPort = "587" $emailSmtpUser = "username" $emailSmtpPass = "password" $emailMessage = New-Object System.Net.Mail.MailMessage $emailMessage.From = "John Smith &lt;firstname.lastname@example.org&gt;" $emailMessage.To.Add( "email@example.com" ) $emailMessage.Subject = "Testing e-mail" $emailMessage.IsBodyHtml = $true $emailMessage.Body = @" &lt;p&gt;Here is a message that is &lt;strong&gt;HTML formatted&lt;/strong&gt;.&lt;/p&gt; &lt;p&gt;From your friendly neighborhood IT guy&lt;/p&gt; "@ $SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort ) $SMTPClient.EnableSsl = $true $SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass ); $SMTPClient.Send( $emailMessage )
Essentially, what we are doing is building the mail message
$emailMessagewith its various components, building the
$SMTPClientwith the server and credential information, and then using that to send the mail message. This obviously takes some more code, but it is more flexible than the
To send attachments using this method, add this before the
$SMTPClient.Sendline:$attachment = "C:\myfile.txt" $emailMessage.Attachments.Add( $attachment )
For multiple attachments, just repeat those two lines of code, with the new file name.
If you need to your message to multiple recipients, for the
Send-MailMessagemethod:$emailTo = "firstname.lastname@example.org","email@example.com"
System.Net.Mailmethod:$emailMessage.To.Add( "firstname.lastname@example.org" ) $emailMessage.To.Add( "email@example.com" )
On a side note, I’ve used a Here-String for the
$emailBodyvariable. When sending a non-HTML formatted message, this allows you to easily insert line breaks without having to use
`nnotation. Even when sending an HTML formatted message, it makes it easier to read in your code.