In the last article in this series I showed you how to add message body content to emails sent from PowerShell scripts.

In this article we’ll take it a step further by adding a HTML message body to an email.

For this to work we’ll be using an System.Net.Mail.MailMessage .NET object. This is basically a message object that can be sent via the SmtpClient .NET object that we used in the previous articles. The benefit of using this object in a script is that it can be used to create HTML email messages.

To create the script we can start with similar variables to last time.

$smtpServer = ""
$smtpFrom = ""
$smtpTo = ""
$messageSubject = "List of Exchange Servers"

Next we create the new MailMessage object using the variables above as the To and From addresses, and then set the message subject as well.

$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject

Now comes the important bit, setting the message body to be HTML.

$message.IsBodyHTML = $true

Now all we need is some HTML content to go into the message body. As an example let’s just get a list of Exchange servers and their server roles.

$message.Body = Get-ExchangeServer | Select-Object Name,ServerRole | ConvertTo-Html

All I needed to do above is run Get-ExchangeServer, pipe the output into a Select-Object command to grab just the server name and roles, and then pipe that into the ConvertTo-Html command.

The final step is to create the SmtpClient object and send the mail message.

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)

The result is an email that looks similar to this.

How to Send SMTP Email Using PowerShell (Part 3)

Here is the complete example script.

$smtpServer = ""
$smtpFrom = ""
$smtpTo = ""
$messageSubject = "List of Exchange Servers"

$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true

$message.Body = Get-ExchangeServer | Select-Object Name,ServerRole | ConvertTo-Html

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)

As you can see this is a pretty simple technique that can be used to easily create basic HTML email messages.

About the Author

Paul Cunningham

Paul is a former Microsoft MVP for Office Apps and Services. He works as a consultant, writer, and trainer specializing in Office 365 and Exchange Server. Paul no longer writes for


  1. frank tobias

    Hello, I have one question:
    How to i embed an HTML file into the body so recipient can view it when they open the email?
    We are using Outlook 360.


  2. Roger

    Hi Paul, great article as always. Been following you for years and when I see it’s something of yours I know it’s quality work. Thank you!

  3. venky

    Hi, I want to send email with jpeg file inserted in the body of the email and everyday i schedule in task manager to send email with different jpeg file daily.
    How to organize the jpeg file that everyday one image is sent.

  4. Steanrdr

    Hi all, the solution for a mail with multiple tables and maybe some text is, to use the Parameter: ConvertTo-Html -fragment . Than setup the final html concatenating the blocks:

    Edit: Removed. Please don’t post large blocks of code directly into comments, it breaks the layout of the site.

    1. Steanrdr

      Hi all, Unfortunately I am not able to post the script here complete. Sorry.

  5. Sean F

    Is there a way to cycle through a text or csv file to grab one recipient per email? I want to send to individuals in my address book, but don’t want to show all their email addresses to each other or mess with BCC.

    1. Avatar photo
      Paul Cunningham

      Yes, pretty straight forward. Get-Content can ingest the txt file for you, and a for loop ( or foreach loop) can iterate through it. Do a little reading on PowerShell and loop processing.

  6. Joelle YI

    thanks for the article, very helpful and complete for a full working script

  7. John Thayer Jensen

    One thing about the ConvertTo-HTML cmdlet is that it allows you to point to a CSS URI to add configuration to your e-mail. I want to centre the table that ConvertTo-HTML produces and put borders around it. I wonder if anyone can point me to a CSS URI that can do that for me.


    1. Avatar photo
      Paul Cunningham

      Are you asking if someone has already written the CSS for this and has it hosted on a URL somewhere you can access?

      CSS is pretty simple to learn. Check out or just do Google searches like “how to center a table with css” and you’ll find lots of sample code to work with.

  8. Proxicon

    @George – Fix it yourself mate go do some research and learn how to program instead of waiting for other to write code for you to use… Its called being lazy.

    Anyway what you seek is an arry so just do this if multiple.
    [string[]]$smtpTo = ‘’,’’,’’

    1. Chad

      While I agree that looking up other’s code is sort of lazy, it is what we all do. Why post anything if you are going to post it wrong?

  9. George

    If we put more that one email ID in the $smtpTo, then the code sends email to only the last email ID.
    Please fix it by adding a CC or BCC code

  10. Frankie Tao

    Thank You!!! This helps me out greatly!

  11. Kevin Russell

    Your code is wrong.

      1. Chad

        I tried it verbatim as you showed above and I got:

        Exception calling “Send” with “1” argument(s): “Failure sending mail.”

        I can send using send-emailmessage with no issue…

        1. Avatar photo
          Paul Cunningham

          It’s a code sample, you need to adjust it for your environment.

          If I try it verbatim from my PC, then I also get that error. Because the $smtpServer variable points to an Exchange server that I can’t reach from this PC (it’s an old lab server).

          If I change the variables to values that are valid in my environment, it works fine.

        2. Sanjeev

          Send it from Hub Transport Server

  12. Eric Miller

    I found I had to do this to get it to work:
    $style = “”
    $style = $style + “BODY{font-family: Arial; font-size: 10pt;}”
    $style = $style + “TABLE{border: 1px solid black; border-collapse: collapse;}”
    $style = $style + “TH{border: 1px solid black; background: #dddddd; padding: 5px; }”
    $style = $style + “TD{border: 1px solid black; padding: 5px; }”
    $style = $style + “”

  13. Dilip Vyas

    Send Mail with fileoutput attachments. File has been saved with default path with random name.

    Please help us

Leave a Reply