为 MailMessage 的每个部分设置编码
我的目标是面向国际市场的 .NET 应用程序,并且需要实现邮件发送功能。我注意到 MailMessage 类有多种设置编码的选项:
我在应用程序的配置页面中添加了一个下拉菜单来选择邮件消息编码。
将 MailMessage 上的每个属性设置为使用此编码是否是最佳实践?世界各地的邮件服务器和客户端会处理它吗?或者只有部分应该这样设置?
I'm targeting a .NET application for the international market and need to implement mail sending functionality. I've noticed on the MailMessage class there are several options for setting encoding:
I have added a drop-down in the app's config page to choose the mail message encoding.
Is it best practice to set every property on the MailMessage to use this encoding? Will mail servers and clients around the world handle it? Or should only some be set this way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的回答是不。
现在,我将详细阐述原因。正确的国际化邮件消息支持以下功能:
编码目前,国际化域名是基本的 ASCII 字符串,通过 Punycode 编码(您可以在 RFC 3492、RFC 5891 和 RFC 5890)。长话短说,您应该使用
IdnMapping
类将 UTF-16 字符串转换为 ASCII(可以通过调用GetAscii()
方法来完成。通过安全编码对于主题行和其他(非地址之类)标头,我的意思是,目前它仍然应该是 7 位安全的,因为相当多旧的和过时的邮件传输代理不支持 8 位编码。您实际上可以在这里支持用户的选择,但如果用户选择 ISO-8859-x、KOI-8-x 或 Shift-JIS 等内容,我建议使用 Quoted Printable 或 Base64 算法进一步编码标头,只是为了确保。如果用户选择 UTF-8,您可以使用 UTF-7 以及 QP 或 B64 编码标头,
最后但并非最不重要的一点是,您可以甚至应该支持用户选择的电子邮件正文编码。位基数(如 UCS2 或 UTF-16),我建议将传输编码设置为 Base64 等。
The short answer would be no.
Now, I am going to elaborate why. Correctly i18n-ed mail message supports these features:
For the time being, i18n-ed domain names are basic ASCII strings, encoded via Punycode (you can read more about it in RFC 3492, RFC 5891 and RFC 5890). To cut a long story short, you should use
IdnMapping
class to convert from UTF-16 string to ASCII (which you can do by callingGetAscii()
method.By safe encoding for subject line and other (non-address like) headers, I meant that for the time being, it should still be 7-bit safe, as quite a few old and outdated Mail Transfer Agents does not support 8-bit encodings here. Therefore you can actually support user's choice here, but if user choses something like ISO-8859-x, KOI-8-x, or Shift-JIS, I would recommend to further encode headers using Quoted Printable or Base64 algorithm, just to make sure. If user choses UTF-8, you can encode headers with UTF-7 as well as QP or B64.
Last but not least, you can and even should support user chosen encoding for e-mail message body. However, if it is 16-bit base (like UCS2 or UTF-16), I would recommend to set the Transfer Encoding to something like Base64.