本文介绍了带有非ASCII长名称的电子邮件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我尝试发送 System.Net.Mail.MailMessage 和 System.Net.Mail.Attachment 。附件名称为Счёт-договор№4321от4июля.pdf Name of attachment is "Счёт-договор №4321 от 4 июля.pdf"附件创建代码: var nameEncoding = Encoding.UTF8;return new System.Net.Mail.Attachment(new MemoryStream(bytes), MessageBodiesHelpers.EncodeAttachmentName(fileName, nameEncoding), attachment.MediaType) { TransferEncoding = TransferEncoding.Base64, NameEncoding = nameEncoding }; MessageBodiesHelpers.EncodeAttachmentName 中的代码取自 https://social.msdn.microsoft.com/Forums/zh-CN/b6c764f7-4697-4394-b45f-128a24306d55/40-smtpclientsend-attachments-mit-umlauten-im-dateinamen?forum=dotnetframeworkde 如果我将该附件发送到gmail或ms exchange,则附件名称已成功解码。 但是! If I send that attachment to gmail or ms exchange, then name of attachment decoded successfully. BUT!如果我将该附件发送到 icloud ,那么我会收到 ????-??? ????№4321от4????。pdf If I send that attachment to icloud then I get "????-??????? №4321 от4 ????.pdf"邮件附件标题:来自ms交换:Content-Type: application/pdf; name="=?utf-8?B?0KHRh9GR0YIt0LTQ?==?utf-8?B?vtCz0L7QstC+0YAg?==?utf-8?B?4oSWNDMyMSDQvtGC?==?utf-8?B?IDQg0LjRjtC70Y8u?==?utf-8?B?cGRm?="Content-Transfer-Encoding: base64Content-Disposition: attachment来自icloud:Content-Transfer-Encoding: BASE64Content-Type: APPLICATION/PDF; name="????-??????? =?utf-8?B?4oSWNDMyMSDQvtGC?= 4 ????.pdf"如何格式化icloud的名称?How to format name for icloud? upd 如果我将邮件从Outlook(MS交换)转发到icloud,则附件名称成功解码。标头:If I forward message from outlook (ms exchange) to icloud, then name of attachment decoded successfully. Headers:Content-Transfer-Encoding: BASE64Content-Disposition: ATTACHMENT; size=200702; modification-date="Mon, 04 Jul 2016 13:40:22 GMT"; filename*=utf-8''%D0%A1%D1%87%D1%91%D1%82%2D%D0%B4%D0%BE%D0%B3%D0%BE%D0%B2%D0%BE%D1%80%20%E2%84%964321%20%D0%BE%D1%82%204%20%D0%B8%D1%8E%D0%BB%D1%8F.pdf; creation-date="Mon, 04 Jul 2016 13:40:22 GMT"Content-Type: APPLICATION/PDF; name*=utf-8''%D0%A1%D1%87%D1%91%D1%82%2D%D0%B4%D0%BE%D0%B3%D0%BE%D0%B2%D0%BE%D1%80%20%E2%84%964321%20%D0%BE%D1%82%204%20%D0%B8%D1%8E%D0%BB%D1%8F.pdf upd2 如果我使用icloud(icloud.com)的Web界面阅读邮件,则附件的名称已成功解码。If I read messages using web interface of icloud (icloud.com), then name of attachment decoded successfully.推荐答案在.NET中4.0 SmtpClient现在实现RFC行长度限制(76个字符)。这需要对编码过程进行广泛的更改,而不会涉及您所描述的问题。In .NET 4.0 SmtpClient now implements the RFC line length limits (76 chars). This required extensive changes to the encoding process and not covered a few issues like the one you describe.在您的情况下,存在非ASCII附件名称的问题,将被编码为超过41个utf-8字节(Encoding.UTF8.GetByteCount(fileName);)。在这种情况下,名称被编码两次,并且其中可能会有额外的换行符。唯一已知的解决方法是限制非ascii文件名的长度。In your case, there is an issue with non-ascii attachment names that would be encoded to more than 41 utf-8 bytes (Encoding.UTF8.GetByteCount(fileName);). In this scenario the names are encoded twice and may have extra line breaks in them. The only known workaround is to limit the length of non-ascii file names.您可以阅读此发布有关您的问题的更多信息you can read this post for more info about your issue 这篇关于带有非ASCII长名称的电子邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-26 07:13