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

3

8

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

Uninspired answered 27/12, 2011 at 9:8 Comment(3)
Can you dump and look at the actual mail message, as it's sent and as it's received, for both display name formats, to look for differences?Bargeman
@AdamMaras For the receiver, I already did and it is wrong. For the sending part, How could I do this? Tracing in the mail server what gets received from my application?Uninspired
That, or using something like Fiddler to see what the application writes to the connection.Bargeman
B
4

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.

Bargeman answered 27/12, 2011 at 9:36 Comment(5)
Thanks, Adam, I've just updated my question with the results from Microsoft Network Monitor.Uninspired
Yeah, this seems in-line with what the KB article (and everything I've read on peoples' experiences with it) so I'd recommend trying the hotfix.Bargeman
Hotfix worked great on my client, trying now on the server. Thanks a lot, Adam!Uninspired
No problem, friend. :) Though we're all taught that when we have a problem, it's almost never the platform's fault... sometimes the platform is actually at fault (however rare it may be).Bargeman
Worked on the server, too. I'm happy :-)Uninspired
C
2

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

Caul answered 27/12, 2011 at 9:31 Comment(1)
Thanks, Bastian, unfortunately this did not help. Thanks for your hint, though!Uninspired
D
0

We actually had similar issues. Mainly with the Subject though. One step in the right direction was MailMergeLib:

http://www.codeproject.com/KB/IP/MailMergeLib.aspx

You could give that a try, but it didn't solve all our issues. Now we switched to Aspose.Email. It's better but still not perfect, we still have garbeled subject, but now only on Mac and iPhones, but they are working on a bugfix.

http://www.aspose.com/categories/.net-components/aspose.email-for-.net/default.aspx

You can try it for free. There are other email libraries on the net.
http://www.chilkatsoft.com/products.asp

Would be very intersted if you figure out something more.

Dato answered 27/12, 2011 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.