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

依然范特西╮ 提交于 2019-12-07 14:33:46

问题


my code is as below:

ContentType ct = new ContentType();
ct.MediaType = MediaTypeNames.Application.Octet;
ct.Name = "这是一个很长的中文文件名希望能用它在附件名中.Doc";
Attachment attach = new Attachment(stream, ct);

but the attachement received is not with the right chinese filename, and I found the ct.Name becames "=?utf-8?B?6L+Z5piv5LiA5Liq5b6I6ZW/55qE5Lit5paH5paH5Lu25ZCN5biM5pyb?=\r\n =?utf-8?B?6IO955So5a6D5Zyo6ZmE5Lu25ZCN5Lit?=" in the VS2010 debuger.

plz advice, how do I use the chinese charaters in attachment file name?


回答1:


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);



回答2:


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.




回答3:


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!



来源:https://stackoverflow.com/questions/4397332/how-to-set-the-attatchment-file-name-with-chinese-characters-in-c-sharp-smtpclie

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