Reputation: 1894
I'm sending an Hebrew email to two places, one is Gmail and the other is Outlook.
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;
Upvotes: 1
Views: 2830
Reputation: 1894
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.
Upvotes: 1
Reputation: 66255
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.
Upvotes: 0