在处理通过电子邮件发送带有国际文件名的文件的项目时,我遇到了一个不寻常的问题。如果我只附加一个US-ASCII文件名,我可以获得超过200个字符长度而没有错误。
如果我包含一个扩展字符,它以UTF-8编码,并且它变得有趣之前的长度非常小(<40个字符)。定义funky ..这是一个变坏后的示例文件名:
=utf-8BSU5GT1JNw4FUSUNBX0ltcGFjdF9Bc3Nl
它看起来像UTF8编码的字符串,带有UTF-8解码指令或mime边界......不确定是哪个。
之前有没有人见过这个 - 以及文件名的规则/限制是什么 - 我尝试通过outlook手动通过电子邮件发送文件并处理它,所以我认为这不是MIME特定限制。
示例代码:
class Program
{
private const string DOMAIN = "foobar.com";
private const string SMTPHOST = "mail." + DOMAIN;
private const string FROM = "chadwick.posey@" + DOMAIN;
private const string TO = FROM;
static void Main(string[] args)
{
MailMessage msg = new MailMessage(FROM, TO, "Subject", "Body");
string path = Path.GetTempPath();
string name = "AAAAAA_AAAAAAAAAAAA_AAAAAAA_AAAA - IIIIIII CCCCCCCCCC DD IIIIIIÁIIII_20111018_091327.pptx";
File.WriteAllText(path + "\\" + name, "blah");
Attachment att = new Attachment(path + "\\" + name, new ContentType("application/vnd.openxmlformats-officedocument.presentationml.presentation"));
msg.Attachments.Add(att);
SmtpClient client = new SmtpClient(SMTPHOST, 25);
client.Send(msg);
}
}
我已经尝试过(到目前为止) - 将attachment.NameEncoding的编码设置为UTF8和UTF32,但都没有工作。在附件上设置ContentDisposition.FileName失败,因为它不使用US-ASCII字符。
有关如何让它包含带有重音/扩展字符的完整文件名的任何建议吗?
由于 查德威克
答案 0 :(得分:1)
微软有一个修补程序似乎已经为我做了诀窍。 Check http://support.microsoft.com/kb/2402064 在该页面上,有一个下载链接可以满足您的需求。 只需安装正确的版本,具体取决于您的系统。
答案 1 :(得分:0)
这应该有效:
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);
How to set the attatchment file name with chinese characters in C# SmtpClient programming?