Sending mail with PowerShell

[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 Send-MailMessage cmdlet:

$emailSmtpServer = ""
$emailFrom = "John Smith <>"
$emailTo = ""
$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

Some notes:

  • On line 1, $emailSmtpServer is the mail server that you will be using to send the message.
  • On line 2, $emailFrom contains 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 = @"
&amp;lt;p&amp;gt;Here is a message that is &amp;lt;strong&amp;gt;HTML formatted&amp;lt;/strong&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;From your friendly neighborhood IT guy&amp;lt;/p&amp;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-MailMessage command 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

The Send-MailMessage method 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 = ""
$emailSmtpServerPort = "587"
$emailSmtpUser = "username"
$emailSmtpPass = "password"

$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = "John Smith &amp;lt;;gt;"
$emailMessage.To.Add( "" )
$emailMessage.Subject = "Testing e-mail"
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = @"
&amp;lt;p&amp;gt;Here is a message that is &amp;lt;strong&amp;gt;HTML formatted&amp;lt;/strong&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;From your friendly neighborhood IT guy&amp;lt;/p&amp;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 $emailMessage with its various components, building the $SMTPClient with 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 Send-MailMessage method.

To send attachments using this method, add this before the $SMTPClient.Send line:

$attachment = "C:\myfile.txt"
$emailMessage.Attachments.Add( $attachment )

For multiple attachments, just repeat those two lines of code, with the new file name.

Multiple Recipients

If you need to your message to multiple recipients, for the Send-MailMessage method:

$emailTo = "",""

For the System.Net.Mail method:

$emailMessage.To.Add( "" )
$emailMessage.To.Add( "" )


On a side note, I’ve used a Here-String for the $emailBody variable. When sending a non-HTML formatted message, this allows you to easily insert line breaks without having to use `n notation. Even when sending an HTML formatted message, it makes it easier to read in your code.

Tagged: Tags

Leave a Reply

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