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.
Related
When receiving e-mails sent with System.Net.Mail.MailMessage some recipients seem to have encoding issues with the e-mail. For example charachter ä is displayed as ä. I have set encoding properties:
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
...
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.SubjectEncoding = System.Text.Encoding.UTF8;
What else can I do?
Update:
If I use the Body property the e-mail is displayed correctly, but when I use the AlternateViews property the e-mail is displayed incorrectly.
Complete code:
SmtpClient smtpClient = new SmtpClient("some.host.com");
MailMessage msg = new MailMessage();
msg.To.Add("someone#host.com");
msg.From = new MailAddress("name#host.com", "Name Name");
msg.Subject = "Test";
//Displays as ä
//msg.Body = "ä";
// Displays as ä
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("ä", new ContentType(MediaTypeNames.Text.Html));
msg.AlternateViews.Add(htmlView);
smtpClient.Send(msg);
When sending to Gmail the e-mail is displayed correctly, but when sending to an Exchange server the e-mail is displayed incorrectly. I have tested in .NET 3.5 and 4.5.
Try adding the content type to the Alternative View:
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("ä", new ContentType(MediaTypeNames.Text.Html));
htmlView.ContentType.CharSet = Encoding.UTF8.WebName;
I don't think the BodyEncoding and SubjectEncoding affects reading of messages in any way - it applies to when you send messages and sets the text encoding for the messages and headers when messages are sent.
SmtpClient will read the content-type and charset encoding from messages sent and decode the content of the message and subject according to the encoding it finds in the headers.
If your message is not getting decoded, it seems that the messages are possibly not encoded correctly (or potentially double encoded UTF-8 encoded string getting encoded again by a message encoder), the request headers on the message don't properly match the actual encoding of the message or a charset format that isn't supported in .NET is used.
The only way to know though is to look at the actual raw request trace of a message that fails to see what's actually getting sent.
You might want to set up System.NET tracing for email messages (see http://codepaste.net/j52ktp) or else monitor the TCP/IP stream with something like Wireshark to see what's actually going over the wire and what the headers are instructing the client to do with the data.
FWIW, there's no reason if a message is properly UTF-8 encoded for SmtpClient to not read these messages correctly.
I'm currently using MailSystem.NET SMTPClient to send email, the email content contains Chinese character in both Subject and Body. By the following code, I'm able to set the Email's body to be Encoded correctly, but Subject is still not Encoded and appeared as ???? in Received Email.
ActiveUp.Net.Mail.Message message = new ActiveUp.Net.Mail.Message();
....
message.Charset = "utf-8";
SmtpClient.Send(message, serverName);
Could anyone familiar with MailSystem.Net kindly tell me how to set the subject as encoded in utf-8 as well? Thanks.
I had a similar problem with Polish chars in my email subjects. Solved it this way (VB.NET):
message.Subject = "=?UTF-8?B?" &
Convert.ToBase64String(Encoding.UTF8.GetBytes(outboxMessage.Title)) &
"?="
Now everything works as expected.
I have a requirement where an email is sent to an user and he can directly reply to the email and that email's content gets posted in his account.
Problem is accented characters are not showing up properly when user posts the content from his email. I send email using MailMessage class:
message.BodyEncoding = Encoding.UTF8;
message.SubjectEncoding = Encoding.UTF8;
As you can see both the body and subject are UTF8 encoded. But when I post the message from my email, the accent gets converted to ?. Can anybody tell me what am I missing?
Edit: Does this have anything to do with IsBodyHtml? I haven't set IsBodyHtml to true. Is that required?
I appears that despite using UTF8 for the encoding the default media type used for the body is text/plain--which is ASCII (i.e. the ASCII character set). If you use IsBodyHtml, it will use a media type of text/html which will use the ISO-8859-1 character set.
I am using below code snippet to download HTTP response to local file.
Sometimes my content which is in url is multi-lingual (chinese, japanese, thai data etc.).
I am using ContentEncoding header to specify my content is in UTF-8 encoding, but this has no effect in my local output file which is generating in ASCII. Due to this, multi-lingual data is corrupted. Any help?
using (var webClient = new WebClient())
{
webClient.Credentials = CredentialCache.DefaultCredentials;
webClient.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0");
webClient.Headers.Add(HttpRequestHeader.ContentEncoding, "utf-8");
webClient.DownloadFile(url, #"c:\temp\tempfile.htm");
}
The ContentEncoding header is not used to specify the character set. It's used by the client to say what kind of encoding (compression) it supports.
The client can't tell the server what character set to send. The server sends its data and some header fields that say what character set is being used. Typically it's in the ContentTypeheader and looks like: text/html; charset=UTF-8.
When you're using WebClient, you want to set the Encoding property as a fallback so that if the server doesn't identify the character set, your default will be used. For example:
WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
string s = client.DownloadString(DownloadUrl);
See http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=800 for a bit more information.
I'm trying to figure out how to resolve the problem:
I create the MailMessage object like that and send it:
MailMessage mail = new MailMessage();
mail.From = new MailAddress("me#mycompany.com");
mail.To.Add("login#gmail.com");
mail.Subject = "This is an email";
AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don't support html", null, MediaTypeNames.Text.Plain);
(1) AlternateView htmlView = AlternateView.CreateAlternateViewFromString("Here is an embedded image.<img src=cid:companylogo>", null, "text/html");
LinkedResource logo = new LinkedResource("c:\\cop1.jpg");
logo.ContentId = "companylogo";
htmlView.LinkedResources.Add(logo);
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);
Everything is OK, the mail has the image in the background.
But the problem is, when I change in the paragraph (1) from (click) to (click) everything fails, the image is not recognized and is threated as a attachment !
I think that is caused by the first colon here background-image:cid:companylogo
Is it possible to resolve that ?
Change it to background-image:url("cid:companylogo").
You didn't say what mail client you are viewing this in. Not all mail clients support STYLE or CSS embedded images.
Here is a link regarding CSS support in mail clients:
http://www.campaignmonitor.com/css/