如何在MailMessage中包含HTML标签

时间:2019-03-21 15:40:07

标签: c# html asp.net mailmessage system.net.mail

在我的Asp.Net应用程序中,我试图使用html标签格式化我的外发邮件,以便收件人在其电子邮件收件箱中打开邮件时看起来正确。以这个post为例,我遇到了错误。首先,我有一个通用的“实用程序”文件,其中我的sendMail函数是使用

配置的

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();
       ....
       }
    }
 }

因此,我的mailMessage.IsBodyHtml设置为true。回到我的SendEmail.cs文件中,将调用该文件,并在单击toaddress时插入相应的bodycontentSubmitBtn等。 bodycontent会根据单选按钮的选择填充自动生成的字符串,然后插入textarea。从textarea中分配了bodycontent。直到我在邮件中添加了`标签,它才能正常工作。

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:steve.mcmaster@ideationinc.com**" +
       "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")
      {
       ...
      }
        ....
    }
 }

在添加<br />标记之前,所有方法都可以正常工作。然后,按钮onClick功能永不触发,并且我在浏览器控制台中收到以下消息:“

  

Sys.WebForms.PageRequestManagerServerErrorException:一个潜在的   从客户端检测到危险的Request.Form值   (ctl00 $ ContentPlaceHolder1 $ SpellTextBoxNotify =“ ... tesinc.com
  关于如何克服此问题并使html标记起作用的任何建议?   “

2 个答案:

答案 0 :(得分:1)

您的问题是,您正在通过网页上控件的Text属性来传递HTML字符串。 Web服务器拒绝了将HTML注入页面的尝试。

一种解决方案是将TextBoxSubject.Text保留为纯文本,并将换行符转换为ButtonSend_Click中的HTML

protected void ButtonSend_Click(object sender, System.EventArgs e)
{
....
  Utility.sendEmail(userEmail, SpellTextBoxNotify.Text, newlineToBR(TextBoxSubject.Text), ccClean);
...
}

答案 1 :(得分:1)

设置方式似乎是用户在向Web表单中输入代码,这是不可以的。

因此,将所有代码从受保护的void uxRadioButtonList_SelectedIndexChanged(对象发件人,EventArgs e)移动到受保护的void ButtonSend_Click(对象发件人,System.EventArgs e),而不必费心将文本框的文本设置为电子邮件正文。

Th