简体   繁体   English

使用正确的编码发送电子邮件。 (前景乱码希伯来语)

[英]Sending Email with the right Encoding. (Outlook gibberish hebrew )

I'm sending an Hebrew email to two places, one is Gmail and the other is Outlook. 我将希伯来语电子邮件发送到两个地方,一个是Gmail,另一个是Outlook。

The problem: 问题:

Gmail is working fine every time (they detect the Encoding automatically) but Outlook display the body in gibberish, I can fix it if I change the display encoding from Hebrew(Windows) to Unicode(UTF-8) (when opening the message display in Outlook). Gmail每次都能正常运行(它们会自动检测到编码),但是Outlook以乱码显示邮件正文,如果我将显示编码从Hebrew(Windows)更改为Unicode(UTF-8) (在外表)。

worth mention that the headers and the subject are fine. 值得一提的是标题和主题都很好。

The Question: How can I "tell" Outlook or any other program to view the mail with Unicode(UTF-8) encoding ? 问题:如何“告诉” Outlook或任何其他程序以使用Unicode(UTF-8)编码查看邮件? without the need to do it manually. 无需手动进行。

I try to set the encoding, char-set and what not but I can get it to work. 我尝试设置编码,字符集以及其他内容,但我可以使其正常工作。

Code related: 相关代码:

public static void SendEmail(MailMessage msg )
{
    ContentType mimeType = new System.Net.Mime.ContentType("text/html");
    msg.AlternateViews.Add(System.Net.Mail.AlternateView.CreateAlternateViewFromString(msg.Body, mimeType));

    SmtpClient smtp = new SmtpClient
    {
        Host = "mailgw.netvision.net.il",
        Port = 25,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential(uName,uPass)
    };

    smtp.Send(msg);
}

Here is a couple examples how I tried to play with the Encoding: 这是我尝试如何使用编码的几个示例:

 msg.BodyEncoding = Encoding.ASCII;
 msg.BodyEncoding = Encoding.UTF8;
 msg.BodyTransferEncoding = TransferEncoding.SevenBit;

At the end what make it work is the configuration of the alternative view, like this: 最后,使它起作用的是备用视图的配置,如下所示:

AlternateView view = System.Net.Mail.AlternateView.CreateAlternateViewFromString(msg.Body, Encoding.UTF8, "text/html");
msg.AlternateViews.Add(view);

As you can see I've set the MIME (as I did before) but I also set the Encoding to UTF-8, what solve the problem. 如您所见,我已经设置了MIME(就像以前一样),但是我还将Encoding设置为UTF-8,这解决了问题。

Firstly, it would be a good idea to HTML encode all Unicode characters in the HTML body itself - https://en.wikipedia.org/wiki/Character_encodings_in_HTML . 首先,对HTML正文本身中的所有Unicode字符进行HTML编码是一个好主意-https: //en.wikipedia.org/wiki/Character_encodings_in_HTML

Secondly, please port the complete MIME source of the message that you create. 其次,请移植您创建的消息的完整MIME源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM