Attachment's name is wrong decoded if norwegian letters are used
Asked Answered
A

2

10

I have this piece of code which creates an attachment and sends email. If name of the file contains æ, ø or æ, the name is totally destroyed.

enter image description here

If I remove norwegian letters, everything is ok

enter image description here

        var stream = new MemoryStream();
        doc.Save(stream, SaveFormat.Docx);

        mail.From = new MailAddress("[email protected]");
        mail.To.Add("[email protected]");
        mail.IsBodyHtml = true;
        mail.Subject = "Attachments test";
        mail.Body = "Hei,<br /><br />";
        stream.Seek(0, SeekOrigin.Begin);

        var attachment = new Attachment(stream, "Name Å Æ Ø.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        attachment.NameEncoding = Encoding.UTF8;
        mail.Attachments.Add(attachment);
        var smtp = new SmtpClient("smtp.server.com") {Port = 25};
        smtp.Send(mail);

How to get this work properly?

SOLUTION

I found a solution here http://social.msdn.microsoft.com/Forums/en-US/dotnetframeworkde/thread/b6c764f7-4697-4394-b45f-128a24306d55

Auden answered 3/5, 2012 at 15:31 Comment(2)
Is mail a MailMessage? If so, could you check if all "encoding" properties, like BodyEncoding, HeadersEncoding and so on, have the same value?Centenary
All these parameters are UTF8 > mail.SubjectEncoding = Encoding.UTF8; mail.BodyEncoding = Encoding.UTF8; mail.HeadersEncoding = Encoding.UTF8; It does not help.Auden
B
2

here is resolution from microsoft for .net framework 4

http://support.microsoft.com/kb/2402064

Busybody answered 6/9, 2013 at 4:59 Comment(1)
direct download: 64: hotfixv4.microsoft.com/… 32: hotfixv4.microsoft.com/…Hetman
W
0

Try changing attachment.NameEncoding = Encoding.UTF8; to attachment.NameEncoding = Encoding.Unicode;.

Weaverbird answered 3/5, 2012 at 15:34 Comment(1)
I have tried all Encodings. If I use Unicode name like this =utf-16MUAByg8........ is coming up.Auden

© 2022 - 2024 — McMap. All rights reserved.