2015-06-22 62 views
0

我想讓我的文本正文轉換爲HTML,因此它不顯示爲我的電子郵件中的純文本。正文轉換爲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。

+0

沒有這裏有足夠的信息,你沒有包含你想要轉換的文本,你沒有顯示'$ htmlreport'包含的內容。另外,創建一個名爲'$ bodyAsHtml'的變量並將其設置爲true將毫無用處。 – arco444

回答

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 
1

$bodyAs包含`enter code here`。你應該刪除那。你也不要把它放在任何地方。

也許嘗試Send-MailMessage小命令來代替:

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

\'在此處輸入代碼\'可能是由於按下了 + 或單擊編輯文本框中的「{}」圖標而被錯誤地插入。 –