无法使用system.net.mail从VB表单发送邮件

时间:2015-05-11 11:32:34

标签: vb.net winforms email system.net.mail

我们公司有一个呼叫中心,电脑被锁定(无法访问互联网,电子邮件,办公室应用程序),他们需要能够从自己的机器上记录支持票。

我创建了一个VB Windows窗体应用程序,它从用户那里获取一些信息(名称,电子邮件,扩展名,主题和问题描述)以及从WMI收集的系统信息。

我打算通过电子邮件将所有这些信息发送到我们的服务台,系统信息将使我们能够识别硬件问题等。

但是在尝试发送邮件时。它只是不起作用。

我们有一个smtp中继,允许匿名电子邮件中继,不需要用户凭据。

一旦我将基本的测试邮件投入使用,我将开始用我从用户那里获得的信息填充邮件。但是,邮件目前正在抛出异常而根本不发送。

注意我是VB和.net的绝对初学者,因为我来自linux / php背景

以下是我的代码:

Imports System.Management
Imports System.Management.Instrumentation
Imports System.Net.Mail

Public Class Form1

    Private Property strComputer As String
    Private Property objWMIService As Object
    Private Property colItems As Object
    Private Property colComputers As Object
    Private Property strComputerRole As String
    Private Property colDisks As Object
    Private Property colOperatingSystems As Object
    Private Property IPConfigSet As Object

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim response As MsgBoxResult
        response = MsgBox("Are you sure you want to exit?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
        If response = MsgBoxResult.Yes Then
            Me.Dispose()
        ElseIf response = MsgBoxResult.No Then
            Exit Sub
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim strName As String
        Dim strDept As String
        Dim strEmail As String
        Dim strExt As String
        Dim strDesc As String
        Dim strAffect As String
        Dim strSubject As String

        strName = TextBox1.Text
        strDept = TextBox2.Text
        strEmail = TextBox3.Text
        strExt = TextBox4.Text
        strSubject = TextBox6.Text
        strDesc = RichTextBox1.Text
        strAffect = TextBox5.Text

        strComputer = "."
        objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" _
            & strComputer & "\root\cimv2")
        colItems = objWMIService.ExecQuery _
            ("Select * from Win32_ComputerSystem")
        For Each objItem In colItems
            TextBox7.Text = objItem.Name
            TextBox8.Text = objItem.Manufacturer
            TextBox9.Text = objItem.Model
            TextBox11.Text = objItem.TotalPhysicalMemory
        Next
        colComputers = objWMIService.ExecQuery _
    ("Select DomainRole from Win32_ComputerSystem")
        For Each objComputer In colComputers
            Select Case objComputer.DomainRole
                Case 0
                    strComputerRole = "Standalone Workstation"
                Case 1
                    strComputerRole = "Member Workstation"
                Case 2
                    strComputerRole = "Standalone Server"
                Case 3
                    strComputerRole = "Member Server"
                Case 4
                    strComputerRole = "Backup Domain Controller"
                Case 5
                    strComputerRole = "Primary Domain Controller"
            End Select
            TextBox10.Text = strComputerRole
        Next
        colItems = objWMIService.ExecQuery("Select * from Win32_Processor")
        For Each objItem In colItems
            TextBox12.Text = objItem.Manufacturer
            TextBox13.Text = objItem.Name
            TextBox14.Text = objItem.MaxClockSpeed & " MHz"
        Next
        colItems = objWMIService.ExecQuery("Select * from Win32_BIOS")
        For Each objItem In colItems
            TextBox15.Text = objItem.Version
            TextBox16.Text = objItem.SerialNumber
        Next
        colItems = objWMIService.ExecQuery("Select * from Win32_VideoController")
        For Each objItem In colItems
            TextBox17.Text = objItem.Name
        Next
        colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk where DeviceID = ""C:""")
        For Each objDisk In colDisks
            TextBox18.Text = Math.Round(objDisk.Size / 1073741824) & " GB"
            TextBox19.Text = Math.Round(objDisk.Freespace / 1073741824) & " GB"
        Next
        colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
        For Each objOperatingSystem In colOperatingSystems
            TextBox20.Text = objOperatingSystem.Caption & "  " & objOperatingSystem.Version
            TextBox21.Text = objOperatingSystem.ServicePackMajorVersion & "." & objOperatingSystem.ServicePackMinorVersion
        Next
        Dim moIP As ManagementObject

        Dim myNet = New ManagementObjectSearcher _
        ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

        For Each moIP In myNet.Get
            ' Eg: Display IP address in Message Box
            TextBox22.Text = moIP("IPAddress")(0)
        Next

        Dim mail As New MailMessage()
        mail.To.Add("helpdesk@company.co.za")
        mail.From = New MailAddress(strEmail)

        Dim smtp As New SmtpClient()
        smtp.Host = "relay.company.local"
        smtp.EnableSsl = False

        mail.Subject = strSubject
        mail.Body = strDesc
        smtp.Send(mail)

    End Sub

    Private Function Wscript() As Object
        Throw New NotImplementedException
    End Function

    Private Function IsNull(p1 As Object) As Boolean
        Throw New NotImplementedException
    End Function


End Class

更新: 以下是一个例外

An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll

Additional information: Service not available, closing transmission channel. The server response was: 4.3.2 Service not available, closing transmission channel

1 个答案:

答案 0 :(得分:0)

您可能会遗漏一些细节。这是一个帮助的片段:

    '...

    'for when not using default credentials
    Dim SMTP_Credentials As System.Net.NetworkCredential = New System.Net.NetworkCredential
    SMTP_Credentials.UserName = "UserName"
    SMTP_Credentials.Password = "Password"


    Using SMTP_Mail As New Net.Mail.SmtpClient
        With SMTP_Mail
            .EnableSsl = False
            .Host = "IP"  'ip address of smtp client
            .Port = 25  'by default SSL does not use port 25 (use port 587 for SSL)
            .UseDefaultCredentials = True
            If Not .UseDefaultCredentials Then .Credentials = SMTP_Credentials
            .Send(mailMsg) 'send email.
        End With
    End Using

    '...

我建议您先使用默认凭据设置。我发现这几乎从来没有像预期的那样设置......除非你是控制服务器,否则不要依赖默认设置是标准设置......