Richard77 Richard77 - 29 days ago 14
ASP.NET (C#) Question

Email sent still contains HTML tags.

I'm sending an email to people using a template. The problem is that all the tags are being shown.

<p>Hello,</p>

<p> Please, click the following link to change your password </p>
//...
<p> PLEASE DO NOT REPLY TO THIS MESSAGE</p>


The mail received is displaying exactly the original message with all the tags. Is there a way to make it look like

Here's my code:

string path = System.Web.HttpContext.Current.Server.MapPath("~/path/myTemplate.txt");
String body;
using (StreamReader sr = new StreamReader(path))
{
body = sr.ReadToEnd();
}

body = body.Replace("<%PasswordLink%>", pwdLink);

var mail = new MailMessage("from", "to");
mail.Subject = "Password Reset";
mail.Priority = MailPriority.High;
mail.Body = body;

var client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = "123.45.67.89";
client.Send(mail);

Answer

You need to state that the mail message is HTML. If your using System.Web.Mail.MailMessage then use:

mail.BodyFormat = MailFormat.Html;

If you're using System.Net.Mail.MailMessage then use:

mail.IsBodyHtml = true;