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é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>
Kenster
  • 23,465
  • 21
  • 80
  • 106
Xavier Poinas
  • 19,377
  • 14
  • 63
  • 95

4 Answers4

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:

Andrew
  • 2,605
  • 3
  • 23
  • 34
  • 2
    Tried setting the delivery format to International, still getting the same error (with Gmail's server). – Xavier Poinas Feb 14 '14 at 12:38
  • 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. – Cristian Cotovan Sep 02 '14 at 10:40
  • 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. – Cristian Cotovan Sep 02 '14 at 10:41
  • @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? – Andrew Sep 02 '14 at 13:10
  • FYI the SmtpClient in .net 4+ knows about unicode but not in 2.0 and before – Bolo Jan 03 '18 at 18:10
  • 4
    According [to the reference source](https://referencesource.microsoft.com/#System/net/System/Net/mail/SmtpClient.cs,ebaa2d61cfed8051) and my tests, **both** conditions must be satisfied (`SmtpDeliveryFormat.International` and `SMTPUTF8`), not **either** of them, like your answer suggests. – Heinzi May 17 '19 at 10:35
  • 1
    @realsonic you're right... 6 years and 13k views later! – Andrew Jun 30 '19 at 14:43
  • @Andrew maybe you'll laugh, but I faced the same issue last Friday. :) – realsonic Jul 01 '19 at 15:31
  • 1
    @Heinzi 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. – Andrew Jul 12 '21 at 13:58
  • @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. – Anders Jun 29 '22 at 13:40
  • in my case I have extra space in db column lol – Satish Patil Aug 08 '23 at 12:18
2

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.

robasta
  • 4,621
  • 5
  • 35
  • 53
  • 2
    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. – Dave de Jong Dec 30 '19 at 22:47
1

The below code worked for me.

SmtpClient smtpClient = new SmtpClient(SMTPServer, SMTPPort)
{
    Credentials = new NetworkCredential("SMTPUserName", "SMTPPassword"),
    EnableSsl = true,
    DeliveryFormat = SmtpDeliveryFormat.International,
}; 
Josef
  • 2,869
  • 2
  • 22
  • 23
Santhosh N
  • 62
  • 4
-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.

tsells
  • 2,751
  • 1
  • 18
  • 20
  • 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" – Xavier Poinas Nov 08 '12 at 06:03
  • 11
    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 EHLO – Andrew Apr 17 '13 at 14:22
  • You need to use reflector or something to inspect the BCL. Look at SmtpClient's private IsUnicodeSupported property. – Andrew Apr 18 '13 at 14:17
  • 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. – tsells Apr 19 '13 at 01:18
  • 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. – Andrew Apr 22 '13 at 11:41