简体   繁体   中英

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.

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).

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 ? 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.

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 .

Secondly, please port the complete MIME source of the message that you create.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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