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.
Related
I'm very confused about a problem with sending multipart emails in ASP.NET. I'm using code which I'm sure has worked before, but the received email appears to have no body.
The key bit of code is this:
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainBody, null, MediaTypeNames.Text.Plain));
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html));
I can verify that plainBody is valid text and htmlBody is valid HTML. Apart from this I'm creating a very simple MailMessage with subject, from and to. I'm not setting any other properties of MailMessage and I'm sending with the standard System.Net.Mail.SmtpClient using Google credentials I've used many times before.
When the email arrives it appears to be completely empty.
If I replace the above two lines with the following (no other changes) then the HTML email arrives correctly.
message.Body = htmlBody;
message.IsBodyHtml = true;
So what could be the cause of my empty emails? I've tried simplifying the HTML right down to a single word wrapped in <b> tags so it's not related to the body content and, as I say, I've used similar code many times before with no problems. I've tested receipt of the emails in GMail and Office 365, both with same result.
Suggestions very welcome.
Why don't you break that piece of the code into two objects.
AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, mimeType);
message.AlternateViews.Add(alternate);
Put a break point in the second line and see whats in the alternate object. Maybe that might give you a little more insight into what's going on in your code.
You cannot set the mail with two different bodies. Since one of the parts is HTML, you need to treat all mail as HTML, just appending the plaintext normally - since the plaintext has no HTML encode inside, the text will appear as plaintext.
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.
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'm sending mail from my C# Application, using the SmtpClient. Works great, but I have to decide if I want to send the mail as Plain Text or HTML. I wonder, is there a way to send both? I think that's called multipart.
I googled a bit, but most examples essentially did not use SmtpClient but composed the whole SMTP-Body themselves, which is a bit "scary", so I wonder if something is built in the .net Framework 3.0?
If not, is there any really well used/robust Third Party Library for sending e-Mails?
The MSDN Documentation seems to miss one thing though, I had to set the content type manually, but otherwise, it works like a charm :-)
MailMessage msg = new MailMessage(username, nu.email, subject, body);
msg.BodyEncoding = Encoding.UTF8;
msg.SubjectEncoding = Encoding.UTF8;
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlContent);
htmlView.ContentType = new System.Net.Mime.ContentType("text/html");
msg.AlternateViews.Add(htmlView);
What you want to do is use the AlternateViews property on the MailMessage
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.alternateviews.aspx
Just want to add that you can use defined constants MediaTypeNames.Text.Html and MediaTypeNames.Text.Plain instead of "text/html" and "text/plain", which is always a preferable way. It's in System.Net.Mime namespace.
So in the example above, it would be:
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlContent, null, MediaTypeNames.Text.Html);
I'm just going to put a note here for anyone that's having problems and finds their way to this page - sometimes, Outlook SMTP servers will reconvert outgoing email. If you're seeing your plain-text body vanish entirely, and nothing but base64-encoded attachments, it might be because your server is reencoding the email. Google's SMTP server does not reencode email - try sending through there and see what happens.
On top of using AlternateViews views to add both the html and the plain text view, make sure you are not also setting the body of the Mail Message object.
// do not do this:
var msg = new MailMessage(model.From, model.To);
msg.Body = compiledHtml;
As it will make your email contain the html content in both views, overriding the alternative views.
For the people(like me) who've had the problem of gmail displaying the plaintext part instead of the html part.
Gmail seems to always display the last part in your message.
So if you've added the html part before your plain text part chances are gmail will always show the plain text variant.
To fix this you can simply add the plain text part before your html part.
For anyone who bumped into this issue you might want to check if you have preheader tags in your html.
In my html I've added a tag with a phrase of "Activate your client admin account by clicking the link.".
It seems like gmail is flagging the phrase "clicking the link" after removing it, all my emails that has been sent, are going straight to the inbox.