2014-03-04 65 views
0

我試圖做一個簡單的電子郵件發送程序,但它時,它說:object reference not set to an instance of an object郵寄嘗試對象參考?

Try 
     Dim mail As MailMessage 
     Dim client As SmtpClient 
     Dim SmtpServer As New SmtpClient() 
     Dim fromAddress As String 
     Dim toAddress1 As String 
     Dim toAddress2 As String 
     Dim toAddress3 As String 
     Dim subject As String 
     Dim message As String 
     SmtpServer.Credentials = New Net.NetworkCredential("*********@gmail.com", "**********") 
     client.UseDefaultCredentials = False 

     fromAddress = FromEmail.Text 
     If ToEmail1.Visible = True Then 
      toAddress1 = ToEmail1.Text 
     ElseIf ToEmail1.Visible = True And ToEmail2.Visible = True Then 
      toAddress1 = ToEmail1.Text 
      toAddress2 = ToEmail2.Text 
     ElseIf ToEmail1.Visible = True And ToEmail2.Visible = True And ToEmail3.Visible = True Then 
      toAddress1 = ToEmail1.Text 
      toAddress2 = ToEmail2.Text 
      toAddress3 = ToEmail3.Text 
     End If 

     subject = "Subject" 
     Message = "Message" 

     mail = New MailMessage(fromAddress, toAddress1, subject, Message) 
     client = New SmtpClient("Smtp.live.com") 

     client.Port = 587 
     Dim SMTPUserInfo As New System.Net.NetworkCredential("[email protected]", "password") 
     client.Credentials = SMTPUserInfo 

     client.Send(mail) 
     MsgBox("sent") 
Catch ex As Exception 
      MsgBox(ex.Message.ToString()) 
End Try 
+0

錯誤發生在哪裏?錯誤信息的全文是什麼?這是WebForms還是MVC? .NET的哪個版本? –

+0

歡迎來到Stack Overflow!幾乎所有的'NullReferenceException'都是一樣的。請參閱「[什麼是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)」的一些提示。 –

回答

0

你聲明瞭兩個smtpclients

Dim client As SmtpClient 
Dim SmtpServer As New SmtpClient() 

你實例化SmtpServer,但不client混淆了我。那麼你做

client.UseDefaultCredentials = False 

我想這是引發異常的地方。 代碼看起來像有一些線路通過SmtpServer從谷歌帳戶和其他線路發送一些郵件通過客戶端從Hotmail帳戶發送一些郵件。此外,SmtpServer正在使用config.sys中定義的一些服務器。

相關問題