显示编码的电子邮件主题

2024-03-15

我正在使用发送邮件消息向我们的支持系统发送电子邮件。 但是当它发送电子邮件时,它会显示主题行,如下屏幕所示!

=?us-ascii?Q?R899076:Aman:系统摘要 ?=

在主题中我使用变量:

$vUserName = (Get-Item env:\username).Value
$vComputerName = (Get-Item env:\Computername).Value
$subject = "$vComputerName : $vUserName : System Summary"

and then

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"变量,结果是一样的。

是的,我尝试过 - 编码选项,它给出了一个错误

Send-MailMessage:无法绑定参数“编码”。无法转换类型“Syste”的“utf8”值 m.String”来输入“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 -编码

有什么线索吗?


您可以编写自定义函数以使用 .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()  
} 

}

(感谢托马斯·李([电子邮件受保护] /cdn-cgi/l/email-protection) - 我从他的代码中调整了这个http://powershell.com/cs/media/p/357.aspx http://powershell.com/cs/media/p/357.aspx)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

显示编码的电子邮件主题 的相关文章

随机推荐