【问题标题】:Unable to send mail from VB form using system.net.mail无法使用 system.net.mail 从 VB 表单发送邮件
【发布时间】:2015-05-11 11:32:33
【问题描述】:

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

我创建了一个 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

【问题讨论】:

  • “但是邮件目前正在抛出异常” 什么异常?这些是它不起作用的原因,请告诉我们帮助我们帮助您:)
  • 另外可能值得注意的是,由于您来自 PHP 背景,您可能更愿意在 C# 而不是 VB 中做这种事情 - 它们彼此一样有能力,但 C#语法会更接近你所熟悉的。
  • “服务不可用” 机器究竟是如何锁定的?可能是阻止访问这些东西的任何东西也阻止了您的代码。
  • 抱歉,我现在心不在焉。我已经更新了原始问题,但有例外。此外,尽管几年前在 VB 中我也做过同样的应用程序,但我认为这将是最容易再次使用的应用程序。此外,此应用程序将是我将在 .net / VB 中编码的唯一内容,但我会查看 C# 选项。只需要让这件事正常工作
  • 他们被组策略锁定,另一方面,我的机器没有 GPO,我仍然遇到异常

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


【解决方案1】:

您可能遗漏了一些细节。这是一个可以帮助的 sn-p:

    '...

    '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

    '...

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

【讨论】:

  • 我发现我的 smtp 服务器设置不允许我使用中继,必须将应用程序指向使用交换服务器,现在它可以工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-02
  • 2012-08-22
  • 1970-01-01
  • 2022-01-18
  • 2017-01-20
相关资源
最近更新 更多