在电子邮件中编码为UTF-8
本文关键字:UTF-8 编码 电子邮件 | 更新日期: 2023-09-27 18:11:13
我有一个客户端正在接收错误编码的电子邮件。我使用System.Net.Mail类并将正文编码设置为UTF-8。我已经做了一些阅读,因为我必须将电子邮件的正文设置为一个字符串,将数据编码为UTF-8字节数组,这对我来说没有任何帮助,因为我必须将其转换回UTF-16字符串。正确吗?
将"agit d'un message de test pour danci.com/"注释为"system system",即"system system",即"system system",即"system system"。
他们看到: *Il s'agit d'un message de test pour déterminer comment le système va gérer les messages envoyés à l'aide des caractères français.
再见journée.*
我在电子邮件上尝试了不同的编码,但我真的怀疑客户端错误地解码了电子邮件,因为我的电子邮件客户端正确显示了这一点。我错过什么了吗?还有什么可以做的吗?
下面的代码SmtpMailService.SendMail(string.Empty, toAddress, "emailaddress@emai.com", "", subject, sb.ToString(), false);
public static bool SendMail(string fromAddress, string toAddress, string ccAddress, string bccAddress, string subject, string body, bool isHtmlBody)
{
if (String.IsNullOrEmpty(toAddress)) return false;
var toMailAddress = new MailAddress(toAddress);
var fromMailAddress = new MailAddress(String.IsNullOrEmpty(fromAddress) ? DefaultFromAddress : fromAddress);
var mailMessage = new MailMessage(fromMailAddress, toMailAddress);
if (!String.IsNullOrEmpty(ccAddress))
{
mailMessage.CC.Add(new MailAddress(ccAddress));
}
if (!String.IsNullOrEmpty(bccAddress))
{
mailMessage.Bcc.Add(new MailAddress(bccAddress));
}
if (!string.IsNullOrEmpty(fromAddress)) mailMessage.Headers.Add("Reply-To", fromAddress);
mailMessage.Subject = subject;
mailMessage.IsBodyHtml = isHtmlBody;
mailMessage.Body = body;
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
var enableSslCfg = ConfigurationManager.AppSettings["Email.Ssl"];
var enableSsl = string.IsNullOrEmpty(enableSslCfg) || bool.Parse(enableSslCfg);
var client = new SmtpClient {EnableSsl = enableSsl};
client.Send(mailMessage);
return true;
}
最终解决我的问题是在备用视图上设置内容类型。只是设置味精。BodyEncoding无法在Outlook中工作,尽管Gmail可以正确显示邮件。
var msg = new MailMessage()
msg.BodyEncoding = Encoding.GetEncoding(1252);
msg.IsBodyHtml = true;
//htmlBody is a string containing the entire body text
var htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, new ContentType("text/html"));
//This does the trick
htmlView.ContentType.CharSet = Encoding.UTF8.WebName;
msg.AlternateViews.Add(htmlView);
您的示例清楚地表明您发送的文本具有UTF-8编码,它被解释为Windows-1252或ISO-8859-1编码(参见几个编码错误的结果,特别是这里表中的最后一行)。
所以你的代码看起来很好,问题可能是客户端只是忽略了邮件正文的编码(例如,因为他们将其嵌入到HTML页面中,而包装HTML使用默认的ISO-8859-1编码)。
尝试将邮件发送到另一个提供商(例如:Gmail)并检查结果。配置良好的提供程序应该处理正文编码。
解决方案1
如果上述假设为真,则必须在客户端修复错误。他们应该处理正文编码,或者如果它总是UTF-8,他们应该简单地在HTML头中添加以下标记:
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
解决方案2
如果您不能不惜任何代价影响客户端,您可能需要尝试使用另一种编码:
mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
请注意你的法语示例将是正确的这种方式;但是,非西欧语言将不能正确显示。
唯一的方法是用合适的字符替换字符,因为你正在使用字符映射和html编码你的代码到不同的垃圾字符
你可以这样修改你的代码:
WebClient myClient = new WebClient();
byte[] requestHTML;
string currentPageUrl = HttpContext.Current.Request.Url.AbsoluteUri;
UTF8Encoding utf8 = new UTF8Encoding();
requestHTML = myClient.DownloadData(currentPageUrl);
var str = utf8.GetString(requestHTML);"
str = str.Replace("’", "'");
尝试:
mailMessage.BodyEncoding = UTF8Encoding.UTF8;
代替:
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
我在邮件中遇到过类似的重音问题。我的解决方案是:
mail.BodyEncoding = System.Text.Encoding.UTF8;