How to set the attatchment file name with chinese characters in C# SmtpClient programming?

时光怂恿深爱的人放手 提交于 2019-12-05 21:06:22
Harish

Can you try:

Attachment att = new Attachment(@"c:\path to file\somename.txt",    
System.Net.Mime.MediaTypeNames.Application.Octet);

//this itself should work.
att.Name = "история-болезни.doc";  // non-english filename

//if the above line doesn't make it work, try this.
att.Name = System.Web.HttpUtility.UrlEncode(att.Name, System.Text.Encoding.UTF8);

No, that's the correct value in the debugger. MIME content file names must always ASCII and so other character sets must be encoded for transmission (see RFC 2047).

That's a base-64 encoded UTF-8 representation of your Chinese characters - that's what the =?utf8?B? prefix means. The recipient's email client should decode that back to the correct characters.

Edit: oops, I'd missed you were saying that the received filename is wrong. I suggest you try emailing different email clients to see if the problem is with the recipient, and to compare message headers with an email with the correct filename if you can generate one from a different client, but I'm not sure what to suggest beyond that.

finally, I found a workround, according to RFC 2047, the delimiter could be CRLF or SPACE between 'ecoded-word's, and the C# use the CRLF as the delimiter, but the NOTES/Gmail client could not interpretation it, and after I replace the CRLF with SPACE, it works well in the NOTE/Gmail.

Thanks Rup, your reference of RCF2047 help me!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!