如何在 `MailMessage` 中包含 `HTML` 标签
How to include `HTML` tags in `MailMessage`
在我的 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
被设置为 true。回到我的 SendEmail.cs
文件,这会被调用,当 SubmitBtn
被点击时,相应的 toaddress
、bodycontent
等被插入。 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: 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?
".
您的问题是您通过网页上控件的 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);
...
}
您设置它的方式看起来像是用户在网络表单中输入代码,这是不行的。
所以将所有代码从 protected void uxRadioButtonList_SelectedIndexChanged(object sender, EventArgs e) 移动到 protected void ButtonSend_Click(object sender, System.EventArgs e) 并且不要设置文本框的文本到电子邮件正文。
日
在我的 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
被设置为 true。回到我的 SendEmail.cs
文件,这会被调用,当 SubmitBtn
被点击时,相应的 toaddress
、bodycontent
等被插入。 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: 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? ".
您的问题是您通过网页上控件的 Text
属性 传递 HTML 字符串。 Web 服务器正在拒绝看起来像是将 HTML 注入您的页面的尝试。
一种解决方案是将 TextBoxSubject.Text
保留为纯文本,并在 ButtonSend_Click
protected void ButtonSend_Click(object sender, System.EventArgs e)
{
....
Utility.sendEmail(userEmail, SpellTextBoxNotify.Text, newlineToBR(TextBoxSubject.Text), ccClean);
...
}
您设置它的方式看起来像是用户在网络表单中输入代码,这是不行的。
所以将所有代码从 protected void uxRadioButtonList_SelectedIndexChanged(object sender, EventArgs e) 移动到 protected void ButtonSend_Click(object sender, System.EventArgs e) 并且不要设置文本框的文本到电子邮件正文。
日