How to Send SMTP Email Using PowerShell (Part 1)

A useful technique for Exchange Server administrators is to be able to send email messages via SMTP from PowerShell. In this series of articles I will take you through different scripting techniques for sending email from your scripts.

There are a few different ways to do this, depending on the version of PowerShell installed on your computer or server. You can check the version number by typing $host in a PowerShell window.

PowerShell 1.0 will show this result:

PS C:\> $host

Name             : ConsoleHost
Version          : 1.0.0.0

PowerShell 2.0 will show this result:

PS C:\> $host

Name             : ConsoleHost
Version          : 2.0

Let’s take a look at how we can send SMTP email using each version of PowerShell.

Sending SMTP Email with PowerShell 1.0

For PowerShell 1.0 we can send mail by running these commands.

PS C:\> $smtp = New-Object Net.Mail.SmtpClient("ho-ex2010-caht1.exchangeserverpro.net")
PS C:\> $smtp.Send("reports@exchangeserverpro.net","administrator@exchangeserverpro.net","Test Email","This is a test")

So what did we just do there? Let’s break it down.

  1. Created a new instance of a .NET object of class SmtpClient, connected to the SMTP server ho-ex2010-caht1 (a Hub Transport server)
  2. Used the Send method of the object to send an email message with the following:
    • From address of “reports@exchangeserverpro.net”
    • To address of “administrator@exchangeserverpro.net
    • Subject of “Test Email”
    • Body of “This is a test”

That works fine, though perhaps a bit cumbersome to type it out every time. Instead what we could do is create a script to send SMTP email using PowerShell 1.0.

#
#.SYNOPSIS
#Sends SMTP email via the Hub Transport server
#
#.EXAMPLE
#.\Send-Email.ps1 -To "administrator@exchangeserverpro.net" -Subject "Test email" -Body "This is a test"
#

param(
[string]$to,
[string]$subject,
[string]$body
)

$smtpServer = "ho-ex2010-caht1.exchangeserverpro.net"
$smtpFrom = "reports@exchangeserverpro.net"
$smtpTo = $to
$messageSubject = $subject
$messageBody = $body

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)

We can save that code as Send-Email.ps1 and run it from the PowerShell window.

PS C:\Scripts> .\Send-Email.ps1 -To "administrator@exchangeserverpro.net" -Subject "Test email" -Body "This is a test"

Less typing required, especially when you hard-code some of the variables such as the From address and SMTP server.

Sending SMTP Email with PowerShell 2.0

PowerShell 2.0 makes life a little easier thanks to the built in cmdlet Send-MailMessage. To send the same email as the above example we would run this command:

PS C:\> Send-MailMessage -From "reports@exchangeserverpro.net" -To "administrator@exchangeserverpro.net" -Subject "Test email" -Body "This is a test email"

One of the first things you might notice in the command above is that no SMTP server was specified. Send-MailMessage does have a -SmtpServer parameter, but if you don’t specify one it will just use the $PSEmailServer preference variable instead. This can be set for the session by running the following command:

PS C:\> $PSEmailServer = "ho-ex2010-caht1.exchangeserverpro.net"

Another point to note with Send-MailMessage is that is uses authenticated SMTP connections. By default it will use the credentials of the user executing the command. So you need to make sure you’re using an SMTP server that either:

Those are just a few simple examples of how to send email using SMTP and PowerShell 1.0 or 2.0.

About Paul Cunningham

Paul is a Microsoft Exchange Server MVP and publisher of Exchange Server Pro. He also holds several Microsoft certifications including for Exchange Server 2007, 2010 and 2013. Connect with Paul on Twitter and Google+.

Comments

  1. Tip: Put the line defining $PSEmailServer in your profile script so you always have it.

  2. Nice info. Learned a couple of things from this, including the $PSEmailServer info and the authenticated sending part.

    Keep it up!

  3. Thanks for the article. One question though. Will this be useful to test scenarios related to journaling. For instance, I have a requirement to automate tests related to journaling. With the above steps will the exchange server send out journaling messages to the configured connectors? Thanks.

  4. Rob Roelvink says:

    By having a quick looking I realized this site contains very helpfull information related to PowerShell.

    Whenever I’ve got enough time I will have a closer look, because with PowerShell you can get almost everything you want to know and to export this information to any format as Word, Excel etc..

  5. Very Useful Info. Delivered in an excellent way !!! Thanks much !!!

Leave a Comment

*

We are an Authorized DigiCert™ SSL Partner.
Loading...

Still running Exchange 2003? Time to get moving and start your upgrade. Find out how - Click Here