myesn

myEsn2E9

hi
github

ABP: MailKit 附件中文名称乱码

中文乱码:
image

修改后:
image
但是点击预览后,还是乱码(不知道怎么解决) 下面的最新代码已解决乱码问题:
image

本来这是修改 MailKit 的附件参数,但 ABP 什么都封装了一遍,所以这篇文章又跟 ABP 有点关系了,就文章分类方面都属于 ABP。

using MiaoXin.BackgroundJobs;
using MiaoXin.Utils;
using Microsoft.Extensions.Options;
using MimeKit;
using MimeKit.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.Emailing;
using Volo.Abp.Emailing.Smtp;
using Volo.Abp.MailKit;
using Volo.Abp.MultiTenancy;

namespace ?
{
    /// <summary>
    /// 重写 MailKitSmtpEmailSender 以解决中文附件名称乱码问题
    /// </summary>
    public class ?MailKitSmtpEmailSender : MailKitSmtpEmailSender
    {
        public ?MailKitSmtpEmailSender(
            ISmtpEmailSenderConfiguration smtpConfiguration,
            IBackgroundJobManager backgroundJobManager,
            IOptions<AbpMailKitOptions> abpMailKitConfiguration)
            : base(smtpConfiguration, backgroundJobManager, abpMailKitConfiguration)
        {
        }

        protected override async Task SendEmailAsync(MailMessage mail)
        {
            // 默认的行为,中文附件名称在 QQ 邮箱中显示乱码
            /*return base.SendEmailAsync(mail)*/;

            using (var client = await BuildClientAsync())
            {
                //var message = MimeMessage.CreateFromMailMessage(mail);
                var message = BuildMimeMessageFromMailMessage(mail);
                message.MessageId = MimeUtils.GenerateMessageId();

                await client.SendAsync(message);
                await client.DisconnectAsync(true);
            }
        }

        private MimeMessage BuildMimeMessageFromMailMessage(MailMessage mailMessage)
        {
            var from = mailMessage.From;
            var to = mailMessage.To.First();

            var message = new MimeMessage();
            message.From.Add(new MailboxAddress(from.DisplayName, from.Address));
            message.To.Add(new MailboxAddress(to.DisplayName, to.Address));
            message.Subject = mailMessage.Subject;
            
            var body = new TextPart(mailMessage.IsBodyHtml ? "html" : "plain")
            {
                Text =  mailMessage.Body
            };

            if(mailMessage.Attachments is not null && mailMessage.Attachments.Count > 0)
            {
                // now create the multipart/mixed container to hold the message text and the
                // image attachment
                var multipart = new Multipart("mixed")
                {
                    body,
                    //attachment
                };

                foreach (var attachment in mailMessage.Attachments)
                {
                    // create an image attachment for the file located at path
                    var attachmentMimePart = new MimePart(attachment.ContentType.MediaType)//"image", "jpg"
                    {
                        Content = new MimeContent(attachment.ContentStream, ContentEncoding.Default),
                        ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
                        ContentTransferEncoding = ContentEncoding.Base64,
                        FileName = attachment.Name //Path.GetFileName(filePath)
                    };
                    multipart.Add(attachmentMimePart);
                }

                // now set the multipart/mixed as the message body
                message.Body = multipart;
            }
            else
            {
                message.Body = body;
            }

            return message;
        }

        //protected override MailMessage BuildMailMessage(string? from, string to, string? subject, string? body, bool isBodyHtml = true, AdditionalEmailSendingArgs? additionalEmailSendingArgs = null)
        //{
        //    //return base.BuildMailMessage(from, to, subject, body, isBodyHtml, additionalEmailSendingArgs);
        //    var message = from == null
        //        ? new MailMessage { To = { to }, Subject = subject, Body = body, IsBodyHtml = isBodyHtml }
        //        : new MailMessage(from, to, subject, body) { IsBodyHtml = isBodyHtml };

        //    if (additionalEmailSendingArgs != null)
        //    {
        //        if (additionalEmailSendingArgs.Attachments != null)
        //        {
        //            foreach (var attachment in additionalEmailSendingArgs.Attachments.Where(x => x.File != null))
        //            {
        //                var fileStream = new MemoryStream(attachment.File!);
        //                fileStream.Seek(0, SeekOrigin.Begin);

        //                // 解决附件名称中文乱码问题:
        //                // https://www.cnblogs.com/rocketRobin/p/8337055.html
        //                // https://github.com/jstedfast/MailKit/issues/264
        //                // https://github.com/jstedfast/MailKit/issues/277

        //                #region 没有用,名字变成英文和数字了

        //                // 需要在 DomainModule 的 ConfigureServices 中注册缺失的字符集 Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
        //                //var attachmentItem = Attachment.CreateAttachmentFromString(
        //                //    Encoding.UTF8.GetString(fileStream.ToArray()),
        //                //    attachment.Name!,
        //                //    Encoding.GetEncoding("GB18030"), //Encoding.UTF8,
        //                //    System.Net.Mime.MediaTypeNames.Image.Jpeg); 

        //                #endregion

        //                var attachmentItem = new Attachment(fileStream, attachment.Name);// , System.Net.Mime.MediaTypeNames.Application.Rtf
        //                var charset = "GB18030";
        //                attachmentItem.ContentType.CharSet = charset;
        //                //// 附件显示名称
        //                //attachmentItem.ContentType.Name = attachment.Name;
        //                // 附件下载名称
        //                // !!中文还是会乱码,为了解决这个问题,先使用固定的英文文件名称,否则下载的后的文件名后缀名错误无法打开,比如 "璺ㄥ鐢靛晢2024骞�堟椿鍔ㄩ鍛�jpg"
        //                // 我靠,没想到QQ邮箱显示名称、详情左上角文件名、下载文件名都是用的 ContentDisposition!.FileName
        //                // 已咨询作者,https://github.com/jstedfast/MailKit/issues/264#issuecomment-2178099225
        //                attachmentItem.ContentDisposition!.FileName = attachment.Name!; // "attachment" + Path.GetExtension(attachment.Name);

        //                message.Attachments.Add(attachmentItem);
        //            }
        //        }

        //        if (additionalEmailSendingArgs.CC != null)
        //        {
        //            foreach (var cc in additionalEmailSendingArgs.CC)
        //            {
        //                message.CC.Add(cc);
        //            }
        //        }
        //    }

        //    return message;
        //}
    }
}


Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.