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

C#.Net

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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

Solution 3 - C#

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

Solution 4 - C#

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.

Solution 5 - C#

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.

Solution 6 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichael StumView Question on Stackoverflow
Solution 1 - C#Michael StumView Answer on Stackoverflow
Solution 2 - C#Nick BerardiView Answer on Stackoverflow
Solution 3 - C#AimView Answer on Stackoverflow
Solution 4 - C#Aric TenEyckView Answer on Stackoverflow
Solution 5 - C#user1852503View Answer on Stackoverflow
Solution 6 - C#PieterView Answer on Stackoverflow