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

4

20

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>
Amasa answered 6/11, 2012 at 22:56 Comment(2)
can you show the code that you are using when trying to send email via SmtpClient.Send() methodAssoil
The code is very straightforward, similar to the example on the SmtpClient.Send() documentation, and otherwise working fine.Amasa
W
29

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:

Wed answered 17/4, 2013 at 14:24 Comment(11)
Tried setting the delivery format to International, still getting the same error (with Gmail's server).Amasa
Same for me, even though the emailClient option is set to SmtpDeliveryFormat.International, it still complains that "The client or server is only configured for E-mail addresses with ASCII local-parts" ... this is sending via Mandrill SMTP server.Neman
Another note to say that I am using on other installs of the same application, Mandrill, to send e-mail with Cyrillic characters and that has no problems.Neman
@CristiCotovan what version of the .NET framework/BCL are you using? What does reflector say about the code paths we mentioned above? Have things changed?Wed
FYI the SmtpClient in .net 4+ knows about unicode but not in 2.0 and beforeWindlass
According to the reference source and my tests, both conditions must be satisfied (SmtpDeliveryFormat.International and SMTPUTF8), not either of them, like your answer suggests.Oldtime
@realsonic you're right... 6 years and 13k views later!Wed
@Wed maybe you'll laugh, but I faced the same issue last Friday. :)Luther
@Oldtime I've adjusted the answer as per your comments. Sorry it took so long. I can't fathom why I thought this was different back at the time, although I was probably digging into the logic via reflector / testing with a questionable SMTP server which may have complicated things.Wed
@Andrew, from what I can see in the reference source, the SMTP server must always return SMTPUTF8 for things to work properly, not only for SmtpDeliveryFormat.SevenBit which the answer is still suggesting. Perhaps that is what you mean, but in that case the text in the answer should be rephrased for clarity.Philomenaphiloo
in my case I have extra space in db column lolPisces
C
2

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.

Clorindaclorinde answered 9/3, 2016 at 18:14 Comment(1)
That encoding is for the display name, not for the email address itself. In any instance, specifying the encoding did not fix it for me.Animadvert
N
1

The below code worked for me.

SmtpClient smtpClient = new SmtpClient(SMTPServer, SMTPPort)
{
    Credentials = new NetworkCredential("SMTPUserName", "SMTPPassword"),
    EnableSsl = true,
    DeliveryFormat = SmtpDeliveryFormat.International,
}; 
Nato answered 8/7, 2021 at 8:0 Comment(0)
D
-7

.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.

Decasyllabic answered 7/11, 2012 at 3:44 Comment(5)
You're right, I found some more info that I didn't find initially because people seem to be getting a different error message for that same problem: "The specified string is not in the form required for an e-mail address"Amasa
this answer is incorrect - .NET does support non-ASCII and will happily send the mail as long as the SMTP gateway responds with SMTPUTF8 in response to EHLOWed
You need to use reflector or something to inspect the BCL. Look at SmtpClient's private IsUnicodeSupported property.Wed
So what you are saying then is the chance of the email being sent is based upon the email server you are sending it though and it's configuration. This is usually not an acceptable solution if it's not the default setting on most email servers.Decasyllabic
If you use the default DeliveryMethod, then that is correct. Whether it's an optimal solution or not is a moot point as this is how it's implemented in the BCL.Wed

© 2022 - 2024 — McMap. All rights reserved.