如何从powershell发送电子邮件到包含Accent的电子邮件地址?

时间:2015-02-17 15:02:11

标签: .net email powershell encoding character-encoding

我使用以下代码段从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”参数调用“Send”的异常:“客户端或服务器仅配置为使用ASCII的电子邮件地址 local-parts: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
   }
}

1 个答案:

答案 0 :(得分:0)

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