在名称中使用德语变音符号和逗号时,电子邮件接收器显示错误
使用 MailMessage
类< /a> 在 .NET 4 中,我今天发现了一个至今无法解决的问题。请参阅以下代码:
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);
}
这是一个简单的代码片段,因此请发送一条 SMTP 消息。相互尝试 /*1*/
、/*2*/
和 /*3*/
行,行为会有所不同
: (“收件人”)名称包含德语变音符号(即“Ä”、“Ö”或“Ü”)和逗号(即“,”),接收者会看到损坏的文本他收到的电子邮件:
正如您在上面的屏幕截图(取自 Outlook 2010)中看到的,有一个神秘的“=?utf- “收件人:”行中的“8?Q?M=C3=BCller”。
省略逗号或删除德语元音变音可以解决此问题。我尝试了 Exchange 2003 和 hmailserver 以获得相同的结果。
我的问题是:
有人知道这种行为并有解决方案吗?
更新 1:
根据用户 Adam Maras 的建议,我启动了 Microsoft Network Monitor,同时发送电子邮件。
对我来说,似乎 MailMessage
类(或 SmtpClient
类?)已经做错了:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,经过一番挖掘,我发现了 Microsoft 支持文章 2576045:修复:电子邮件客户端不支持 a 中的名称如果名称包含非 ASCII 字符,则由 .NET Framework 4 中的
MailMessage
类编码的投递地址字段。看来,在写出包含 Unicode 字符的地址时,
MailMessage
类未正确编码某些内容。我当然无法根据知识库文章的信息告诉你它是什么,但无论它是什么,都足以让下游读者被标题噎住。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.电子邮件标头必须为 us-ascii(7 位),使用变音符号需要按照 rfc2047 中所述进行编码。对字符串进行编码的方法有多种,看起来 Outlook 或您的邮件服务器无法理解 utf8 编码。
您可以尝试使用 iso-8859-1 自行对地址进行 mime 编码。
编辑:我刚刚检查了文档 http://msdn .microsoft.com/en-us/library/system.net.mail.mailaddress.aspx
您是否尝试过使用 MailAddress(String, String, Encoding) ?
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) ?
我们实际上也遇到过类似的问题。主要是与主题有关。
MailMergeLib 是朝着正确方向迈出的一步:
http://www.codeproject.com/KB/ IP/MailMergeLib.aspx
您可以尝试一下,但它并不能解决我们所有的问题。现在我们切换到Aspose.Email。它更好了,但仍然不完美,我们仍然有乱码主题,但现在仅在 Mac 和 iPhone 上,但他们正在修复错误。
http://www.aspose.com /categories/.net-components/aspose.email-for-.net/default.aspx
您可以免费试用。网上还有其他电子邮件库。
http://www.chilkatsoft.com/products.asp
如果您弄清楚,我会非常感兴趣更多的东西。
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.