在发送包含阿拉伯字符(显示为????)的附件文件时出现问题用于文件名。但是,如果文件名是英语,则不会出现此问题。我尝试使用UTF-8来回编码,但无法正常工作。
private static void GenerateAndSendMail(string subject, string body, List<string> filesFullPath = null)
{
try
{
var mailMessage = new MailMessage();
mailMessage.From = new MailAddress(MyAppConfigService.CurrentUser.Email);
mailMessage.Subject = subject;
mailMessage.IsBodyHtml = true;
mailMessage.Body = body;
mailMessage.SubjectEncoding = Encoding.Unicode;
mailMessage.HeadersEncoding = Encoding.Unicode;
mailMessage.BodyEncoding = Encoding.Unicode;
if (filesFullPath != null && filesFullPath.Count > 0)
{
foreach (var item in filesFullPath)
{
var Attachment = new Attachment(item);
Attachment.NameEncoding = Encoding.Unicode;
mailMessage.Attachments.Add(Attachment);
}
}
//save the MailMessage to the filesystem
var filename = Path.GetTempPath() + "mymessage.eml";
mailMessage.Save(filename);
//Open the file with the default associated application registered on the local machine
Process.Start(filename);
}
catch (Exception ex)
{
throw ex;
}
}
答案 0 :(得分:2)
经过6个小时的互联网研究,并尝试使用不同类型的编码(例如UTF-8,UTF-16和Unicode),我找到了解决方案。
问题出在附件编码中,无法正确编码阿拉伯字符,并且编码结果错误。解决方案是在NameEncoding下添加以下行代码:
Attachment.ContentType.CharSet = "UTF-8";