C# System.Net.Mail 和 =?utf-8?B?XXXXX.... 标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/454833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.Net.Mail and =?utf-8?B?XXXXX.... Headers
提问by Omer van Kloeten
I'm trying to use the code below to send messages via System.Net.Mail
and am sometimesgetting subjects like '=?utf-8?B?W3AxM25dIEZpbGV...'
(trimmed). This is the code that's called:
我正在尝试使用下面的代码通过以下代码发送消息,System.Net.Mail
并且有时会收到诸如'=?utf-8?B?W3AxM25dIEZpbGV...'
(修剪)之类的主题。这是调用的代码:
MailMessage message = new MailMessage()
{
From = new MailAddress("[email protected]", "Service"),
BodyEncoding = Encoding.UTF8,
Body = body,
IsBodyHtml = true,
ReplyTo = new MailAddress("[email protected]"),
SubjectEncoding = Encoding.UTF8
};
foreach (string emailAddress in addresses)
{
message.To.Add(new MailAddress(emailAddress.Trim(), "Person"));
}
message.Subject = subject;
I'd like to emphasize that this does not happen all of the time.
我想强调的是,这不会一直发生。
What am I doing wrong?
我究竟做错了什么?
采纳答案by Rowland Shaw
When your subject contains characters outside the ASCII range, then the mailing software must encode them (RFC2822 mail does not permit non-ASCII characters in headers). There are two ways to do this:
当您的主题包含 ASCII 范围之外的字符时,邮件软件必须对它们进行编码(RFC2822 邮件不允许在标题中使用非 ASCII 字符)。有两种方法可以做到这一点:
- Quoted Printable(subject starts with
"=?utf-8?Q"
) - Base64(subject starts with
"=?utf-8?B"
)
- 引用可打印(主题以 开头
"=?utf-8?Q"
) - Base64(主题以 开头
"=?utf-8?B"
)
It appears that the framework has figured that the Base64 encoding is more efficient (=shorter) than the quoted-printable encoding. This makes sense when your subject contains relatively many characters outside the ASCII range.
似乎该框架认为 Base64 编码比引用可打印编码更有效(=更短)。当您的主题包含相对较多的 ASCII 范围之外的字符时,这是有道理的。
To answer your question: You're doing nothing wrong. That's how internet mail with non-ASCII characters is supposed to look like. Of course, the software that reads such mail should detect and decode such subject fields.
回答你的问题:你没有做错任何事。这就是带有非 ASCII 字符的 Internet 邮件的外观。当然,读取此类邮件的软件应该检测并解码此类主题字段。
回答by Rowland Shaw
The answer might be in the rest of the trimmed subject -- the section you provided decodes as "[p13n] File", but is you've any non-ASCII characters in there, then I would expect it to encode as it has
答案可能在修剪主题的其余部分——您提供的部分解码为“[p13n] 文件”,但是您是否在那里有任何非 ASCII 字符,那么我希望它按照它的方式进行编码
回答by jcl
I came across this post when I was debugging an identical problem, and based on my further investigations I can provide an alternate explanation to Andreas's:
我在调试相同的问题时遇到了这篇文章,根据我的进一步调查,我可以为 Andreas 提供另一种解释:
The problem may be that your e-mail client software (Outlook 2003, in my case) is incorrectly decoding the subject line. In other words, it's a bug in Outlook, not in .NET or your program.
问题可能是您的电子邮件客户端软件(在我的情况下为 Outlook 2003)未正确解码主题行。换句话说,这是 Outlook 中的错误,而不是 .NET 或您的程序中的错误。
If you use a subject value like this (the letter "c" repeated 256 times), it displays fine in Outlook:
如果您使用这样的主题值(字母“c”重复 256 次),它在 Outlook 中显示正常:
subject = New String("c"c, 256)
Likewise, if you use a subject like this (the letter "c" repeated 178 times, with a Unicode non-breaking space character appended), it also displays as expected in Outlook:
同样,如果您使用这样的主题(字母“c”重复 178 次,并附加一个 Unicode 不间断空格字符),它也会按预期在 Outlook 中显示:
subject = New String("c"c, 178) + System.Text.Encoding.UTF8.GetChars(New Byte() {194, 160})
However, the following subject displays as "=?utf-8?B"-prepended garbage in Outlook:
但是,以下主题在 Outlook 中显示为“=?utf-8?B”-前置垃圾:
subject = New String("c"c, 179) + System.Text.Encoding.UTF8.GetChars(New Byte() {194, 160})
The difference is that this third subject line is 256 bytes when UTF-8-encoded. I assume that Outlook must be truncating the subject line to 255 characters before displaying it... which would be fine except that it is doing this by truncating the encoded string to 255 bytes, which cuts off the encoding terminator ("?="), making it undecodable.
不同之处在于,当 UTF-8 编码时,第三个主题行是 256 个字节。我假设 Outlook 必须在显示它之前将主题行截断为 255 个字符......这很好,只是它通过将编码字符串截断为 255 个字节来执行此操作,这会切断编码终止符(“?=”) ,使其无法解码。
This is a bug in Outlook and not your mail provider or .NET; you can see the full, untruncated UTF-8 encoded subject line in Outlook by right-clicking on the message in your message list and selecting "Options..." from the context menu, then scrolling down in the "Internet Headers" box until you see the line starting with "Subject:".
这是 Outlook 中的错误,而不是您的邮件提供商或 .NET;通过右键单击邮件列表中的邮件并从上下文菜单中选择“选项...”,然后在“Internet 标题”框中向下滚动,直到您会看到以“主题:”开头的行。
Contrary to the situation that Andreas suggests, the problem manifests itself not only when there are many non-ASCII characters, but when there is one or more non-ASCII characters and the subject line is long. A workaround might be to use a shorter subject line or to strip out all the non-ASCII characters in your subject.
与 Andreas 建议的情况相反,问题不仅出现在非 ASCII 字符很多时,而且出现在一个或多个非 ASCII 字符且主题行很长时。解决方法可能是使用较短的主题行或删除主题中的所有非 ASCII 字符。
(This bug was particularly tricky for me to track down because, as above, the problem data included no obvious non-ASCII characters -- just a few non-breaking spaces. These display the same as regular ASCII spaces when you print them out to the console. Moreover, if you change the value of the string variable in the Visual Studio debugger, it silently replaces them with regular spaces.)
(这个错误对我来说特别棘手,因为如上所述,问题数据不包含明显的非 ASCII 字符——只有几个不间断的空格。当你将它们打印出来时,这些显示与常规 ASCII 空格相同控制台。此外,如果您在 Visual Studio 调试器中更改字符串变量的值,它会默默地将它们替换为常规空格。)