Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Sunday, March 2, 2008

Setting up form for HTML Email and Avoiding HttpRequestValidationException

In a previous post I showed you how to set up .NET for email. But what if I want to send it as HTML. I tried and received this error: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client ...

The validation problem is solved by setting the Page attribute Validation="false". But then when I sent the HTML it was received as text.

The solution is to use IsHTML=true In my example, I used a check box:

protected void btnSend_Click(object sender, EventArgs e)
{
string sTo = "jane@foo.com";
string sFrom = ddlFrom.SelectedItem.Text.Trim();
string sSubject = txtSubject.Text.Trim();
string sBody = txtContent.Text.Trim();
lblStatus.Text = "Sending... please wait";
MailMessage msg = new MailMessage(sFrom, sTo, sSubject, sBody);
msg.IsBodyHtml = chkIsHTML.Checked;
SmtpClient mailObj = new SmtpClient("localHost");
mailObj.Credentials = new NetworkCredential("username", "password");
mailObj.Send(msg);
clearTexts();
lblStatus.Text = "Success: Sent message to Jane at " + DateTime.Now.ToLongTimeString() + " Eastern.";

}



References: http://www.hintsandtips.com/ShowPost/131/hat.aspx
http://aspnet.4guysfromrolla.com/articles/080206-1.aspx

No comments: