Using SmtpClient
, MailMessage
and MailAddress
classes, I cannot send to email addresses such as åbc.def@domain.se. I get the error/exceptions as shown below:
An invalid character was found in the mail header: 'å'.
--------------------------- Error sending email --------------------------- System.Net.Mail.SmtpException: The client or server is only configured for E-mail addresses with ASCII local-parts: åbc.def@domain.se.
at System.Net.Mail.MailAddress.GetUser(Boolean allowUnicode)
at System.Net.Mail.MailAddress.GetAddress(Boolean allowUnicode)
at System.Net.Mail.MailAddress.Encode(Int32 charsConsumed, Boolean allowUnicode)
at System.Net.Mail.MailAddressCollection.Encode(Int32 charsConsumed, Boolean allowUnicode) at System.Net.Mail.Message.PrepareHeaders(Boolean sendEnvelope, Boolean allowUnicode) at System.Net.Mail.Message.Send(BaseWriter writer, Boolean sendEnvelope, Boolean allowUnicode) at System.Net.Mail.SmtpClient.Send(MailMessage message)
These characters in my subject/body are fine, but not in email addresses.
I've tried setting SmtpClient.DeliveryMethod = SmtpDeliveryFormat.International
, or MailMessage.HeadersEncoding = Encoding.Unicode
(or UTF8
) and it doesn't seem to change anything. These are real email addresses of people we need to communicate with so it's a bit of a problem.
I have been digging through the .Net source code but not really gotten anywhere. I tracked down a ServerSupportsEai
property, and Google tells me EAI stands for Email Address Internationalisation (https://tools.ietf.org/html/rfc5336) but it's fairly unclear if this is a limitation in my code, or if the specific server I'm talking to just doesn't support this... since I'm using a test server to avoid sending emails to unsuspecting Swedish people!
Can someone help me clear this up - does .Net support this, if so what if anything should my client code do to enable it?
To be able to to send UTF-8 characters according to RFC6531 both client and server need to support it.
If you use the .NET implementation of
SmtpClient
you'll need to target framework version 4.5 because that implementation supports the SMTPUTF8 extension.If you set the
DeliveryFormat
property you have done what is needed for your client to support UTF8 characters:If we reverse-engineer the
Send
method we can easily follow the stack-trace from your question. You'll find this implementation:That
allowUnicode
boolean is being supplied from theSend
method:Now here comes the server in the picture. When does the private boolean
ServerSupportsEai
becomes true? It turns out that on sending theEHLO
command theSmptConnection
callsParseExtensions
:If you want to know upfront if your mail server is supporting that extension you can simply connect with a telnet client (I use putty) to your smtp server and send the
EHLO somename
command and inspect the results.Connecting to
smtp.gmail.com
on port 587 gives me this output:Notice the last line: it holds our required SMTPUTF8
tl;dr
To be able to use international characters in the emailaddres make sure to set the
DeliveryFormat
toSmtpDeliveryFormat.International
and use an smtp server that supports the SMTPUTF8 extension and advertises it in itsEHLO
command.