我有一个发送包含4个字段的电子邮件的表单。其中一个字段由针对数据库的自定义验证程序验证,如果找到该值,则应发送电子邮件。如果未找到,则应取消电子邮件,并且应该出现错误消息。一切正常,除了电子邮件发送,无论验证如何。如何保持电子邮件不发送?
Imports System.Net.Mail
Imports System.Data.OleDb
Imports System.Data.SqlClient
Partial Class inforequest
Inherits System.Web.UI.Page
Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim mm As New MailMessage("sender@email.com", "receiver@email.com")
mm.Subject = txtSubject.Text
mm.Body = "Name: " & txtName.Text & "<br /><br />Email: " & txtEmail.Text & "<br />" & txtBody.Text & "<br /> Agent Code:" & AgentCode.Text
mm.IsBodyHtml = True
Dim smtp As New SmtpClient()
smtp.Host = "mailserver"
smtp.EnableSsl = False
Dim NetworkCred As New System.Net.NetworkCredential()
smtp.UseDefaultCredentials = False
NetworkCred.UserName = "username"
NetworkCred.Password = "password"
smtp.EnableSsl = False
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mm)
lblMessage.Text = "Email Sent SucessFully."
End Sub
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim dbconn As String
Dim qstring = Request.QueryString("ID")
Dim addressDR As System.Data.SqlClient.SqlDataReader
Dim sqlcommand As String = "SELECT * FROM listings WHERE ID=@qstring"
dbconn = ConfigurationManager.ConnectionStrings("houses").ToString
Dim connection As New System.Data.SqlClient.SqlConnection(dbconn)
connection.Open()
Dim addresscmd As New System.Data.SqlClient.SqlCommand(sqlcommand, connection)
addresscmd.Parameters.AddWithValue("@qstring", qstring)
addressDR = addresscmd.ExecuteReader()
If addressDR.HasRows Then
addressDR.Read()
Me.txtBody.Text = "I would like to request a showing of the home located at: " & addressDR("address") & " MLS#: " & addressDR("mlsnum")
addressDR.Close()
End If
connection.Close()
End Sub
Protected Sub CodeValidate_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CodeValidate.ServerValidate
Dim AgentCode = Request.Form("AgentCode")
Dim sql As String = "SELECT agentcode FROM Codes WHERE agentcode = @AgentCode"
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("Codes").ConnectionString)
Using cmd As New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("@AgentCode", AgentCode)
conn.Open()
Using rdr As SqlDataReader = cmd.ExecuteReader()
If (rdr.Read()) Then
'MsgBox("reader reading")
'If AgentCode = rdr("agentcode").ToString() Then
args.IsValid = True
'MsgBox("valid!")
Else
args.IsValid = False
'MsgBox("not valid")
End If
End Using
conn.Close()
End Using
End Using
End Sub
End Class
答案 0 :(得分:2)
您需要调用Page.Validate()并检查Page.IsValid