2014-10-29 61 views
1

我有一個表單,發送一個包含4個字段的電子郵件。其中一個字段由自定義驗證器對數據庫驗證,如果發現該值,則應發送電子郵件。如果沒有找到該電子郵件應取消,並應該有一個錯誤消息。無論驗證如何,除了電子郵件發送之外,一切正在工作我怎樣才能保持電子郵件發送?取消電子郵件發送與asp.net自定義驗證程序

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("[email protected]", "[email protected]") 
    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 [email protected]" 
    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

沒關係,我知道了。我聲明瞭一個布爾值並在驗證器中設置爲true,然後僅在其設置爲true時才發送。我正在反思 – adgoodso23 2014-10-29 16:58:32

回答

2

你需要調用Page.Validate(),並且檢查Page.IsValid

相關問題