在SmtpClient / MailMessage主题编码主题、SmtpClient、MailMessage

2023-09-04 09:47:48 作者:纠纠纠纠缠°

我试图发送包含使用 SmtpClient MailMessage 类非ASCII字符的电子邮件。

I am trying to send emails that contain non-ASCII characters using the SmtpClient and MailMessage classes.

我使用一个外部邮件服务( MailChimp )和我的一些电子邮件已经拒绝了他们的SMTP服务器。我曾接触过他们,这是他们的回答是:

I am using an external mailing service (MailChimp) and some of my emails have been rejected by their SMTP server. I have contacted them and this is what they replied:

它出现在主题行正在Base64的EN codeD,然后引用打印连接codeD,通常要细,但人物之一,被两行打破。所以,当你的主题行的时间长一点,才能被正确处理,它的破两行。当使用UTF-8引用的可打印在一个主题行,字符串不应该字里行间被打破。取而代之的是线应短,这样完整的字符串仍然在一起。在这种情况下,没有发生,使重$ P $字符的字符串psents单个字符被多行打破,因此它不是有效的UTF-8引用可打印连接codeD

It appears the subject line is being Base64 encoded and then Quoted-Printable encoded, which generally should be fine, but one of the characters is being broken across two lines. So when your subject lines are a bit longer, in order to be processed correctly, it's broken on to two lines. When using UTF-8 quoted printable in a subject line, character strings aren't supposed to be broken between lines. Instead a line should be shorted so that the full character string remains together. In this case, that's not happening, so the string of characters that represents a single character is being broken across multiple lines, and therefore isn't validly UTF-8 quoted-printable encoded.

有问题的主题是以下内容:

The problematic subject is the following:

Subject: XXXXXXX - 5 personnes vous ont nommé guide

这是在UTF-8 / Base64的:

Which is, in UTF-8/Base64:

Subject: WFhYWFhYWCAtIDUgcGVyc29ubmVzIHZvdXMgb250IG5vbW3DqSBndWlkZQ==

由于该头将超过特定的最大长度(我不能确定是否是引用打印编码和每行76个字符,或者SMTP头限制它的极限),编码和分割之后,首标将变成:

Because that header would exceed a certain maximum length (I am unsure whether it is the Quoted-Printable encoding and its limit of 76 characters per line, or the SMTP header limit), after encoding and split, the header will become:

Subject: =?utf-8?B?WFhYWFhYWCAtIDUgcGVyc29ubmVzIHZvdXMgb250IG5vbW3D?=
 =?utf-8?B?qSBndWlkZQ==?=

显然,这在解码时(因为第一线不能去coded到一个有效的字符串)会导致一个问题。我不知道我完全理解这个问题,我有以下问题:

Apparently this causes an issue when decoding (because the first line cannot be decoded to a valid string). I am not sure I fully understand the problem, and I have the following questions:

为什么?UTF-8?B'部分重复?不应该分裂线,因此其头不应重复之前的QP编码发生的呢? 在QP解码,难道我们不应该结束了一个有效的1线的Base64字符串? 在这里是它是QP编码之外的第二行开始的空间,可这是什么问题? 是EN codeR坏了,或者是去codeR?

另外请注意,一些其他SMTP服务器会接受此消息,不过,这并不意味着它是有效的。

Also note that some other SMTP servers will accept this message, though that does not mean it is valid.

作为一种解决办法,我已经试过禁用Base64编码,这显然是不必要的,但是MailMessage类有一个BodyTransferEncoding属性控制此编码,但只适用于消息的主体部分。无属性似乎能控制对象的转让的编码。

As a workaround, I have tried disabling the Base64 encoding, which apparently is unnecessary, however the MailMessage class has a BodyTransferEncoding property that controls this encoding, but only for the body part of the message. No property seems to control the "transfer" encoding of the subject.

推荐答案

本被确认为在MSDN论坛中的错误: http://social.msdn.microsoft.com/Forums/vstudio/en-US/4d1c1752-70ba-420a-9510-8fb4aa6da046/subject-encoding-on-smtpclientmailmessage

This was confirmed as a bug in the MSDN forums: http://social.msdn.microsoft.com/Forums/vstudio/en-US/4d1c1752-70ba-420a-9510-8fb4aa6da046/subject-encoding-on-smtpclientmailmessage

和一个错误的提交Microsoft连接: https://connect.microsoft.com/VisualStudio/feedback/details/785710/mailmessage-subject-incorrectly-en$c$cd-in-utf-8-base64

And a bug was filed on Microsoft Connect: https://connect.microsoft.com/VisualStudio/feedback/details/785710/mailmessage-subject-incorrectly-encoded-in-utf-8-base64

一个解决办法是到MailMessage的SubjectEncoding设置为其他编码,如ISO-8859-1。在这种情况下,这个问题将成为连接中引用Printable(不是Base64的)codeD这就避免了这个问题。

One work-around is to set the SubjectEncoding of the MailMessage to an other encoding, such as ISO-8859-1. In this case, the subject will be encoded in Quoted Printable (not Base64) which avoids the problem.