Attachment filename not en/de-coded correctly when longer than 13 characters
I am using the following code to send an email attachment. I'm using C#, .NET 4.0, BPOS Exchange server to send.
var message = new MailMessage("duncanbayne@example.com", "duncanbayne@example.com")
{
Subject = "Test Message"
};
var ms = new MemoryStream(Encoding.UTF8.GetBytes("我希望這個作品。"));
var attachment = new Attachment(ms, "檢", "text/plain")
{
TransferEncoding = TransferEncoding.Base64
};
message.Attachments.Add(attachment);
var server = new SmtpClient("smtpserver", 25);
server.Send(message);
This works just fine. If I gradually increase the length of the filename, things continue to work, up to & including the point where the filename is 13 characters long ("檢檢檢檢檢檢檢檢檢檢檢檢檢"):
http://i.stack.imgur.com/lm1Ey.png
However, if I increase the filename length to 14 characters ("檢檢檢檢檢檢檢檢檢檢檢檢檢檢"), something goes awry and the filename appears in Outlook as though it's been wrongly en/de-coded:
http://i.stack.imgur.com/gvNMV.png
In either case, the attachment contents are intact ("我希望這個作品。"). Only the name of the attachment is affected.
Clearly I'm hitting some sort of limit here ... could someone please tell me what I'm doing wrong?
Update: The problem occurs regardless of whether the client is Gmail or Outlook 2010. However, when the client is Gmail and the filename is > 13 characters, the attachment also appears in the body of the message:
http://i.stack.imgur.com/DYtCN.p开发者_StackOverflow社区ng
Turns out that this is a known issue, & Microsoft has issued a hotfix. See: An email message attachment name that contains non-ASCII characters and is longer than 41 UTF-8 encoded bytes is encoded two times before transmission in an application that is compiled for the .NET Framework 4.
精彩评论