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.
- Part 1 – How to Send SMTP Email Using PowerShell
- Part 2 – How to Add a Message Body to Emails Sent from Scripts
- Part 3 – How to Add a HTML Message Body to Emails Sent from Scripts
- Part 4 – How to Create Formatted HTML Output from 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 : 22.214.171.124
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("firstname.lastname@example.org","email@example.com","Test Email","This is a test")
So what did we just do there? Let’s break it down.
- Created a new instance of a .NET object of class SmtpClient, connected to the SMTP server ho-ex2010-caht1 (a Hub Transport server)
- Used the Send method of the object to send an email message with the following:
- From address of “firstname.lastname@example.org”
- To address of “email@example.com
- 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 "firstname.lastname@example.org" -Subject "Test email" -Body "This is a test" # param( [string]$to, [string]$subject, [string]$body ) $smtpServer = "ho-ex2010-caht1.exchangeserverpro.net" $smtpFrom = "email@example.com" $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 "firstname.lastname@example.org" -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 "email@example.com" -To "firstname.lastname@example.org" -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:
- Permits that authenticated user credential to send email messages via SMTP
- Accepts anonymous SMTP/relay for the IP address of the sending host (you can see how to configure a relay connector for Exchange here)
Those are just a few simple examples of how to send email using SMTP and PowerShell 1.0 or 2.0.