将正文转换为HTML不起作用

时间:2015-06-22 09:30:47

标签: html powershell html-email

我正在尝试将我的文本正文转换为HTML,因此它不会在我的电子邮件中显示为纯文本。

这是代码

$From = ""
$To = ""
$SMTPServer = ""
$SMTPPort = "587"
$Username = ""
$Password = ""
$subject = "Test Powershell"
$body = $htmlreport
$bodyAsHtml = $true
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.Send($From, $To, $subject, $body);

我无法将报告以HTML格式发送。

2 个答案:

答案 0 :(得分:1)

$bodyAs包含`enter code here`。您应删除。你也不要把它放在任何地方。

也许尝试使用Send-MailMessage cmdlet:

Send-MailMessage -SmtpServer $SMTPServer -To $To -From $From -Subject $subject -Body $body -BodyAsHtml

答案 1 :(得分:1)

不应将单个字符串传递给$smtp.Send(),而应创建一个MailMessage对象并将其发送:

$msg            = New-Object System.Net.Mail.MailMessage
$msg.From       = $From
$msg.To         = $To
$msg.Subject    = $subject
$msg.Body       = $body
$msg.IsBodyHtml = $true # this is where the magic happens

$smtp             = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
$smtp.EnableSSL   = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)

$smtp.Send($msg) # and then send the message we just composed above