gpt4 book ai didi

powershell - 显示编码的电子邮件主题

转载 作者:行者123 更新时间:2023-12-03 16:48:54 25 4
gpt4 key购买 nike

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

=?us-ascii?Q?R899076:Aman:System Summary ?=

在主题中我使用了变量:

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

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

Send-MailMessage : Cannot bind parameter 'Encoding'. Cannot convert the "utf8" value of type "Syste m.String" to type "System.Text.Encoding". At D:\PowerShell\MyScripts\SystemInfo\SysInfo-V6-test[notfinal].ps1:151 char:84 + send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Encoding <<<< utf8 -B ody $body -Attachments "$filepath\$name.html" -BodyAsHtml -Priority High + CategoryInfo : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.SendMai lMessage

有什么线索吗?

最佳答案

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

}

(感谢 Thomas Lee (tfl@psp.co.uk) - 我根据他在 http://powershell.com/cs/media/p/357.aspx 的代码对此进行了调整)

关于powershell - 显示编码的电子邮件主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7482851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com