Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erroneous email receiver display when using German umlauts and a comma in name

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:

enter image description here

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:

enter image description here

like image 684
Uwe Keim Avatar asked Dec 27 '11 09:12

Uwe Keim


2 Answers

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.

like image 85
Adam Maras Avatar answered Oct 17 '22 12:10

Adam Maras


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) ?

like image 31
Bastian Avatar answered Oct 17 '22 11:10

Bastian