我制作了一个电子邮件程序。当我启动它时,它给我这个错误消息:
Menü(VB Version 1.0).exe中出现未处理的'System.InvalidOperationException'类型异常
我的代码
Imports System.Net.Mail
Public Class Email
Dim Tárgy As String = TextBox4.Text
Dim Üzenet As String = TextBox5.Text
Dim UserEmail As String = TextBox1.Text
Dim UserPass As String = TextBox2.Text
Dim ToEmail As String = TextBox3.Text
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyMailMessage As New MailMessage()
Try
MyMailMessage.From = New MailAddress(UserEmail)
MyMailMessage.To.Add(ToEmail)
MyMailMessage.Subject = (Tárgy)
MyMailMessage.Body = (Üzenet)
Dim SMTP As New SmtpClient("smtp.gmail.com")
SMTP.Port = 587
SMTP.EnableSsl = True
SMTP.Credentials = New System.Net.NetworkCredential((UserEmail), (UserPass))
SMTP.Send(MyMailMessage)
Catch ex As Exception
End Try
End Sub
End Class
答案 0 :(得分:1)
在Click事件中移动字符串的初始化。您之前不需要它们并且在全局级别执行它将导致异常,因为TextBox和其他控件尚未初始化
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Tárgy As String = TextBox4.Text
Dim Üzenet As String = TextBox5.Text
Dim UserEmail As String = TextBox1.Text
Dim UserPass As String = TextBox2.Text
Dim ToEmail As String = TextBox3.Text
Dim MyMailMessage As New MailMessage()
....
SMTP.Send(MyMailMessage)
End Sub
此外,如果您不对异常做任何事情,请不要使用空的trt / catch块。如果发生异常,则您无法看到错误。