Send Authenticated SMTP with PowerShell

Send Authenticated SMTP with PowerShell

4.5/5 - (4 votes)

Today, while I was testing out some transport rules, I wanted to send a bunch of test messages to make sure they were firing correctly.  I wanted to create some custom messages and be able to automate them, and I wanted to use an outside relay service that requires SMTP authentication.

It took a good bit of tinkering, but here’s what I cobbled together:

# Sender and Recipient Info
$MailFrom = ""
$MailTo = ""

# Sender Credentials
$Username = ""
$Password = "SomePassword"

# Server Info
$SmtpServer = ""
$SmtpPort = "2525"

# Message stuff
$MessageSubject = "Live your best life now" 
$Message = New-Object System.Net.Mail.MailMessage $MailFrom,$MailTo
$Message.IsBodyHTML = $true
$Message.Subject = $MessageSubject
$Message.Body = @'
<!DOCTYPE html>
This is a test message to trigger an ETR.

# Construct the SMTP client object, credentials, and send
$Smtp = New-Object Net.Mail.SmtpClient($SmtpServer,$SmtpPort)
$Smtp.EnableSsl = $true
$Smtp.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)

There’s a few other interesting properties to the message and ways to get them in there.  As I am wont to do, I like to dig and poke around:

As you see, there are are plenty of things you can do.  Want to add an attachment?  Easy as pie:


Of course, maybe you didn’t want to share both pie recipes.  I know how you are.  You can remove them (though it’s not quite as intuitive, at least when I’ve tried to do it).

$Message.Attachments.RemoveAt("0") # Remove the attachment at index 0
$Message.Attachments.RemoveAt("1") # Remove the attachment at index 1

One of the interesting properties that we have available is Headers.  The MSDN documentation doesn’t have too much on it, but if you want to use it to add a custom header, you can use the … wait for it … Add method:


In this example, I populated $Message.Body with a Here-String.  If you have a larger HTML message body, you can also import it using Get-Content:

$Message.Body = Get-Content htmlemail.html

And yes, I’m familiar with Send-MailMessage.  One of the things it doesn’t have is the ability to modify message headers, so when you absolutely, positively need to emulate some very specific parameters or settings, accept no substitutes.

Hopefully this is helpful to someone out there in the universe.  If not, just disregard as the ramblings of an increasingly older man.


Published by Aaron Guilmette

Helping companies conquer inferior technology since 1997. I spend my time developing and implementing technology solutions so people can spend less time with technology. Specialties: Active Directory and Exchange consulting and deployment, Virtualization, Disaster Recovery, Office 365, datacenter migration/consolidation, cheese.

Reader Comments

    1. Well, they’re supposed to be differentiated by the From and Sender properties, but my understanding is the .NET ignores the Sender.

      You can try this:
      # Create message
      $Message = New-Object System.Net.Mail.MailMessage

      # P1 address
      $Message.From = New-Object System.Net.Mail.MailAddress ‘’,’P1 Sender’

      # P2 address
      $Message.Sender = New-Object System.Net.Mail.MailAddress ‘’,’P2 Sender’

      $Message.Subject = ‘Test senders’
      $Message.Body = @’
      Check out my sender names!

      -I’m P1 or P2, but you’ll never know

      # Send using SmtpClient
      $SmtpClient = New-Object System.Net.Mail.SmtpClient ‘’

  1. Nice script. Improvement idea > after sending the email successfully, copy it to the “sent items” folder of the account which is used to authenticate.
    many moons back, I was looking for a similar script & needed the option to see sent emails.

    G****e implements this in their SMTP server ( which is not in the SMTP specifications ) and I had a tough time convincing the customer to move their application email from G to an exchange server. eventually moved it to a script which used EWS instead of SMTP and has this capability.

    1. The primary answer is in my example, I needed to set specific X-headers to test transport rules (which I have since updated to make more clear–sometimes all the thoughts in my head don’t make it all the way to paper). In the System.Net.Mail.MailMessage class, we have the ability to construct and manipulate headers, which I dn’t think the Send-MailMessage cmdlet has.

      If only someone would PUT IT ALL TOGETHER.

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Exit mobile version