0

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
Stroes
  • 351
  • 2
  • 5
  • 23
  • _"However the mail is currently throwing exceptions"_ What exceptions? Those are the reason it's not working, help us to help you by telling us :) – James Thorpe May 11 '15 at 11:34
  • Also may be worth noting that since you come from a PHP background, you may be more comfortable doing this sort of thing in C# rather than VB - they're as capable as each other, but the C# syntax will be closer to what you're familiar with. – James Thorpe May 11 '15 at 11:38
  • _"Service not available"_ How exactly are the machines locked down? It could be that whatever is blocking access to these things is also blocking your code. – James Thorpe May 11 '15 at 11:39
  • Apologies, i am scatterminded at the moment. I have updated the original question with the exception. In addition i have done the same application before although a couple of years ago in VB and reckoned it would be the easiest to pick up again. Also this application will be the only thing i will be coding in .net / VB but i will look at the C# option though. Just need to get this thing working – Stroes May 11 '15 at 11:40
  • They are locked down with group policies, my machine on the other hand has no GPO's and i am still getting the exception – Stroes May 11 '15 at 11:41
  • Ok - in that case it would appear that the mail server is configured quite restrictively to only accept connections from certain things. You may need to take this up with your mail server administrator – James Thorpe May 11 '15 at 11:42
  • Is there an easier way of sending mail with VB? instead of using the above method – Stroes May 11 '15 at 11:44
  • That _is_ the easy way of sending mail - it's only a few lines! – James Thorpe May 11 '15 at 11:44

1 Answers1

0

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...

JStevens
  • 2,090
  • 1
  • 22
  • 26
  • I have found that my smtp server settings didnt allow me to use the relay and had to point the app to use the exchange server and it now works. – Stroes May 12 '15 at 05:37