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

Posted

技术标签:

【中文标题】MailMessage 带重音符号的附件文件名【英文标题】:MailMessage Attachment filename with accents 【发布时间】:2011-07-18 00:27:12 【问题描述】:

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

原码:

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

这个很好用。 但是,如果我将“simplefilename.xls”替换为“échec.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";

我一直在努力解决这个问题太久了。热烈欢迎任何灯光!

【问题讨论】:

我认为您可以在某处指定文件名的编码。但一般来说,带有特殊字符的文件名会在 ANSI 代码页不包含它们的 Windows 系统上引起很多问题。所以我会尽可能避免它们。 @CodeInChaos:我确实尝试过 NameEncoding,但无济于事。关于文件名中重音的使用,我有一个非常不同的看法:它们更具可读性,显然会被能够阅读它们的人使用,因此具有正确的 ansi 代码页。还是谢谢。 【参考方案1】:

附件名称需要使用Quoted-Printable格式:

C#: Class for decoding Quoted-Printable encoding? http://www.codeproject.com/KB/security/TextCoDec.aspx

【讨论】:

谢谢约翰。我确实注意到 Outlook 使用 Quoted-Printable 成功发送相同类型的电子邮件。我将深入研究您的第一个链接中提供的解决方案。【参考方案2】:

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

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

【讨论】:

以上是关于MailMessage 带重音符号的附件文件名的主要内容,如果未能解决你的问题,请参考以下文章

为什么我不能在单词边界旁使用带重音符号?

直接从 MIME 字符串将附件添加到 MailMessage

如何仅使用 CSS 摆脱重音符号

在 MailMessage 中添加附件 base64 图像并在 html 正文中读取

重音字符未显示

MySQL 表中带有重音符号的字符串在 R (RODBC) 中返回为 NA