lundi 11 mai 2015

Unable to send mail from VB form using system.net.mail

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.

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.

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.

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

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

Aucun commentaire:

Enregistrer un commentaire