如何在`MailMessage`中包含`HTML`标签

问题描述 投票:0回答:2

在我的Asp.Net应用程序中,我正在尝试使用html标记格式化我的外发邮件消息,以便当收件人在其电子邮件收件箱中打开它时它看起来正确。以这个post为例,我遇到了错误。首先,我有一个通用的'Utilities'文件,其中我的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被设置为真。回到我的SendEmail.cs文件中,这个被调用,并且当点击toaddress时插入适当的bodycontentSubmitBtn等等。 bodycontent充满了基于radiobutton选择的自动生成的字符串,然后插入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:[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")
      {
       ...
      }
        ....
    }
 }

这一切都正常,直到我添加<br />标签。然后,按钮onClick函数永远不会触发,我在浏览器控制台中收到以下消息:“

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

c# html asp.net mailmessage system.net.mail
2个回答
1
投票

你设置它的方式看起来像用户正在将代码输入到webform中,这是不行的。

因此,将所有代码从受保护的void uxRadioButtonList_SelectedIndexChanged(对象发送者,EventArgs e)移动到受保护的void ButtonSend_Click(对象发送者,System.EventArgs e),并且不要打扰将文本框的文本设置到电子邮件正文。


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);
...
}
© www.soinside.com 2019 - 2024. All rights reserved.