Michael Stum
Michael Stum

Reputation: 181054

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

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?

Upvotes: 54

Views: 60442

Answers (7)

FritsJ
FritsJ

Reputation: 71

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.

Upvotes: 0

Pieter
Pieter

Reputation: 216

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.

Upvotes: 9

user1852503
user1852503

Reputation: 5617

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.

Upvotes: 9

Aim
Aim

Reputation:

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

Upvotes: 37

Aric TenEyck
Aric TenEyck

Reputation: 8032

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.

Upvotes: 11

Michael Stum
Michael Stum

Reputation: 181054

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

Upvotes: 67

Nick Berardi
Nick Berardi

Reputation: 54894

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

Upvotes: 49

Related Questions