带有重音符号的MailMessage附件文件名

时间:2011-03-18 09:03:26

标签: c# .net email mailmessage

我正在尝试发送带有附加Excel文件名的HTML电子邮件。这一切都运行良好,直到我发送邮件的附件名称包含重音字母:-(我尝试过的每一种解决方法都失败了。

原始代码:

  var attachment = new Attachment(
       new MemoryStream(excelFileContents),
       "simplefilename.xls");

这个工作正常。 但是,如果我用“échec.xls”替换“simplefilename.xls”,附件就会被包含(名称和内容)。

我试过这些,但无济于事:

  var attachment = new Attachment(
       new MemoryStream(excelFileContents),
       new System.Net.Mime.ContentType("application/vnd.ms-excel"));
  attachment.Name = "échec.xls";

最后一个更糟糕:SmtpClient.Send()抛出异常,抱怨文件名中的é

  var attachment = new Attachment(
       new MemoryStream(excelFileContents),
       new System.Net.Mime.ContentType("application/vnd.ms-excel"));
  attachment.ContentDisposition.FileName = "échec.xls";

我一直在敲打这个太久了。任何灯都热烈欢迎!

2 个答案:

答案 0 :(得分:6)

我终于找到了一个有效的答案。

http://social.msdn.microsoft.com/Forums/en-US/dotnetframeworkde/thread/b6c764f7-4697-4394-b45f-128a24306d55

除了Marcel Roma的帖子外,内容为德语。我将他的代码放入我的应用程序中。我能够发出带有重音符号的pdf,我们看到附件而不是垃圾。

所以这是:

public class AttachmentHelper
{
    public static System.Net.Mail.Attachment CreateAttachment(string attachmentFile, string displayName, TransferEncoding transferEncoding)
    {
        System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(attachmentFile);
        attachment.TransferEncoding = transferEncoding;

        string tranferEncodingMarker = String.Empty;
        string encodingMarker = String.Empty;
        int maxChunkLength = 0;

        switch (transferEncoding)
        {
            case TransferEncoding.Base64:
                tranferEncodingMarker = "B";
                encodingMarker = "UTF-8";
                maxChunkLength = 30;
                break;
            case TransferEncoding.QuotedPrintable:
                tranferEncodingMarker = "Q";
                encodingMarker = "ISO-8859-1";
                maxChunkLength = 76;
                break;
            default:
                throw (new ArgumentException(String.Format("The specified TransferEncoding is not supported: {0}", transferEncoding, "transferEncoding")));
        }

        attachment.NameEncoding = Encoding.GetEncoding(encodingMarker);

        string encodingtoken = String.Format("=?{0}?{1}?", encodingMarker, tranferEncodingMarker);
        string softbreak = "?=";
        string encodedAttachmentName = encodingtoken;

        if (attachment.TransferEncoding == TransferEncoding.QuotedPrintable)
            encodedAttachmentName = HttpUtility.UrlEncode(displayName, Encoding.Default).Replace("+", " ").Replace("%", "=");
        else
            encodedAttachmentName = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(displayName));

        encodedAttachmentName = SplitEncodedAttachmentName(encodingtoken, softbreak, maxChunkLength, encodedAttachmentName);
        attachment.Name = encodedAttachmentName;

        return attachment;
    }

    private static string SplitEncodedAttachmentName(string encodingtoken, string softbreak, int maxChunkLength, string encoded)
    {
        int splitLength = maxChunkLength - encodingtoken.Length - (softbreak.Length * 2);
        var parts = SplitByLength(encoded, splitLength);

        string encodedAttachmentName = encodingtoken;

        foreach (var part in parts)
            encodedAttachmentName += part + softbreak + encodingtoken;

        encodedAttachmentName = encodedAttachmentName.Remove(encodedAttachmentName.Length - encodingtoken.Length, encodingtoken.Length);
        return encodedAttachmentName;
    }

    private static IEnumerable<string> SplitByLength(string stringToSplit, int length)
    {
        while (stringToSplit.Length > length)
        {
            yield return stringToSplit.Substring(0, length);
            stringToSplit = stringToSplit.Substring(length);
        }

        if (stringToSplit.Length > 0) yield return stringToSplit;
    }
}

以下列方式使用它:

static void Main(string[] args)
{
    string smtpServer = String.Empty;
    string userName = String.Empty;
    string password = String.Empty;
    string attachmentFilePath = String.Empty;
    string displayName = String.Empty;

    System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(smtpServer);
    client.Credentials = new System.Net.NetworkCredential(userName, password);

    var msg = new System.Net.Mail.MailMessage(fromAddress, toAddress, "Subject", "Body");
    System.Net.Mail.Attachment attachment =
         AttachmentHelper.CreateAttachment(attachmentFilePath, displayName, TransferEncoding.Base64);

    msg.Attachments.Add(attachment);
    client.Send(msg);
}

答案 1 :(得分:4)