Powershell中的HTML格式电子邮件

时间:2019-02-21 03:41:48

标签: powershell

Powershell生成的电子邮件中的HTML正文未格式化。

cat "volumes.txt"

Checking Array  plutus  for volumes
--------------------+----------+------+-----------+----------+---------+---- 
---
Name                 Size       Online Offline     Usage      Reserve % 
Quota %
                 (MiB)              Reason      (MiB)
--------------------+----------+------+-----------+----------+---------+---- 
---
puloto-001        2048000 Yes    N/A            434035         0     100
puloto-002        2048000 Yes    N/A            380142         0     100
puloto-003        2048000 Yes    N/A            312061         0     100
puloto-004        2048000 Yes    N/A            292216         0     100

但是然后以这种方式在Powershell中以html格式的电子邮件发送内容

检查阵列mhstpmanb200的卷检查阵列plutus的卷-------------------- + ---------- + ----- -+ ----------- + ---------- + --------- + -------名称大小在线离线使用保留%配额%(MiB)原因(MiB)-------------------- + ---------- + ------ + --- -------- + ---------- + --------- + ------- puloto-001 2048000是不适用434035 0100 puloto- 002 2048000是N / A 380142 0 100 puloto-003 2048000是N / A 312061 0100 puloto-004 2048000是N / A 292216 0100 puloto-005 2048000是N / A 245774 0100 puloto-006 2048000是N / A 251434 0 100 puloto-007 2048000是N / A 241778 0100 puloto-008 2048000是N / A 202305 0100

这是我的代码

     $FILE = "volumes.txt";
     $message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
     $message.Subject = $messageSubject
     $message.IsBodyHTML = $true

     $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 + "</style>"
     $message.Body = "<head><pre>$style</pre></head>"
     $message.Body = "$(cat $FILE)"
     $smtp = New-Object Net.Mail.SmtpClient($smtpServer)`enter code here`
     $smtp.Send($message)

我在这里做什么错了?

谢谢

1 个答案:

答案 0 :(得分:0)

您是否要在邮件中邮寄“ volumes.txt”数据?那么这就是解决方案。

Get-Content ".\volumes.txt"| ConvertTo-Html |Out-File volumes.html
$message.body = Get-Content ".\volumes.html"