问题描述
SmtpClient.Send() 方法在以下情况下抛出此异常我尝试向包含重音字符 (é) 的地址发送电子邮件:
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:客户端或服务器仅配置对于带有 ASCII 本地部分的电子邮件地址:léo.xxx@example.com.
在 System.Net.Mail.MailAddress.GetAddress(布尔 allowUnicode)
在 System.Net.Mail.SmtpClient.ValidateUnicodeRequirement(MailMessage...)
在 System.Net.Mail.SmtpClient.Send(MailMessage 消息)
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.
我尝试了多个 SMTP 服务器,包括 Gmail.以下是再现的相关位:
I have tried several SMTP servers, including Gmail. Here are the relevant bits for a repro:
代码
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>
推荐答案
如果SmtpClient 实例的 aspx" rel="nofollow noreferrer">DeliveryFormat 属性设置为 SmtpDeliveryFormat.SevenBit
(默认)然后您需要确保您的 SMTP 网关正在回复 .NET 在尝试发送电子邮件时发送
.EHLO
时的 SMTPUTF8SmtpClient
使用它来确定网关是否能够支持 UTF8.
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.
根据 .NET Framework 4.5+ 和 .NET core 的源,接收服务器必须支持 UTF8 并且 DeliveryFormat
必须设置为 SmtpDeliveryFormat.International
为了能够发送.
According to the source for both .NET Framework 4.5+ and .NET core, the receiving server must support UTF8 and the DeliveryFormat
must be set to SmtpDeliveryFormat.International
in order to be able to send.
有关最新逻辑和完整详细信息,请参阅 IsUnicodeSupported
in:
For the latest logic and full details, see IsUnicodeSupported
in:
- 当前 .NET 源代码
- .NET Framework 参考源
这篇关于SmtpException:客户端或服务器仅配置为具有 ASCII 本地部分的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!