Using the MailMessage
class in .NET 4, I found an issue today that I'm unable to resolve so far. Please see the following code:
using (var message = new MailMessage())
{
message.From = new MailAddress(@"[email protected]", "Uwe Keim");
message.Bcc.Add(new MailAddress(@"[email protected]", "Uwe Keim"));
// This fails (see screenshot).
/*1*/ message.To.Add(new MailAddress(@"[email protected]", "Müller, Fred"));
// This succeeds.
/*2*/ message.To.Add(new MailAddress(@"[email protected]", "Fred Müller"));
// This also succeeds.
/*3*/ message.To.Add(new MailAddress(@"[email protected]", "Muller, Fred"));
message.Subject = "Test";
message.Body = "Some text body.";
new SmtpClient().Send(message);
}
This is a simple snippet so send an SMTP message. Mutually trying the lines /*1*/
, /*2*/
and /*3*/
, the behavior differs:
Whenever a receiver ("To") name contains a German umlaut (i.e. "Ä", "Ö" or "Ü") and a comma (i.e. ","), the receiver sees damaged text in the email message he receives:
As you can see in the above screenshot (taken from Outlook 2010), there is a cryptic "=?utf-8?Q?M=C3=BCller" in the "To:" line.
Leaving out the comma or removing the German umlaut fixes this. I've tried both Exchange 2003 and hmailserver to get the same result.
My question is:
Is anyone aware of this behaviour and has a solution to it?
Update 1:
As suggested by user Adam Maras, I fired up Microsoft Network Monitor while sending the email message.
To me, it seems that the MailMessage
class (or the SmtpClient
class?) is already doing it wrong:
So, after some digging, I came across Microsoft Support article 2576045: FIX: Email client does not support a name in a delivery address field that is encoded by the MailMessage
class in the .NET Framework 4 if the name contains a non-ASCII character.
It appears that, when writing out an address that contains Unicode characters, the MailMessage
class does not correctly encode something. I certainly can't tell you what it is based on the KB article's information, but whatever it is, it's enough to make downstream readers choke on the headers.
E-mail headers have to be us-ascii (7-bit), using umlauts need encoding as discribed in rfc2047. There are different ways to encode the strings and it looks like outlook or your mailserver won't understand the utf8 encoding.
You could try to mime encode the address yourself using iso-8859-1.
edit: I just checked the documentation at http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx
have you tried using MailAddress(String, String, Encoding) ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With