Home » Exchange Server » How to Send SMTP Email Using PowerShell (Part 1)

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:

PowerShell 2.0 will show this result:

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.

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.

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

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:

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:

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.

Paul is a Microsoft MVP for Office Servers and Services. He works as a consultant, writer, and trainer specializing in Office 365 and Exchange Server. Paul is a co-author of Office 365 for IT Pros and several other books, and is also a Pluralsight author.
Category: Exchange Server


  1. Anand says:

    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.

  2. 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..

  3. Boball says:

    Can you help me with a simple problem I am having due to not being very good at Powershell.

    I am using a super-so-simple get-content command to read a file with a list of users and move their mailbox to another mailstore and I would like to be able to use a send-mailmessage at the end to email each user to say the move has finished.

    The line I am using is as follows,
    Get-Content C:users.txt | Move-Mailbox -TargetDatabase “ServernameFirst Storage GroupMailbox Database” -Confirm:$false

    This works OK enough but I would like to finish it off with the email functionality but I don’t know how to use the outputs of Get-Content such a System.Object, which I presume would do it for me if I knew my a*** from my elbow!
    Any help greatly appreciated.

  4. Sunil says:

    I am trying to send mail by same command as gave above i.e.
    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”)

    However i am getting error “Exception calling “Send” with “4” argument(s): “Failure sending mail.”
    At line:1 char:11″

    Can you please help me in this.

    Appreciate your kind help.

    • DIDO says:

      if i’ve understand you can add ath the end of command -smtpserver and put @IP or mail server name,without using objects.try this

      Send-MailMessage -From “reports@exchangeserverpro.net” -To “administrator@exchangeserverpro.net
      ” -Subject “Test email” -Body “This is a test email” -smtpserver “IP or server’s name”

  5. Pat Richard says:

    Did you change the name of the SmtpClient to match your server name? And change the to/from address to match valid addresses?

  6. Jon says:

    I’m using Send-Mailmessage to generate a number of messages via foreach loop, but it recycles the session for each one. The relay server limit is 15 messages per smtp session, so I’m wondering if there is a way to force close the current SMTP session and make send-mailmessage start a new one.


  7. Christiaan van der Kooij says:

    If you want to foricbly send mails anonymous you can use the following script, it also reads a logfile for error strings and parses it in the e-mail for added benefit. 🙂

    $User = “anonymous”
    $PWord = ConvertTo-SecureString –String “anonymous” –AsPlainText -Force
    $Creds = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $user, $pword

    Send-MailMessage -From “from@from.com” -To “to@to.com”
    -SmtpServer "localmailserveriphere"

    -Subject “Subject”

    -Credential $creds
    -Body @"
    Errors! "
    $(Get-ChildItem C:TEMPsynctool*.log | `
    Select-String -Pattern errors -AllMatches -SimpleMatch |
    Foreach-Object {$_.Line}

  8. Raul says:

    Hi, I wrote this code to send email requesting a few parameters to send an attached file with information between exchange servers in two different organizations

    Write-Host -NoNewLine “Ingrese el fqdn del dominio de correo: ” -ForeGroundColor Yellow;$Suffix=Read-Host
    Write-Host -NoNewLine “Ingrese el nombre de nuestro servidor Exchange: “-ForeGroundColor Yellow; $Server=Read-Host
    Write-Host -NoNewLine “Ingrese la dirección de correo destino: “-ForeGroundColor Yellow;$Receipt_Mail_Address=Read-Host
    Write-Host -NoNewLine “Ingrese la dirección de correo remitente: “-ForeGroundColor Yellow;$Sender_Mail_Address=Read-Host
    Write-Host ” ”
    Write-Host ” ”


    $msg=new-object Net.Mail.MailMessage
    $att=new-object Net.Mail.Attachment($filename)
    $smtp=new-object Net.Mail.SmtpClient($Server)

    $msg.Subject = “Correo automatizado – Contacts-“+$Suffix+”.TXT”
    $msg.Body=“Envío automatizado del archivo Contacts-“+$Suffix+”.TXT”

    Write-Host “Enviando Correo automatizado, por favor espere … ” -ForeGroundColor Green
    Write-Host ” ”
    Write-Host ” ”
    Write-Host “Correo enviado” -ForeGroundColor Green

    In Exchange 2010 it works fine, but in Exchange 2013 I receive an SMTP error 5.7.1 unable to relay, thats mean the script works fine but the server are not allowing to relay to the remote host, how can i solve this?

  9. Raul says:

    Researching a little bit i found the solution!
    It was just add a one more line


    The script is working fine now!

  10. Amy Hu says:

    Thank you for your sharing. I tried with your command to send email from Office 365. But with some of the computer, got error:

    Exception calling “Send” with “4” argument(s): “The remote certificate is invalid according to the validation procedure”

    Can you please me with this error?

    Thank you.

    • Amy Hu says:

      $EmailFrom = “myaccount@domain.com”

      $EmailTo = “emailaddress@domain.com”

      $Subject = “Test from O365 SMTP”

      $Body = “Test from O365 SMTP Authentication”

      $SMTPServer = “smtp.office365.com”

      $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)

      $SMTPClient.EnableSsl = $true

      $SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“myaccount@domain.com”, “*****”);

      $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

Leave a Reply

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