【问题标题】:Mailing Attempt Object Reference?邮件尝试对象参考?
【发布时间】:2023-03-10 18:15:01
【问题描述】:

我正在尝试制作一个简单的电子邮件发送程序,但是当它说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("user@hotmail.com", "password")
       client.Credentials = SMTPUserInfo

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

【问题讨论】:

  • 错误发生在哪里?错误信息的全文是什么?这是 WebForms 还是 MVC?哪个版本的 .NET?
  • 欢迎来到 Stack Overflow!几乎所有NullReferenceException 的情况都是一样的。请参阅“What is a NullReferenceException in .NET?”获取一些提示。

标签: vb.net email smtp nullreferenceexception mailing


【解决方案1】:

你声明了两个 smtpclients

 Dim client As SmtpClient
 Dim SmtpServer As New SmtpClient()

您实例化了SmtpServer,但没有实例化client。那你就做吧

 client.UseDefaultCredentials = False

我想这是引发异常的地方。 该代码看起来像是有一些行从谷歌帐户通过 SmtpServer 发送一些邮件,而其他行从 hotmail 帐户通过客户端发送一些邮件。此外,SmtpServer 正在使用 config.sys 中定义的一些服务器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多