powershell:使用 get-content 作为 smtp 邮件的正文
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
找到答案:
读取文件时使用out-string in。 IE
Found the answer:
use out-string in when reading the file. i.e.
在行的每一端添加 HTML 换行标记:
Add HTML line break tag at each end of a line:
对我有用的是将内容转换为 HTML,如下所示:
What worked for me was converting the content to HTML as the following:
如果您使用的是 PowerShell v2.0,请尝试 Send-MailMessage:http://technet。 microsoft.com/en-us/library/dd347693.aspx
此处的默认编码为 ASCII。因此,为了能够保留编码,您可能需要尝试找到文件的编码,然后设置相关的编码。
要查找文件的编码,请使用此处的脚本。
http://poshcode.org/2059
现在,将上述函数与 Send-MailMessage 结合起来。例如,
请记住,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,
Remember, Get-FileCoding is not a builtin cmdlet. You need to copy it from the poshcode link.