1

I need to send the mail in HTML Format. I googled lot but i could not able to get the expected result as html format. Can anyone help what i am missing in this following code to get the html format mail.

C# Code

         MailAddress sender = new MailAddress(ConfigurationManager.AppSettings["smtpUser"]);
             string MailId = Convert.ToString(Session["EmailID"]);
     SmtpClient smtp = new SmtpClient()
                    {
                        Host = ConfigurationManager.AppSettings["smtpServer"],
                        Port = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]),
                        UseDefaultCredentials = false,
                        EnableSsl = true,
                        Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpPass"]),
                        DeliveryMethod = SmtpDeliveryMethod.Network
                    };
  System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
                string text = "<table><tr><td>EmpId</td><td>Emp name</td><td>age</td></tr><tr><td>value</td><td>value</td><td>value</td></tr></table>";
                msg.From = sender;
                msg.To.Add(MailId);
                msg.Body = text;                
                msg.IsBodyHtml = true;
                mail.Subject = "Password Credentials"; 
 smtp.Send(mail.From, mail.To, mail.Subject, msg.Body); 

Output

enter image description here

15
  • 1
    msg.IsBodyHtml = true; should be all you need. What result are you getting? Commented May 8, 2015 at 7:04
  • dotnetfox.com/articles/… Commented May 8, 2015 at 7:07
  • @Willy i am getting the text format result not html format Commented May 8, 2015 at 7:08
  • <html> <head> <title></title> </head> <body> <table> <tr> <td> Dear, <p>yourname</p></td> <td><br /></td> <td> Your Password has been created.</td> </tr> </table> </body> </html> Commented May 8, 2015 at 7:08
  • What mail client are you using? Commented May 8, 2015 at 7:11

1 Answer 1

1

Not fully working example, but a hint is given below:

var msg = new MailMessage();

var htmlBody = AlternateView.CreateAlternateViewFromString(your_html_string_in_variable, Encoding.UTF8,"text/html");

msg.AlternateViews.Add(htmlBody);

IsBodyHtml = true;

... and do the rest of the stuff

Sign up to request clarification or add additional context in comments.

1 Comment

I was add smtp.Send(msg); instead of smtp.Send(mail.From, mail.To, mail.Subject, msg.Body); @vijay

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.