Sending a mail as both HTML and Plain Text in .net

Viewed 55969

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?

7 Answers

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

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.

Related