powershell:使用get-content作为smtp邮件的主体

Man*_*jot 2 powershell smtp

我使用PowerShell发送SMTP邮件.电子邮件的正文来自文件.问题是,当我收到这封电子邮件时,它会删除所有空格和换行符,因此它看起来很难看.

Outlook客户端无法删除换行符.

我的代码如下:

$smtpserver = "smtpserver"
$from="email1@domain.com"
$to="email2@domain.com"
$subject="something"
$body= (Get-Content $OutputFile )
$mailer = new-object Net.Mail.SMTPclient($smtpserver)
$msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body)
$msg.IsBodyHTML = $true
$mailer.send($msg)
Run Code Online (Sandbox Code Playgroud)

我甚至试图使用带有-encoding ASCII的get-content和其他几个但没有帮助.有人可以帮忙吗?

-

谢谢

Man*_*jot 11

找到答案:

在读取文件时使用out-string.即

$body= (Get-Content $OutputFile | out-string )
Run Code Online (Sandbox Code Playgroud)


Sha*_*evy 5

在每一行的末尾添加 HTML 换行标记:

$body= (Get-Content $OutputFile) -join '<BR>'
Run Code Online (Sandbox Code Playgroud)