我无法将CAPTCHA集成到我正在使用的表单中。我几乎没有.NET经验,所以任何帮助将不胜感激!我终于找到了一个有效的联系表格(当使用我个人域名中的电子邮件地址时 - 由于某些原因似乎无法使用gmail)。
我设法让它具有风格和功能,但它需要一个CAPTCHA来阻止垃圾邮件进入。我在http://www.tipstricks.org/找到了一个预制的CAPTCHA表单,它本身很好用,但我真的不确定将其与下面的表单集成。我可以让CAPTCHA单独工作,并且表单可以单独工作,但我似乎无法弄清楚如何让CAPTCHA验证并提交表单。
现在,当我填写表格并提交表格时,我收到一条绿色确认信,说明信息已发送,但有一条红色信息说CAPTCHA失败。无论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
答案 0 :(得分:1)
问题是你有两个不同的地方你回应btnSubmit。其中一个是在.aspx文件的脚本标记中。这是您发送电子邮件的地方。另一个地方是btnSubmit_Click方法中的代码隐藏文件。那是你检查CAPTCHA的地方。
您已连接按钮以触发两个事件。他们两个都在开火,这就是为什么你总能得到两个结果。
您需要将电子邮件代码移动到btnSubmit_Click中,以便CAPTCHA成功后面的代码。
以下是CAPTCHA通过时调用的send email方法的更新代码。此外,您需要从.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