9

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("no-replay@email.no");
        mail.To.Add("my@email.no");
        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

podeig
  • 2,597
  • 8
  • 36
  • 60
  • Is `mail` a `MailMessage`? If so, could you check if all "encoding" properties, like `BodyEncoding`, `HeadersEncoding` and so on, have the same value? – Mr Lister May 03 '12 at 16:04
  • All these parameters are UTF8 > mail.SubjectEncoding = Encoding.UTF8; mail.BodyEncoding = Encoding.UTF8; mail.HeadersEncoding = Encoding.UTF8; It does not help. – podeig May 04 '12 at 08:07

2 Answers2

2

here is resolution from microsoft for .net framework 4

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

Saar
  • 8,286
  • 5
  • 30
  • 32
  • 1
    direct download: 64: http://hotfixv4.microsoft.com/.NET%20Framework%204.0%20-%20Windows%20XP,%20Windows%202003,%20Windows%20Vista,%20Windows%20Server%202008,%20Win7,%20Windows%20Server%202008%20R2%20(MSI)/nosp/DevDiv933059/30319.364/free/421618_intl_x64_zip.exe 32: http://hotfixv4.microsoft.com/.NET%20Framework%204.0%20-%20Windows%20XP,%20Windows%202003,%20Windows%20Vista,%20Windows%20Server%202008,%20Win7,%20Windows%20Server%202008%20R2%20(MSI)/nosp/DevDiv933059/30319.364/free/421617_intl_i386_zip.exe – phoenix Dec 19 '14 at 03:09
0

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

Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • I have tried all Encodings. If I use Unicode name like this =utf-16MUAByg8........ is coming up. – podeig May 03 '12 at 15:40