powershell:使用 get-content 作为 smtp 邮件的正文

发布于 2024-12-06 14:42:03 字数 784 浏览 1 评论 0原文

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

Outlook 客户端不会删除换行符。

我的代码如下:

$smtpserver = "smtpserver"
$from="[email protected]"
$to="[email protected]"
$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)

我什至尝试使用 get-content 和 -encoding ASCII 以及其他一些方法,但没有帮助。 有人可以帮忙吗?

-

谢谢

I am using powershell to send an SMTP mail. The body of the email is from a file.
The problem is, when I receive this email, it removes all spaces and linefeeds so it looks ugly.

Outlook client is no removing linebreaks.

My code is as follows:

$smtpserver = "smtpserver"
$from="[email protected]"
$to="[email protected]"
$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)

I have even tried to use get-content with -encoding ASCII and couple of others but no help.
Can anyone please help?

-

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魔法少女 2024-12-13 14:42:04

找到答案:

读取文件时使用out-string in。 IE

$body= (Get-Content $OutputFile | out-string )

Found the answer:

use out-string in when reading the file. i.e.

$body= (Get-Content $OutputFile | out-string )
素染倾城色 2024-12-13 14:42:04

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

$body= (Get-Content $OutputFile) -join '<BR>'

Add HTML line break tag at each end of a line:

$body= (Get-Content $OutputFile) -join '<BR>'
童话里做英雄 2024-12-13 14:42:04

对我有用的是将内容转换为 HTML,如下所示:

$html = $result | ConvertTo-Html | Out-String
Send-MailMessage -Credential $mycreds -From "" -To "" -BodyAsHtml $html -Encoding ASCII -Subject "Test" -SmtpServer '' -Port 25

What worked for me was converting the content to HTML as the following:

$html = $result | ConvertTo-Html | Out-String
Send-MailMessage -Credential $mycreds -From "" -To "" -BodyAsHtml $html -Encoding ASCII -Subject "Test" -SmtpServer '' -Port 25
盗琴音 2024-12-13 14:42:04

如果您使用的是 PowerShell v2.0,请尝试 Send-MailMessage:http://technet。 microsoft.com/en-us/library/dd347693.aspx

此处的默认编码为 ASCII。因此,为了能够保留编码,您可能需要尝试找到文件的编码,然后设置相关的编码。

要查找文件的编码,请使用此处的脚本。
http://poshcode.org/2059

现在,将上述函数与 Send-MailMessage 结合起来。例如,

send-mailmessage -from "User01 <[email protected]>" -to "User02 <[email protected]>", "User03 <[email protected]>" -subject "Sending the Attachment" -body (Get-Content C:\somefile.txt) -dno onSuccess, onFailure -smtpServer smtp.fabrikam.com -encoding (Get-FileEncoding C:\somefile.txt)

请记住,Get-FileCoding 不是内置 cmdlet。您需要从 poshcode 链接复制它。

If you are using PowerShell v2.0 try Send-MailMessage: http://technet.microsoft.com/en-us/library/dd347693.aspx

The default encoding here will be ASCII. So, to be able to retain the encoding, you may want to try and find the encoding of the file and then set the relevant encoding.

To find encoding of a file, use the script here.
http://poshcode.org/2059

Now, combine the above function with Send-MailMessage. For example,

send-mailmessage -from "User01 <[email protected]>" -to "User02 <[email protected]>", "User03 <[email protected]>" -subject "Sending the Attachment" -body (Get-Content C:\somefile.txt) -dno onSuccess, onFailure -smtpServer smtp.fabrikam.com -encoding (Get-FileEncoding C:\somefile.txt)

Remember, Get-FileCoding is not a builtin cmdlet. You need to copy it from the poshcode link.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文