Reputation: 19733
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é[email protected].
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("[email protected]");
msg.To.Add(new MailAddress("lé[email protected]"));
new SmtpClient().Send(msg);
app.config
<system.net>
<mailSettings>
<smtp from="[email protected]">
<network host="smtp.gmail.com" port="587" userName="[email protected]" password="password" enableSsl="true" />
</smtp>
</mailSettings>
</system.net>
Upvotes: 21
Views: 29535
Reputation: 2715
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.
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.
For the latest logic and full details, see IsUnicodeSupported
in:
Upvotes: 29
Reputation: 82
The below code worked for me.
SmtpClient smtpClient = new SmtpClient(SMTPServer, SMTPPort)
{
Credentials = new NetworkCredential("SMTPUserName", "SMTPPassword"),
EnableSsl = true,
DeliveryFormat = SmtpDeliveryFormat.International,
};
Upvotes: 1
Reputation: 4701
Late answer, but, I solved this by specifying encoding like this:
var mailMessage = new MailMessage
{
From = new MailAddress("[email protected]", "Test User", Encoding.UTF8)
}
In my case, the server was causing the error.
Upvotes: 2
Reputation: 2771
.net only supports ASCII characters. I don't believe it supports extended ASCII characters (which includes the accented e in question).
We ran into the same issues with users trying to use the danish character for a / e.
Upvotes: -7