In the last article in this series I showed you how to add message body content to emails sent from PowerShell 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
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 = "ho-ex2010-caht1.exchangeserverpro.net" $smtpFrom = "reports@exchangeserverpro.net" $smtpTo = "administrator@exchangeserverpro.net" $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) $smtp.Send($message)
The result is an email that looks similar to this.
Here is the complete example script.
$smtpServer = "ho-ex2010-caht1.exchangeserverpro.net" $smtpFrom = "reports@exchangeserverpro.net" $smtpTo = "administrator@exchangeserverpro.net" $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) $smtp.Send($message)
As you can see this is a pretty simple technique that can be used to easily create basic HTML email messages.
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.
Thanks
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!
Pingback: download
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.
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.
Hi all, Unfortunately I am not able to post the script here complete. Sorry.
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.
The Real Person!
Author Paul Cunningham acts as a real person and passed all tests against spambots. Anti-Spam by CleanTalk.
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.
thanks for the article, very helpful and complete for a full working script
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.
jj
The Real Person!
Author Paul Cunningham acts as a real person and passed all tests against spambots. Anti-Spam by CleanTalk.
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 W3Schools.com or just do Google searches like “how to center a table with css” and you’ll find lots of sample code to work with.
@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 = ‘Email1@.net’,’Email2@.net’,’Email3@.net’
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?
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
Thank You!!! This helps me out greatly!
Your code is wrong.
The Real Person!
Author Paul Cunningham acts as a real person and passed all tests against spambots. Anti-Spam by CleanTalk.
Which code?
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…
The Real Person!
Author Paul Cunningham acts as a real person and passed all tests against spambots. Anti-Spam by CleanTalk.
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.
Send it from Hub Transport Server
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 + “”
Pingback: Create an Exchange Mail Flow Latency Heat Map with PowerShell
Send Mail with fileoutput attachments. File has been saved with default path with random name.
Please help us
Pingback: How to Automate Exchange 2010 Database Backup Alert Emails
Pingback: PowerShell: How to Create Formatted HTML Output from Scripts