System.Net.Mail 和 =?utf-8?B?XXXXX.... 标题 [英] System.Net.Mail and =?utf-8?B?XXXXX.... Headers

查看:28
本文介绍了System.Net.Mail 和 =?utf-8?B?XXXXX.... 标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用下面的代码通过 System.Net.Mail 发送消息,并且我有时 收到诸如 '=?utf-8 之类的主题?B?W3AxM25dIEZpbGV...'(已修整).这是调用的代码:

I'm trying to use the code below to send messages via System.Net.Mail and am sometimes getting subjects like '=?utf-8?B?W3AxM25dIEZpbGV...' (trimmed). This is the code that's called:

MailMessage message = new MailMessage()
{
    From = new MailAddress("someone@somewhere.com", "Service"),
    BodyEncoding = Encoding.UTF8,
    Body = body,
    IsBodyHtml = true,
    ReplyTo = new MailAddress("do.not.reply@somewhere.com"),
    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.

我做错了什么?

推荐答案

当您的主题包含 ASCII 范围之外的字符时,邮件软件必须对它们进行编码(RFC2822 邮件不允许在标题中使用非 ASCII 字符).有两种方法可以做到这一点:

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:

  • 引用可打印(主题以"=?utf-8?Q"开头)
  • Base64(主题以 "=?utf-8?B" 开头)
  • Quoted Printable (subject starts with "=?utf-8?Q")
  • Base64 (subject starts with "=?utf-8?B")

框架似乎认为 Base64 编码比引用的可打印编码更有效(=更短).当您的主题包含相对较多的 ASCII 范围之外的字符时,这是有道理的.

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.

回答你的问题:你没有做错任何事.这就是带有非 ASCII 字符的 Internet 邮件的样子.当然,读取此类邮件的软件应该检测并解码此类主题字段.

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.

这篇关于System.Net.Mail 和 =?utf-8?B?XXXXX.... 标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆