Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check error output from smtp.Send(mail)

Status
Not open for further replies.

MikeM2468

IS-IT--Management
Apr 5, 2011
100
US
I'm putting together a POC script just to test email functionality. The script works but I want to add some redundancy to it. In its current form, it sends an email using the IP address of an SMTP server (and it works as planned). What I need to do is to check that it worked and if it didn't, try again using a different SMTP server. Here is the current working code.

Code:
Imports System.Net.Mail
Module MainTest
    Public Sub Main()
        MsgBox("Click OK ", 0, "This is a test!")

        'get the hostname
        Dim strHostName As String
        strHostName = System.Net.Dns.GetHostName()
        'create the mail message
        Dim mail As New MailMessage()

        'set the addresses
        mail.From = New MailAddress("user1@domain.com")
        mail.To.Add("user2@domain.com")

        'set the content
        mail.Subject = "Hello"
        mail.Body = "This was run on " & strHostName & "."

        'send the message
        Dim smtp As New SmtpClient("1.1.1.1")
        smtp.Send(mail)
    End Sub
End Module
 
This seems to work:

Code:
Imports System.Net.Mail
Module MainTest
    Public Sub Main()
        MsgBox("Click OK ", 0, "This is a test!")

        'get the hostname
        Dim strHostName As String
        strHostName = System.Net.Dns.GetHostName()
        'create the mail message
        Dim mail As New MailMessage()

        'set the addresses
        mail.From = New MailAddress("user1@domain.com")
        mail.To.Add("user2@domain.com")

        'set the content
        mail.Subject = "Hello"
        mail.Body = "This was run on " & strHostName & "."

        'send the message
        Try
            Dim smtp As New SmtpClient("1.1.1.1")
            smtp.Send(mail)
        Catch
            Try
                Dim smtp As New SmtpClient("2.2.2.2")
                smtp.Send(mail)
            Catch
            End Try
        End Try
    End Sub
End Module
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top