Powershell 显示编码的电子邮件主题
我正在使用send mail message向我们的支持系统发送电子邮件。 但是当它发送电子邮件时,它会显示主题行,就像这个屏幕一样 =?美国ascii?Q?R899076:阿曼:系统摘要= 在主题中,我使用变量:Powershell 显示编码的电子邮件主题,powershell,Powershell,我正在使用send mail message向我们的支持系统发送电子邮件。 但是当它发送电子邮件时,它会显示主题行,就像这个屏幕一样 =?美国ascii?Q?R899076:阿曼:系统摘要= 在主题中,我使用变量: $vUserName = (Get-Item env:\username).Value $vComputerName = (Get-Item env:\Computername).Value $subject = "$vComputerName : $vUserName : Syst
$vUserName = (Get-Item env:\username).Value
$vComputerName = (Get-Item env:\Computername).Value
$subject = "$vComputerName : $vUserName : System Summary"
然后
send-MailMessage -SmtpServer Smtp-local -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Priority High
但是当我在Outlook中收到这封电子邮件时,它看起来很好,你知道吗
实际上,这是一个大约150行的脚本,服务器中已经指定了电子邮件和smtp服务器的主体。 是的,我尝试了
$subject=“$env:ComputerName:$env:UserName:System Summary”
变量,结果相同
是的,我尝试了-encoding选项,它给出了一个错误
发送邮件消息:无法绑定参数“Encoding”。无法转换“Syste”类型的“utf8”值
m、 字符串“to type”System.Text.Encoding。
位于D:\PowerShell\MyScripts\SystemInfo\SysInfo-V6-test[notfinal].ps1:151 char:84
+send-MailMessage-SmtpServer$smtp-To$To-From$From-Subject$Subject-Encoding发送电子邮件后,send-MailMessage cmdlet没有任何输出,我想知道输出来自何处?您可以包括您使用的命令和输出吗 至于你的主题行,你可以把它减少到一行:
$subject = "$env:ComputerName : $env:UserName : System Summary"
Send MailMessage有一个编码参数,您试过了吗?您可以使用.net编写一个自定义函数来发送,而不是使用Send MailMessage cmdlet。它要笨重得多,但解决了这个问题 大概是这样的:
Function SendEmail ($emailFrom, $emailTo, $subject, $body, $attachment) {
# Create from/to addresses
$from = New-Object System.Net.Mail.MailAddress $emailFrom
$to = New-Object System.Net.Mail.MailAddress $emailTo
# Create Message
$message = new-object System.Net.Mail.MailMessage $from, $to
$message.Subject = $subject
$message.Body = $body
$attachment = new-object System.Net.Mail.Attachment($attachment)
$message.Attachments.Add($attachment)
# Set SMTP Server and create SMTP Client
$server = <your server>
$client = new-object system.net.mail.smtpclient $server
# Send the message
"Sending an e-mail message to {0} by using SMTP host {1} port {2}." -f $to.ToString(), $client.Host, $client.Port
try {
$client.Send($message)
"Message to: {1}, from: {0} has beens successfully sent" -f $from, $to
}
catch {
"Exception caught in CreateTestMessage: {0}" -f $Error.ToString()
}
}
函数sendmail($emailFrom、$emailTo、$subject、$body、$attachment){
#创建发件人/收件人地址
$from=新对象System.Net.Mail.MailAddress$emailFrom
$to=新对象System.Net.Mail.MailAddress$emailTo
#创建消息
$message=新对象System.Net.Mail.MailMessage$from,$to
$message.Subject=$Subject
$message.Body=$Body
$attachment=新对象System.Net.Mail.attachment($attachment)
$message.Attachments.Add($attachment)
#设置SMTP服务器并创建SMTP客户端
$server=
$client=new object system.net.mail.smtpclient$server
#发送消息
“使用SMTP主机{1}端口{2}向{0}发送电子邮件。”-f$to.ToString(),$client.host,$client.port
试试{
$client.Send($message)
“已成功将消息发送到:{1},发件人:{0}”-f$from$to
}
捕获{
CreateTestMessage中捕获异常:{0}”-f$Error.ToString()中
}
}
(感谢托马斯·李(tfl@psp.co.uk)-我从他的代码中调整了这一点。当主题中有空格时,我使用默认编码(ASCII)发送邮件消息时,这种情况发生在我身上 首先,回答关于编码参数的问题:您试图将其作为字符串传递(即“-Encoding ASCII”),而应该使用这种语法:“-Encoding([System.Text.Encoding]::ASCII)” 这个“主题中的编码”问题发生在我身上,我把它缩小到主题中的空格。证明: 这将不包含主题中的编码:
Send-MailMessage -To "alice@example.com" -From "bob@example.com" -Smtp "localhost" -Subject "one" -BodyAsHtml "body1" -Encoding ([System.Text.Encoding]::ASCII)
Send-MailMessage -To "alice@example.com" -From "bob@example.com" -Smtp "localhost" -Subject "one" -BodyAsHtml "body1" -Encoding ([System.Text.Encoding]::UTF8)
但这将:
Send-MailMessage -To "alice@example.com" -From "bob@example.com" -Smtp "localhost" -Subject "one two" -BodyAsHtml "body1" -Encoding ([System.Text.Encoding]::ASCII)
请注意,唯一重要的区别是主体中的空间
如果我指定UTF8作为编码,则主题中没有编码:
Send-MailMessage -To "alice@example.com" -From "bob@example.com" -Smtp "localhost" -Subject "one" -BodyAsHtml "body1" -Encoding ([System.Text.Encoding]::ASCII)
Send-MailMessage -To "alice@example.com" -From "bob@example.com" -Smtp "localhost" -Subject "one" -BodyAsHtml "body1" -Encoding ([System.Text.Encoding]::UTF8)
但正如你所说,它的前景看起来不错,所以我认为这个主题是正确的。我不是电子邮件格式或文本编码方面的专家,因此我不会猜测原因。嗨,Shay,我尝试了-encoding参数的编码,但参数给出了错误。请参阅下面的错误信息。我只是注意到,若我把简单的文本放在主题中,它会很好。但是我在-Subject中使用了变量,然后出现了这个问题。@Aman,您不能使用字符串(utf8)作为值,请尝试以下操作:-编码([System.Text.Encoding]::utf8)…谢谢您的回复SHAY{抱歉,回复太晚了},我使用了自定义SENDMAil功能,这很有效。