中文乱码:
修改后:
但是点击预览后,还是乱码(不知道怎么解决) 下面的最新代码已解决乱码问题:
本來這是修改 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)
{
// 現在創建 multipart/mixed 容器來保存消息文本和
// 圖像附件
var multipart = new Multipart("mixed")
{
body,
//attachment
};
foreach (var attachment in mailMessage.Attachments)
{
// 為位於路徑的文件創建圖像附件
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);
}
// 現在將 multipart/mixed 設置為消息正文
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;
//}
}
}