[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1047
  • Last Modified:

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>
0
Elad-a
Asked:
Elad-a
  • 4
  • 4
1 Solution
 
RammesteinCommented:
I hve tried a normal as well an email with attachment  using powershell
both use plain text
0
 
Chris DentPowerShell DeveloperCommented:

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
0
 
Elad-aAuthor Commented:
Wow chris your the best!
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
Chris DentPowerShell DeveloperCommented:

Yeah, this works for me, hopefully it will for you to. Let me know if anything here isn't clear :)

Chris
$HTMLBody = Get-Content "test.html"
$SMTPServer = "yourmailserver"
$From = "someone@somewhere.com"
$To = "your@yourdomain.com"
 
$MailMessage = New-Object System.Net.Mail.MailMessage($From, $To)
$MailMessage.IsBodyHtml = $True
$MailMessage.Body = $HTMLBody
 
(New-Object System.Net.Mail.SmtpClient($SMTPServer)).Send($MailMessage)

Open in new window

0
 
Elad-aAuthor Commented:
Thanks Chris, ill try it out and let you know..
Thanks,
Jason.
0
 
Elad-aAuthor Commented:
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.
0
 
Chris DentPowerShell DeveloperCommented:

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
0
 
Chris DentPowerShell DeveloperCommented:

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
0
 
Elad-aAuthor Commented:
Chris Dent is the Man!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now