SmtpException: The client or server is only configured for e-mail addresses with ASCII local-parts

人盡茶涼 提交于 2019-11-30 01:53:34

问题


The SmtpClient.Send() method is throwing this exception when I try to send an email to an address containing an accentuated character (é):

System.Net.Mail.SmtpException: The client or server is only configured for e-mail addresses with ASCII local-parts: léo.xxx@example.com.
at System.Net.Mail.MailAddress.GetAddress(Boolean allowUnicode)
at System.Net.Mail.SmtpClient.ValidateUnicodeRequirement(MailMessage...)
at System.Net.Mail.SmtpClient.Send(MailMessage message)

The formulation of the message makes me thing there might be a setting that I can activate to make this work, though I haven't found anything on this subject.

I have tried several SMTP servers, including Gmail. Here are the relevant bits for a repro:

Code

var msg = new MailMessage();
msg.Subject = "Test";
msg.From = new MailAddress("xxx@gmail.com");
msg.To.Add(new MailAddress("léo.yyy@gmail.com"));

new SmtpClient().Send(msg);

app.config

<system.net>
    <mailSettings>
        <smtp from="xxx@gmail.com">
            <network host="smtp.gmail.com" port="587" userName="xxx@gmail.com" password="password" enableSsl="true" />
        </smtp>
    </mailSettings>
</system.net>

回答1:


.net only supports ASCII characters. I don't believe it supports extended ASCII characters (which includes the accented e in question).

http://www.asciitable.com/

We ran into the same issues with users trying to use the danish character for a / e.




回答2:


If the DeliveryFormat property of your SmtpClient instance is set to SmtpDeliveryFormat.SevenBit (the default) then you need to make sure your SMTP gateway is replying with SMTPUTF8 when sent EHLO by .NET while it's trying to send the email. SmtpClient uses this to work out if the gateway is able to support UTF8.

If the DeliveryFormat is SmtpDeliveryFormat.International, then you will be able to send regardless.




回答3:


Late answer, but, I solved this by specifying encoding like this:

var mailMessage = new MailMessage
            {
               From = new MailAddress("test@domain.co.zw", "Test User", Encoding.UTF8)
}

In my case, the server was causing the error.



来源:https://stackoverflow.com/questions/13260772/smtpexception-the-client-or-server-is-only-configured-for-e-mail-addresses-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!