Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email attachment with long non-ascii name

I try to send System.Net.Mail.MailMessage with System.Net.Mail.Attachment.

Name of attachment is "Счёт-договор №4321 от 4 июля.pdf"

Code for attachment creation:

var nameEncoding = Encoding.UTF8;
return new System.Net.Mail.Attachment(new MemoryStream(bytes), 
                                      MessageBodiesHelpers.EncodeAttachmentName(fileName, nameEncoding),
                                      attachment.MediaType)
       {
            TransferEncoding = TransferEncoding.Base64,
            NameEncoding = nameEncoding
       };

Code inside MessageBodiesHelpers.EncodeAttachmentName taken from https://social.msdn.microsoft.com/Forums/en-US/b6c764f7-4697-4394-b45f-128a24306d55/40-smtpclientsend-attachments-mit-umlauten-im-dateinamen?forum=dotnetframeworkde

If I send that attachment to gmail or ms exchange, then name of attachment decoded successfully. BUT!

If I send that attachment to icloud then I get "????-??????? №4321 от4 ????.pdf"

Mail attachment headers:

From ms exchange:

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: base64
Content-Disposition: attachment

From icloud:

Content-Transfer-Encoding: BASE64
Content-Type: APPLICATION/PDF;
    name="????-??????? =?utf-8?B?4oSWNDMyMSDQvtGC?= 4 ????.pdf"

How to format name for icloud?

upd

If I forward message from outlook (ms exchange) to icloud, then name of attachment decoded successfully. Headers:

Content-Transfer-Encoding: BASE64
Content-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

If I read messages using web interface of icloud (icloud.com), then name of attachment decoded successfully.

like image 553
mif Avatar asked Jul 04 '16 14:07

mif


1 Answers

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.

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

like image 58
Abd Avatar answered Nov 15 '22 20:11

Abd