通过电子邮件发送带有长名称和重音符号的附件

本文关键字:音符 符号 电子邮件 | 更新日期: 2023-09-27 18:34:40

我们在发送包含带有长名称和重音符号的附件的电子邮件时遇到了问题。

大小或类型似乎不会影响结果(我尝试使用txt和pdf,大小为300kb和3833kb(

经过一番搜索,我发现这篇文章 http://social.msdn.microsoft.com/Forums/en-US/dotnetframeworkde/thread/b6c764f7-4697-4394-b45f-128a24306d55 展示了如何解决这个问题。

如果我的名字真的很长(参见 attachement2 变量名称(,它会起作用。但是在名称稍短的情况下(请参阅附件 1 变量名称(,名称显示为 -=_iso-8859-1_Q_Example,_Example_and_other_

我认为这与拆分编码附件名称中的以下行有关 encodedAttachmentName = encodedAttachmentName.Remove(encodedAttachmentName.Length - encodingtoken.长度,编码令牌。长度(;

这是变量的手表附加 1 - "=?ISO-8859-1?Q?示例=2c_Example_and_other_Repr=e9senta.txt?=">

附加 2 - "=?ISO-8859-1?Q?例子=2c_Example_and_other_Repr=e9sentant_Example_Examp?==?ISO-8859-1?Q?le_Example_Example_Example_Example2.txt?=">

我似乎无法正确发送附件1。.

(我过去确实回答过这个问题 - 带有重音的邮件附件文件名,但我有一个错误,重构并遇到了这个问题..(

这是我能想到的导致错误的最小代码示例。

class Program
{
    static void Main(string[] args)
    {
        Attachment attachment = new Attachment(@"c:'client'temp'Example,_Example_and_other_Représenta.pdf"); //3488kb
        Attachment attachment2 = new Attachment(@"c:'client'temp'Example,_Example_and_other_Représentant_Example_Example_Example_Example_Example_Example.pdf"); //3488kb
        Console.WriteLine(attachment.Name);
        MailMessage mm = new MailMessage();
         mm.From = new MailAddress("toemail");
         mm.To.Add("toemail");
        mm.Subject = "Yo";
        mm.Body = "hello";
        mm.Attachments.Add(CreateAttachment(attachment, attachment.Name)); // =_iso-8859-1_Q_Example,_Example_and_other_
        mm.Attachments.Add(CreateAttachment(attachment2, attachment2.Name));
        SmtpClient smtp = new SmtpClient("SmptServer");
        smtp.Send(mm);
    }
    /// <summary>
    /// This method fixes the name of the attachment to allow accents
    /// </summary>
    /// <remarks>Taken from http://social.msdn.microsoft.com/Forums/en-US/dotnetframeworkde/thread/b6c764f7-4697-4394-b45f-128a24306d55</remarks>
    public static Attachment CreateAttachment(Attachment attachmentFile, string displayName)
    {
        Attachment attachment = attachmentFile;
        attachment.TransferEncoding = TransferEncoding.Base64;
        string tranferEncodingMarker = "Q";
        string encodingMarker = "ISO-8859-1";
        int maxChunkLength = 76;
        attachment.NameEncoding = Encoding.GetEncoding(encodingMarker);
        string encodingtoken = String.Format("=?{0}?{1}?", encodingMarker, tranferEncodingMarker);
        string softbreak = "?=";
        string encodedAttachmentName = encodingtoken;
        encodedAttachmentName = HttpUtility.UrlEncode(displayName, Encoding.Default).Replace("+", " ").Replace("%", "=");
        encodedAttachmentName = SplitEncodedAttachmentName(encodingtoken, softbreak, maxChunkLength, encodedAttachmentName);
        attachment.Name = encodedAttachmentName;
        return attachment;
    }

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

通过电子邮件发送带有长名称和重音符号的附件

原来有一个错误报告 - https://connect.microsoft.com/VisualStudio/feedback/details/696372/filename-encoding-error-when-encoding-utf-8-and-encoded-name-exceeds-the-length-of-a-single-mime-header-line#details

以及一篇知识库文章,用于修复带有特殊字符的长名称的初始问题。http://support.microsoft.com/kb/2402064

安装 kb 后,

我变了

mm.Attachments.Add(CreateAttachment(attachment, attachment.名称((;mm.Attachments.Add(CreateAttachment(attachment2, attachment2.名称((;

回到应该是什么毫米.附件.添加(附件(;mm.附件.添加(附件2(;

一切都奏效了..