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

时间:2022-08-21 18:13:17

Our company has a call centre which pc's are locked down (no access to internet, email, office applications) and there is a need for them to be able to log support tickets from their own machines.

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

I have created a VB Windows forms application which grabs some info from the user (name, email, extension, subject and description of the issue) as well as system information collected from WMI.

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

I intent to send all of this information to our helpdesk via email, the sys information would enable us to identify hardware problems and such.

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

However when trying to send a mail. it just doesnt work.

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

We have a smtp relay which allows for anonymous email relay and does not require user credentials.

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

Once i get the basic test mail to work i will start populating the mail with the information i get from the user. However the mail is currently throwing exceptions and not sending at all.

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

Note I am an absolute beginner when it comes to VB and .net as i come from a linux / php background

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

Below is my code:

以下是我的代码:

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

UPDATE: Below is an exception

更新:以下是一个例外

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 个解决方案

#1


You might be missing a few details. Here is a snippet to help:

您可能会遗漏一些细节。这是一个帮助代码:

    '...

    '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

    '...

I am going to suggest you play with the default credential settings first. I have found this to be almost never set as expected... Don't rely on the default settings being what is standard unless you are the on in control of the server...

我建议您先使用默认凭据设置。我发现这几乎从未按预期设置...除非你是控制服务器,否则不要依赖默认设置是什么标准...

#1


You might be missing a few details. Here is a snippet to help:

您可能会遗漏一些细节。这是一个帮助代码:

    '...

    '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

    '...

I am going to suggest you play with the default credential settings first. I have found this to be almost never set as expected... Don't rely on the default settings being what is standard unless you are the on in control of the server...

我建议您先使用默认凭据设置。我发现这几乎从未按预期设置...除非你是控制服务器,否则不要依赖默认设置是什么标准...