Link to home
Start Free TrialLog in
Avatar of Elad-a
Elad-a

asked on

Use a .HTM file as a body of an email sent via PowerShell

Hi,
I use powershell to generate an HTM report that i want to send over to my users via Powershell.
Thing is that I dont want to send it as an attachment but as the body of the mail I'm sending to my users.
There are lots of PS1 scripts out there to send mail via powershell but I cant get any of them to send an HTM formatted mail with the HTM file as the body.
Instead i just get an email with the below content:
Can you help?


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"  "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <style>BODY{background-color:peachpuff;}TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:palegoldenrod}TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:palegoldenrod}</style> </head><body> <H2>Mail ITem Count Harts</H2> <table> <colgroup> <col> <col> <col> <col> <col> <col> </colgroup> <tr><th>From: 04/24/2009 10:09:17</th><th>To:04/25/2009 10:09:17 </th><th>there were</th><th>4</th><th>E-Mail's To</th><th>mail accountname </th></tr> </table></body></html>
Avatar of Rammestein
Rammestein
Flag of India image

I hve tried a normal as well an email with attachment  using powershell
both use plain text
Avatar of Chris Dent

There's flag for this in the MailMessage Class (IsBodyHTML property), going to see if it works and I'll pop in a sample.

Chris
Avatar of Elad-a
Elad-a

ASKER

Wow chris your the best!
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Elad-a

ASKER

Thanks Chris, ill try it out and let you know..
Thanks,
Jason.
Avatar of Elad-a

ASKER

chris,
To add a subject line i'd just modify :
$MailMessage = New-Object System.Net.Mail.MailMessage($From, $To)
to be
$MailMessage = New-Object System.Net.Mail.MailMessage($From, $To,$subject)
correct?
Thanks
Jason.

Not according to the documentation :) The constructor for MailMessage is limited to To / From details.

Instead try:

$MailMessage = New-Object System.Net.Mail.MailMessage($From, $To)
$MailMessage.Subject = "Your Subject"
$MailMessage.IsBodyHtml = $True
$MailMessage.Body = $HTMLBody

Chris

Oops I take it back, and you're right. I missed the last constructor and it should work with your version as well.

In fact it should work with:

$MailMessage = New-Object System.Net.Mail.MailMessage($From, $To, $Subject, $Body)

So we only have to set IsBodyHtml outside of the constructor.

Chris
Avatar of Elad-a

ASKER

Chris Dent is the Man!