在电子邮件中编码为UTF-8 [英] encoding to UTF-8 in email

查看:529
本文介绍了在电子邮件中编码为UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户接收的电子邮件编码不正确.我正在使用System.Net.Mail类,并将主体编码设置为UTF-8.我已经做了一些阅读,并且由于我必须将电子邮件的正文设置为将数据编码为UTF-8字节数组的字符串,因此对我实际上没有任何帮助,因为我必须将其转换回为UTF- 16.正确吗?

I have a client that is receiving email incorrectly encoded. I am using the System.Net.Mail class and setting the body encoding to UTF-8. I have done a bit of reading and since I have to set the body of the email as a string encoding the data to a UTF-8 byte array really does nothing for me since I have to convert is back to a string that is UTF-16. Correct?

我发送的时间: 法兰西岛消息使者们的消息传来了测试的确定性评论. Merci et bonnejournée.

when I send: 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. Merci et bonne journée.

他们看到了: * Il s'agit d'un message de test pour dterminer comment lesystémevagérerles messagesenvoyésàl'aide descaractéresfrançais.

They see: *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.

Merci et bonnejournée.*

Merci et bonne journée.*

我在电子邮件上尝试了不同的编码,但是我真的怀疑客户端错误地解码了电子邮件,因为我的电子邮件客户端正确显示了该密码.我想念什么吗?还有什么可以做的吗?

I have tried different encodings on the email, but I am really suspecting that the client is incorrectly decoding the email since my email client displays this correctly. Am I missing something? Is there something else that can be done?

下面的代码

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

推荐答案

最终解决我问题的方法是在备用视图上设置contenttype.尽管Gmail正确显示了电子邮件,但仅在Outlook中(仅在其中)设置msg.BodyEncoding是无效的.

What finally solved my problem was setting the contenttype on the alternate view. Just setting the msg.BodyEncoding didn't work in (among others) Outlook although Gmail displayed the email correctly.

    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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆