Organizational Research By

Surprising Reserch Topic

send html email via c sharp with smtpclient


send html email via c sharp with smtpclient  using -'c#,asp.net,email,smtpclient'

How do I send an HTML email? I use the code in this answer to send emails with SmtpClient, but they're always plain text, so the link in the example message below is not formatted as such.

<p>Welcome to SiteName. To activate your account, visit this URL: <a href="http://SiteName.com/a?key=1234">http://SiteName.com/a?key=1234</a>.</p>


How do I enable HTML in the e-mail messages I send?
    

asked Sep 24, 2015 by santosh soni
0 votes
17 views



Related Hot Questions

3 Answers

0 votes

This is what I do:

MailMessage mail = new MailMessage(from, to, subject, message);
mail.IsBodyHtml = true;
SmtpClient client = new SmtpClient("localhost");
client.Send(mail);

Note that I set the mail message html to true: mail.IsBodyHtml = true;

answered Sep 24, 2015 by sanjaypal1983
0 votes

IsBodyHtml = true is undoubtedly the most important part.

But if you want to provide an email with both a text/plain part and a text/html part composed as alternates, it is also possible using the AlternateView class:

MailMessage msg = new MailMessage();
AlternateView plainView = AlternateView
     .CreateAlternateViewFromString("Some plaintext", Encoding.UTF8, "text/plain");
// We have something to show in real old mail clients.
msg.AlternateViews.Add(plainView); 
string htmlText = "The fancy part.";
AlternateView htmlView = 
     AlternateView.CreateAlternateViewFromString(htmlText, Encoding.UTF8, "text/html");
msg.AlternateViews.Add(htmlView); // And a html attachment to make sure.
msg.Body = htmlText;  // But the basis is the html body
msg.IsBodyHtml = true; // But the basis is the html body
answered Sep 24, 2015 by rajesh
0 votes

i have an idea , you can add a check box to your project for sending email as html as an option for user , and add this code to enable it :

         MailMessage mail = new MailMessage(from, to, subject, message);

         if(checkBox1.CheckState == CheckState.Checked )
           {
               mail.IsBodyHtml = true;
           }

         SmtpClient client = new SmtpClient("localhost");

         client.Send(mail);
answered Sep 24, 2015 by manju bhargava

...