【问题标题】:Email Subject showing Encoding显示编码的电子邮件主题
【发布时间】:2011-11-20 22:09:08
【问题描述】:

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

=?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:无法绑定参数“编码”。无法转换类型为“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 -Encoding

有什么线索吗?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您可以编写自定义函数以使用 .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 的代码中对此进行了调整)

    【讨论】:

      【解决方案2】:

      发送邮件后,Send-MailMessage cmdlet 没有任何输出,不知道输出是从哪里来的?你能包括你使用的命令和输出吗?

      至于您的主题行,您可以将其缩减为仅一行:

      $subject = "$env:ComputerName : $env:UserName : System Summary"
      

      Send-MailMessage有一个Encoding参数,你试过了吗?

      【讨论】:

      • 嗨,Shay,我尝试对 -encoding 参数进行编码,但给出了错误。请参阅下面的错误。我只是注意到,如果我在主题中加入简单的文字,它会很好。但是后来我在 -Subject 中使用了变量,然后这个问题就来了。
      • @Aman,你不能使用字符串 (utf8) 作为值,试试这个:-Encoding ([System.Text.Encoding]::UTF8) ...
      • 感谢SHAY的回复{抱歉回复晚了},我使用了自定义的SENDMAil功能,效果很好。
      【解决方案3】:

      当我在主题中有空格时使用带有默认编码 (ASCII) 的 Send-MailMessage 时发生了这种情况。

      首先,回答有关编码参数的问题:您尝试将其作为字符串传递(即“-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 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]::UTF8)
      

      但正如您所说,它在 Outlook 中看起来不错,所以我认为主题是正确的。我不是电子邮件格式或文本编码方面的专家,所以我不会推测原因。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-23
        • 2014-05-24
        • 1970-01-01
        • 2011-06-21
        • 2017-05-16
        • 2013-02-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多