Marco2752

导航

 

机器翻译,将就着看哈)。

  好吧,我败了。只能怪我爱得太深了,竟不曾怀疑过你。

  可我还是不想升级Framework,生产环境,可不是闹着玩的呢。 

  试着用网友ZYD的方法,把文件名先编码拆分,问题可以得到解决:

 

方法类

public class MailHelper
    {
        public static Attachment CreateAttachment(Stream attachmentFile, string displayName, string attachmentFilePath)
        {
            var currentCulture = Thread.CurrentThread.CurrentCulture;//.net4.0 bug,
            var attachment = new Attachment(attachmentFile, displayName);
            try
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                attachment.ContentType = new ContentType();
                attachment.ContentDisposition.CreationDate = File.GetCreationTime(attachmentFilePath);
                attachment.ContentDisposition.ModificationDate = File.GetLastWriteTime(attachmentFilePath);
                attachment.ContentDisposition.ReadDate = File.GetLastAccessTime(attachmentFilePath);
                attachment.TransferEncoding = TransferEncoding.Base64;
                attachment.NameEncoding = Encoding.UTF8;
                string encodedAttachmentName = Convert.ToBase64String(Encoding.UTF8.GetBytes(displayName));
                encodedAttachmentName = SplitEncodedAttachmentName(encodedAttachmentName);
                attachment.Name = encodedAttachmentName;
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = currentCulture;
            }
            return attachment;
        }

        public static Stream WriteFileToMemory(string filePath)
        {
            var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            return fileStream;
        }

        public static string SplitEncodedAttachmentName(string encoded)
        {
            const string encodingtoken = "=?UTF-8?B?";
            const string softbreak = "?=";
            const int maxChunkLength = 30;
            int splitLength = maxChunkLength - encodingtoken.Length - (softbreak.Length * 2);
            IEnumerable<string> 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;
        }

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

 

使用方法

Attachment attachment = null;
//添加附件
attachment = MailHelper.CreateAttachment(MailHelper.WriteFileToMemory(file), name, file);
message.Attachments.Add(attachment);

 

原文地址:http://www.myexception.cn/c-sharp/2012056.html

posted on 2018-01-11 14:34  Marco2752  阅读(173)  评论(0编辑  收藏  举报