【问题标题】:How can I send an email from powershell to an email address containing an Accent?如何从 powershell 将电子邮件发送到包含口音的电子邮件地址?
【发布时间】:2015-04-18 07:41:59
【问题描述】:

我使用下面的代码 sn-p 从 powershell 发送 HTML 格式的电子邮件。一段时间以来,这一直没有问题,直到将一组外国用户添加到我们的环境中,其中一些新用户的电子邮件地址包含带有重音符号(á、é、í、ó、ú、ü、ñ)的字符。如何处理带有这些特殊字符的地址?

我在发送到包含 ú 的电子邮件地址时收到以下错误:

Send-HTMLFormattedEmail -to 'thisütest@domain.com' -From sender@domain.com -Subject "testemail" -XSLPath "$scriptDir\emailbodyGen.xsl"`
-Days "1" -Company 'testcompany' -Relay email.domain.com -ToDisName "Tim" -FromDisName "sender" -LogoPath "$scriptDir\logo.gif"

错误:

使用“1”参数调用“发送”的异常:“客户端或服务器仅配置为使用 ASCII 的电子邮件地址 本地部分:thisütest@domain.com。” 在 C:\test\Untitled1.ps1:150 char:9 + $Client.Send($Message) + ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SmtpException

代码:

function Send-HTMLFormattedEmail {
    <# 
    .Synopsis
        Used to send an HTML Formatted Email.
    .Description
        Used to send an HTML Formatted Email that is based on an XSLT template.
    .Parameter To
        Email address or addresses for whom the message is being sent to.
        Addresses should be seperated using ;.
    .Parameter ToDisName
        Display name for whom the message is being sent to.
    .Parameter CC
        Email address if you want CC a recipient.
        Addresses should be seperated using ;.
    .Parameter BCC
        Email address if you want BCC a recipient.
        Addresses should be seperated using ;.
    .Parameter From
        Email address for whom the message comes from.
    .Parameter FromDisName
        Display name for whom the message comes from.
    .Parameter Subject
        The subject of the email address.
    .Parameter Days
        The number of days in which the passowrd will expire.
    .Parameter Company
        The Company name for this notification.
    .Parameter Relay
        FQDN or IP of the SMTP relay to send the message to.
    .Parameter XSLPath
        The full path to the XSL template that is to be used.
    .Parameter LogoPath
        The full path to the jpg image for the signature.
    .NOTES
        Taken from: http://community.spiceworks.com/scripts/show/1037-send-html-emails-via-powershell
    #>
    param(
        [Parameter(Mandatory=$True)][String]$To,
        [Parameter(Mandatory=$True)][String]$ToDisName,
        [String]$CC,
        [String]$BCC,
        [Parameter(Mandatory=$True)][String]$From,
        [Parameter(Mandatory=$True)][String]$FromDisName,
        [Parameter(Mandatory=$True)][String]$Subject,
        [Parameter(Mandatory=$True)][String]$Days,
        [Parameter(Mandatory=$True)][String]$Company,
        [Parameter(Mandatory=$True)][String]$Relay,
        [Parameter(Mandatory=$True)][String]$XSLPath,
        [Parameter(Mandatory=$True)][String]$LogoPath
        )

    try 
    {

        $Message = New-Object System.Net.Mail.MailMessage

        # add the attachment, and set it to inline.
        $Attachment = New-Object Net.Mail.Attachment($LogoPath)
        $Attachment.ContentDisposition.Inline = $True
        $Attachment.ContentDisposition.DispositionType = "Inline"
        $Attachment.ContentType.MediaType = "image/gif"
        $Logo = "cid:logo" 

        # Load XSL Argument List
        $XSLArg = New-Object System.Xml.Xsl.XsltArgumentList
        $XSLArg.Clear() 
        $XSLArg.AddParam("To", $Null, $ToDisName)
        $XSLArg.AddParam("Days", $Null, $Days)
        $XSLArg.AddParam("Logo", $Null, $Logo)
        $XSLArg.AddParam("Company", $Null, $Company)

        # Load Documents
        $BaseXMLDoc = New-Object System.Xml.XmlDocument
        $BaseXMLDoc.LoadXml("<root/>")

        $XSLTrans = New-Object System.Xml.Xsl.XslCompiledTransform
        $XSLTrans.Load($XSLPath)

        #Perform XSL Transform
        $FinalXMLDoc = New-Object System.Xml.XmlDocument
        $MemStream = New-Object System.IO.MemoryStream

        $XMLWriter = [System.Xml.XmlWriter]::Create($MemStream)
        $XSLTrans.Transform($BaseXMLDoc, $XSLArg, $XMLWriter)

        $XMLWriter.Flush()
        $MemStream.Position = 0

        # Load the results
        $FinalXMLDoc.Load($MemStream) 
        $Body = $FinalXMLDoc.Get_OuterXML()

        # Populate the Message.
        $html = [System.Net.Mail.AlternateView]::CreateAlternateViewFromString($body, $null, "text/html")
        $imageToSend = new-object system.net.mail.linkedresource($LogoPath,"image/jpg")
        $imageToSend.ContentID = "logo"
        $html.LinkedResources.Add($imageToSend)
        $message.AlternateViews.Add($html)
        $Message.Subject = $Subject
        $Message.IsBodyHTML = $True
        $message.Priority = 'High'

        # Add From
        $MessFrom = New-Object System.Net.Mail.MailAddress $From, $FromDisName
        $Message.From = $MessFrom

        # Add To
        $To = $To.Split(";") # Make an array of addresses.
        $To | foreach {$Message.To.Add((New-Object System.Net.Mail.Mailaddress $_.Trim()))} # Add them to the message object.

        # Add CC
        if ($CC){
            $CC = $CC.Split(";") # Make an array of addresses.
            $CC | foreach {$Message.CC.Add((New-Object System.Net.Mail.Mailaddress $_.Trim()))} # Add them to the message object.
            }

        # Add BCC
        if ($BCC){
            $BCC = $BCC.Split(";") # Make an array of addresses.
            $BCC | foreach {$Message.BCC.Add((New-Object System.Net.Mail.Mailaddress $_.Trim()))} # Add them to the message object.
            }

        # Create SMTP Client
        $Client = New-Object System.Net.Mail.SmtpClient $Relay

        # Send The Message
        $Client.Send($Message)
    }  
    catch 
    {
        throw $_
    }   
        $attachment.Dispose() #dispose or it'll lock the file
}

function Resolve-Error ($ErrorRecord=$Error[0])
{
   $ErrorRecord | Format-List * -Force
   $ErrorRecord.InvocationInfo |Format-List *
   $Exception = $ErrorRecord.Exception
   for ($i = 0; $Exception; $i++, ($Exception = $Exception.InnerException))
   {   "$i" * 80
       $Exception |Format-List * -Force
   }
}

【问题讨论】:

标签: .net email powershell encoding character-encoding


【解决方案1】:

MailAddress 有一个允许您指定编码的 ctor,请参见此处:https://msdn.microsoft.com/en-us/library/f52hswkf%28v=vs.110%29.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多