System.Net.Mail和=?UTF-8?B'XXXXX ....标题标题、Mail、Net、System

2023-09-02 10:31:03 作者:简单也极端

我尝试使用下面的code发送消息通过 System.Net.Mail 和我的有时获得的科目,如=?UTF-8?B'W3AxM25dIEZpbGV ......(修剪)。这是就是所谓的code:

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范围之外的字符,然后邮寄软件必须连接code他们(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:

引用Printable(主题开头=?UTF-8?Q) 的Base64(主题开头=?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字符应该样子。当然,阅读这些邮件的软件,应检测和去code等学科领域。

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.