Search code examples
c#htmlasp.netmailmessagesystem.net.mail

How to include `HTML` tags in `MailMessage`


In my Asp.Net application, I am trying to format my outgoing mail message with html tags so that it looks correct when the recipient opens it in their email inbox. Using this post as an example, I have run into errors. First, I have a general 'Utilities' file where my sendMail function is configured with the

Utilities.cs

 public static class Utility
{
   public static void sendEmail(string toaddress, string bodycontent, string subjectTxt, string ccAddress)
  {
      string sendEmail = string.Empty;
      try
      {
        System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
        mailMessage.IsBodyHtml = true;
        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
       ....
       }
    }
 }

So, my mailMessage.IsBodyHtml is being set to true. Back in my SendEmail.cs file, this is called and the appropriate toaddress, bodycontent, etc... are plugged in when the SubmitBtn is clicked. The bodycontent is filled with an autogenerated string based on a radiobutton choice and then plugged into textarea. From the textarea the bodycontent is assigned. It works fine until I added the ` tags in the message.

SendEmail.cs

protected void ButtonSend_Click(object sender, System.EventArgs e)
{
    ....
  Utility.sendEmail(userEmail, SpellTextBoxNotify.Text, TextBoxSubject.Text, ccClean);
...
}
protected void uxRadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
{
   DataTable emailInfo = _emailDtr.GetTicketInfoByTicketNumber(Session["TicketNumber"].ToString());
   string email = emailInfo.Rows[0]["RequestorEmail"].ToString();
   string name = emailInfo.Rows[0]["RequestorName"].ToString();
   string phone = emailInfo.Rows[0]["RequestorPhone"].ToString();
             .....
   if (uxRadioButtonList.SelectedItem.Text == "Forward")
   {
     TextBoxTo.Text = userEmail;
     TextBoxSubject.Text = "BCA Helpline Request Detailed Response : " + s;
     string new_message = "Please email:[email protected]**" +
       "Dear Steve,*<br />" +
       "This BC Helpline inquiry has been flagged as a Tier 4 request.**<br /><br />" +
       "Please see the inquiry below. This ticket will be closed out in our system. Please let us know if you’d like  * <br />" +
       "us to respond.   **<br />" +
       "Contact info: *<br />" +
        name + "*<br />" +
        "P: " + phone + "*<br />" +
        email + "**<br /><br />" +
        "Regards, **<br /><br />" + name + "(CTR) *" + "Dewberry Consultants LLC, Contractor<br />*Federal Emergency Management Agency*<br />FEMA HQ*<br />500 C. St., SW*Washington, D.C. 20472<br />*Email: " + userEmail;
        new_message = new_message.Replace("*", "" + System.Environment.NewLine);
        SpellTextBoxNotify.Text = new_message;
      }

      else if (uxRadioButtonList.SelectedItem.Text == "Interim Response")
      {
       ...
      }
        ....
    }
 }

This all works fine until I add the <br /> tags. Then, the button onClick function never fires and I get the following message in my browser console: "

Sys.WebForms.PageRequestManagerServerErrorException: A potentially dangerous Request.Form value was detected from the client (ctl00$ContentPlaceHolder1$SpellTextBoxNotify="...tesinc.com
Any suggestions on how to overcome this issue and get the html tags to work? ".


Solution

  • The way you have it set up it looks like the user is entering code into the webform which is a no no.

    So move all that code from protected void uxRadioButtonList_SelectedIndexChanged(object sender, EventArgs e) to protected void ButtonSend_Click(object sender, System.EventArgs e) and don't bother setting the textbox's text to the email body.

    Th