将验证码集成到 .NET 电子邮件表单中
Integrating Captcha Into .NET Email Form
我在将验证码集成到我正在使用的表单中时遇到问题。我几乎没有 .NET 经验,所以非常感谢任何帮助!我终于找到了一个有效的联系表格(当使用我个人域中的电子邮件地址时 - 由于某种原因似乎不适用于 gmail)。
我成功地设计了它的样式和功能,但它需要一个验证码来阻止垃圾邮件流入。我在 http://www.tipstricks.org/ 上找到了一个预制的验证码表单,它本身就很好用,但我真的不是确定如何将其与下面的表格集成。我可以让 CAPTCHA 自己工作,让表单自己工作,但我似乎无法弄清楚如何让 CAPTCHA 验证并提交表单。
就像现在一样,当我填写表格并提交时,我收到一条绿色确认消息,表示消息已发送,但一条红色消息表示验证码失败。无论在 CAPTCHA 字段中填写什么内容,消息都会通过。
坦率地说,我对任何验证码都很满意,而不仅仅是我下载的代码。如果你们有更好的代码或将验证码合并到此表单中的方法,我会很乐意改用它。
以下是我在表单中使用的内容。如果我需要提供更多详细信息,请告诉我。正如我所说,我们将不胜感激任何帮助!
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="custom_aspnet_test" %>
<%@ Import Namespace="System.Web.Mail" %>
<script runat="server">
Sub btnSubmit_Click(sender as object, e as EventArgs)
Dim objEMail As New MailMessage()
objEMail.To = "myemail@email.com"
objEmail.From = txtFrom.Text
objEmail.Subject = "Contact Form"
objEmail.Body = "IP: " & Request.ServerVariables("REMOTE_ADDR") & vbcrlf & "Date/Time: " &Now & vbcrlf & "Name: " &txtName.Text & vbcrlf & "Email: " &txtFrom.Text & vbcrlf & "Phone: " &txtPhone.Text & vbcrlf & "Comments: " &txtBody.Text
objEmail.Priority = MailPriority.High
SmtpMail.SmtpServer = "smtp.address.com"
try
SmtpMail.Send(objEMail)
Response.Write("<font color='green'><strong>Thank you for contacting us. We will respond shortly.</strong></font>")
catch exc as Exception
Response.Write("<font color='red'><strong>Send failure: </strong></font>" + exc.ToString())
End Try
End Sub
</script>
<html>
<head>
<title>Contact Form</title>
</head>
<body>
<form id="ContactMain" runat="server">
<table border="0" align="center" cellpadding="3" cellspacing="0" width="100%">
<tr>
<td valign="top" class="form-input" colspan="2">
<label class="form-label-left">Name</label>
<asp:TextBox runat="server" class="form-textbox" ID="txtName"></asp:TextBox>
</td>
</tr>
<tr>
<td valign="top" class="form-input" colspan="2">
<label class="form-label-left">Email <font color="red">*</font></label>
<asp:TextBox runat="server" class="form-textbox" ID="txtFrom" ></asp:TextBox>
<asp:RequiredFieldValidator runat=server
ControlToValidate=txtFrom
ErrorMessage="Please enter a valid Email address">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td valign="top" class="form-input" colspan="2">
<label class="form-label-left">Phone</label>
<asp:TextBox runat="server" class="form-textbox" ID="txtPhone" ></asp:TextBox>
</td>
</tr>
<tr>
<td valign="top" class="form-input">
<label class="form-label-left">Comments <font color="red">*</font></label>
<asp:TextBox runat="server" TextMode="MultiLine" rows="6" cols="40" class="form-textarea" ID="txtBody"></asp:TextBox>
<asp:RequiredFieldValidator runat=server
ControlToValidate=txtBody
ErrorMessage="Please let us know what we can help you with">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2" valign="top" class="form-line">
<asp:PlaceHolder ID="phTest" runat="server" Visible="true">
<img src="captcha.ashx" id="imgCaptcha" /> <a href="javascript:void(0);" onclick="RefreshImage('imgCaptcha');">Refresh</a><br />
Write the characters in the image above<br />
<asp:TextBox ID="txtCaptcha" runat="server"></asp:TextBox><br />
<asp:Button Runat = server ID = btnSubmit OnClick = btnSubmit_Click Text = "Submit" class="form-submit-button"></asp:Button>
</asp:PlaceHolder>
<asp:PlaceHolder ID="phResult" runat="server" Visible="false">
<p><asp:Label ID="lblResult" runat="server" Font-Bold="true"></asp:Label></p>
<asp:Button ID="btnRetry" runat="server" Text="Take another test" />
</asp:PlaceHolder>
</td>
</tr>
</table>
</form>
</body>
</html>
这里是文件 "CodeFile" 引用的代码:
Partial Class custom_aspnet_test
Inherits System.Web.UI.Page
Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
If IsNothing(Session("ASPCAPTCHA")) OrElse Session("ASPCAPTCHA").ToString.Trim.Length = 0 Then
lblResult.Text = "This test has expired."
lblResult.ForeColor = Drawing.Color.Red
Else
Dim TestValue As String = txtCaptcha.Text.Trim.ToUpper(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))
If StrComp(TestValue, Session("ASPCAPTCHA").ToString.Trim, CompareMethod.Text) = 0 Then
lblResult.Text = "CAPTCHA PASSED"
lblResult.ForeColor = Drawing.Color.Green
Else
lblResult.Text = "CAPTCHA FAILED"
lblResult.ForeColor = Drawing.Color.Red
End If
'//IMPORTANT: You must remove session value for security after the CAPTCHA test//
Session.Remove("ASPCAPTCHA")
'//////////
End If
phTest.Visible = False
phResult.Visible = True
End Sub
Protected Sub btnRetry_Click(sender As Object, e As System.EventArgs) Handles btnRetry.Click
txtCaptcha.Text = ""
phTest.Visible = True
phResult.Visible = False
End Sub
End Class
问题是您在两个不同的地方响应 btnSubmit。其中之一位于 .aspx 文件的脚本标记中。那就是您发送电子邮件的地方。另一个地方是 btnSubmit_Click 方法中的代码隐藏文件。那就是你检查验证码的地方。
您已连接按钮以触发两个事件。他们两个都在开火,这就是为什么你总是得到两个结果。
您需要将邮箱代码移至btnSubmit_Click验证码成功后的代码
这是更新后的代码,其中包含验证码通过时调用的发送电子邮件方法。此外,您需要从 .aspx 文件中删除发送电子邮件代码,并从 btnSubmit 按钮声明中删除此属性:OnClick = btnSubmit_Click
Imports System.Web.Mail
Partial Class test
Inherits System.Web.UI.Page
Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
If IsNothing(Session("ASPCAPTCHA")) OrElse Session("ASPCAPTCHA").ToString.Trim.Length = 0 Then
lblResult.Text = "This test has expired."
lblResult.ForeColor = Drawing.Color.Red
Else
Dim TestValue As String = txtCaptcha.Text.Trim.ToUpper(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))
If StrComp(TestValue, Session("ASPCAPTCHA").ToString.Trim, CompareMethod.Text) = 0 Then
lblResult.Text = "CAPTCHA PASSED"
lblResult.ForeColor = Drawing.Color.Green
' The CAPTCHA passes so send the email
Me.SendEmail()
Else
lblResult.Text = "CAPTCHA FAILED"
lblResult.ForeColor = Drawing.Color.Red
End If
'//IMPORTANT: You must remove session value for security after the CAPTCHA test//
Session.Remove("ASPCAPTCHA")
'//////////
End If
phTest.Visible = False
phResult.Visible = True
End Sub
''' <summary>
''' This was the code you had in the aspx file.
''' </summary>
''' <remarks></remarks>
Private Sub SendEmail()
Dim objEMail As New MailMessage()
objEMail.To = "myemail@email.com"
objEMail.From = txtFrom.Text
objEMail.Subject = "Contact Form"
objEMail.Body = "IP: " & Request.ServerVariables("REMOTE_ADDR") & vbCrLf & "Date/Time: " & Now & vbCrLf & "Name: " & txtName.Text & vbCrLf & "Email: " & txtFrom.Text & vbCrLf & "Phone: " & txtPhone.Text & vbCrLf & "Comments: " & txtBody.Text
objEMail.Priority = MailPriority.High
SmtpMail.SmtpServer = "smtp.address.com"
Try
SmtpMail.Send(objEMail)
Response.Write("<font color='green'><strong>Thank you for contacting us. We will respond shortly.</strong></font>")
Catch exc As Exception
Response.Write("<font color='red'><strong>Send failure: </strong></font>" + exc.ToString())
End Try
End Sub
Protected Sub btnRetry_Click(sender As Object, e As System.EventArgs) Handles btnRetry.Click
txtCaptcha.Text = ""
phTest.Visible = True
phResult.Visible = False
End Sub
End Class
我在将验证码集成到我正在使用的表单中时遇到问题。我几乎没有 .NET 经验,所以非常感谢任何帮助!我终于找到了一个有效的联系表格(当使用我个人域中的电子邮件地址时 - 由于某种原因似乎不适用于 gmail)。
我成功地设计了它的样式和功能,但它需要一个验证码来阻止垃圾邮件流入。我在 http://www.tipstricks.org/ 上找到了一个预制的验证码表单,它本身就很好用,但我真的不是确定如何将其与下面的表格集成。我可以让 CAPTCHA 自己工作,让表单自己工作,但我似乎无法弄清楚如何让 CAPTCHA 验证并提交表单。
就像现在一样,当我填写表格并提交时,我收到一条绿色确认消息,表示消息已发送,但一条红色消息表示验证码失败。无论在 CAPTCHA 字段中填写什么内容,消息都会通过。
坦率地说,我对任何验证码都很满意,而不仅仅是我下载的代码。如果你们有更好的代码或将验证码合并到此表单中的方法,我会很乐意改用它。
以下是我在表单中使用的内容。如果我需要提供更多详细信息,请告诉我。正如我所说,我们将不胜感激任何帮助!
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="custom_aspnet_test" %>
<%@ Import Namespace="System.Web.Mail" %>
<script runat="server">
Sub btnSubmit_Click(sender as object, e as EventArgs)
Dim objEMail As New MailMessage()
objEMail.To = "myemail@email.com"
objEmail.From = txtFrom.Text
objEmail.Subject = "Contact Form"
objEmail.Body = "IP: " & Request.ServerVariables("REMOTE_ADDR") & vbcrlf & "Date/Time: " &Now & vbcrlf & "Name: " &txtName.Text & vbcrlf & "Email: " &txtFrom.Text & vbcrlf & "Phone: " &txtPhone.Text & vbcrlf & "Comments: " &txtBody.Text
objEmail.Priority = MailPriority.High
SmtpMail.SmtpServer = "smtp.address.com"
try
SmtpMail.Send(objEMail)
Response.Write("<font color='green'><strong>Thank you for contacting us. We will respond shortly.</strong></font>")
catch exc as Exception
Response.Write("<font color='red'><strong>Send failure: </strong></font>" + exc.ToString())
End Try
End Sub
</script>
<html>
<head>
<title>Contact Form</title>
</head>
<body>
<form id="ContactMain" runat="server">
<table border="0" align="center" cellpadding="3" cellspacing="0" width="100%">
<tr>
<td valign="top" class="form-input" colspan="2">
<label class="form-label-left">Name</label>
<asp:TextBox runat="server" class="form-textbox" ID="txtName"></asp:TextBox>
</td>
</tr>
<tr>
<td valign="top" class="form-input" colspan="2">
<label class="form-label-left">Email <font color="red">*</font></label>
<asp:TextBox runat="server" class="form-textbox" ID="txtFrom" ></asp:TextBox>
<asp:RequiredFieldValidator runat=server
ControlToValidate=txtFrom
ErrorMessage="Please enter a valid Email address">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td valign="top" class="form-input" colspan="2">
<label class="form-label-left">Phone</label>
<asp:TextBox runat="server" class="form-textbox" ID="txtPhone" ></asp:TextBox>
</td>
</tr>
<tr>
<td valign="top" class="form-input">
<label class="form-label-left">Comments <font color="red">*</font></label>
<asp:TextBox runat="server" TextMode="MultiLine" rows="6" cols="40" class="form-textarea" ID="txtBody"></asp:TextBox>
<asp:RequiredFieldValidator runat=server
ControlToValidate=txtBody
ErrorMessage="Please let us know what we can help you with">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2" valign="top" class="form-line">
<asp:PlaceHolder ID="phTest" runat="server" Visible="true">
<img src="captcha.ashx" id="imgCaptcha" /> <a href="javascript:void(0);" onclick="RefreshImage('imgCaptcha');">Refresh</a><br />
Write the characters in the image above<br />
<asp:TextBox ID="txtCaptcha" runat="server"></asp:TextBox><br />
<asp:Button Runat = server ID = btnSubmit OnClick = btnSubmit_Click Text = "Submit" class="form-submit-button"></asp:Button>
</asp:PlaceHolder>
<asp:PlaceHolder ID="phResult" runat="server" Visible="false">
<p><asp:Label ID="lblResult" runat="server" Font-Bold="true"></asp:Label></p>
<asp:Button ID="btnRetry" runat="server" Text="Take another test" />
</asp:PlaceHolder>
</td>
</tr>
</table>
</form>
</body>
</html>
这里是文件 "CodeFile" 引用的代码:
Partial Class custom_aspnet_test
Inherits System.Web.UI.Page
Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
If IsNothing(Session("ASPCAPTCHA")) OrElse Session("ASPCAPTCHA").ToString.Trim.Length = 0 Then
lblResult.Text = "This test has expired."
lblResult.ForeColor = Drawing.Color.Red
Else
Dim TestValue As String = txtCaptcha.Text.Trim.ToUpper(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))
If StrComp(TestValue, Session("ASPCAPTCHA").ToString.Trim, CompareMethod.Text) = 0 Then
lblResult.Text = "CAPTCHA PASSED"
lblResult.ForeColor = Drawing.Color.Green
Else
lblResult.Text = "CAPTCHA FAILED"
lblResult.ForeColor = Drawing.Color.Red
End If
'//IMPORTANT: You must remove session value for security after the CAPTCHA test//
Session.Remove("ASPCAPTCHA")
'//////////
End If
phTest.Visible = False
phResult.Visible = True
End Sub
Protected Sub btnRetry_Click(sender As Object, e As System.EventArgs) Handles btnRetry.Click
txtCaptcha.Text = ""
phTest.Visible = True
phResult.Visible = False
End Sub
End Class
问题是您在两个不同的地方响应 btnSubmit。其中之一位于 .aspx 文件的脚本标记中。那就是您发送电子邮件的地方。另一个地方是 btnSubmit_Click 方法中的代码隐藏文件。那就是你检查验证码的地方。
您已连接按钮以触发两个事件。他们两个都在开火,这就是为什么你总是得到两个结果。
您需要将邮箱代码移至btnSubmit_Click验证码成功后的代码
这是更新后的代码,其中包含验证码通过时调用的发送电子邮件方法。此外,您需要从 .aspx 文件中删除发送电子邮件代码,并从 btnSubmit 按钮声明中删除此属性:OnClick = btnSubmit_Click
Imports System.Web.Mail
Partial Class test
Inherits System.Web.UI.Page
Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
If IsNothing(Session("ASPCAPTCHA")) OrElse Session("ASPCAPTCHA").ToString.Trim.Length = 0 Then
lblResult.Text = "This test has expired."
lblResult.ForeColor = Drawing.Color.Red
Else
Dim TestValue As String = txtCaptcha.Text.Trim.ToUpper(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))
If StrComp(TestValue, Session("ASPCAPTCHA").ToString.Trim, CompareMethod.Text) = 0 Then
lblResult.Text = "CAPTCHA PASSED"
lblResult.ForeColor = Drawing.Color.Green
' The CAPTCHA passes so send the email
Me.SendEmail()
Else
lblResult.Text = "CAPTCHA FAILED"
lblResult.ForeColor = Drawing.Color.Red
End If
'//IMPORTANT: You must remove session value for security after the CAPTCHA test//
Session.Remove("ASPCAPTCHA")
'//////////
End If
phTest.Visible = False
phResult.Visible = True
End Sub
''' <summary>
''' This was the code you had in the aspx file.
''' </summary>
''' <remarks></remarks>
Private Sub SendEmail()
Dim objEMail As New MailMessage()
objEMail.To = "myemail@email.com"
objEMail.From = txtFrom.Text
objEMail.Subject = "Contact Form"
objEMail.Body = "IP: " & Request.ServerVariables("REMOTE_ADDR") & vbCrLf & "Date/Time: " & Now & vbCrLf & "Name: " & txtName.Text & vbCrLf & "Email: " & txtFrom.Text & vbCrLf & "Phone: " & txtPhone.Text & vbCrLf & "Comments: " & txtBody.Text
objEMail.Priority = MailPriority.High
SmtpMail.SmtpServer = "smtp.address.com"
Try
SmtpMail.Send(objEMail)
Response.Write("<font color='green'><strong>Thank you for contacting us. We will respond shortly.</strong></font>")
Catch exc As Exception
Response.Write("<font color='red'><strong>Send failure: </strong></font>" + exc.ToString())
End Try
End Sub
Protected Sub btnRetry_Click(sender As Object, e As System.EventArgs) Handles btnRetry.Click
txtCaptcha.Text = ""
phTest.Visible = True
phResult.Visible = False
End Sub
End Class