How to check and see if an email was successfully sent using vb 2.0

Posted on 2009-04-09
Last Modified: 2012-05-06
When I request that an email be sent using  smtpClient.Send(message) how can I immedately check in code to see if it was sent successfully.

I have tried using try...catch, but weird things have happened.

So I was hoping to use an IF...THEN statement to see if the email has been sent and determine what happens if it has not been successfully sent.

Any suggestions on how to do this?


Question by:DanGettel
  • 4
  • 3
  • 2
LVL 15

Accepted Solution

spprivate earned 300 total points
Comment Utility
Unfortunately try catch is the only option as far as I know.What is the wierd things happening with it
LVL 48

Assisted Solution

jpaulino earned 200 total points
Comment Utility
I agree with spprivate ... it's the only way to know.

Author Comment

Comment Utility
The following code is where I am having my problems.  Specifically the 2nd TRY... CATCH at the bottom.

The protected sub worked fine before I added that 2nd TRY..CATCH.  The only issue was if there was a problem sending the email (I would get the nasty looking error messages).

After adding the 2nd TRY...CATCH if there is an exception in the 2nd TRY...CATCH it doesn't display the label as I am asking it to do and then stop. Instead, it goes through the entire sub a 2nd time (doing a second update to the database) and then displaying the label and ending the sub when it hits the CATCH for the second time.

Any idea why it would do that?
    Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateButton.Click



            Dim GettelDataSource As New SqlDataSource()

            GettelDataSource.ConnectionString = ConfigurationManager.ConnectionStrings("MyConnectionString").ToString()



            GettelDataSource.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure

            GettelDataSource.UpdateCommand = "UpdateInfo"


            GettelDataSource.UpdateParameters.Add("FirstName", FirstNameTextBox.Text)

            GettelDataSource.UpdateParameters.Add("LastName", LastNameTextBox.Text)

            GettelDataSource.UpdateParameters.Add("Email", EmailAddressTextBox.Text)

            GettelDataSource.UpdateParameters.Add("PhoneNumber", PhoneNumberTextBox.Text)

            GettelDataSource.UpdateParameters.Add("Comments", CommentsTextBox.Text)

            GettelDataSource.UpdateParameters.Add("UserName", UserName)

            GettelDataSource.UpdateParameters.Add("UserId", UserID)


            Dim rowsAffected As Integer = 0



                rowsAffected = GettelDataSource.Update()


            Catch ex As Exception





                GettelDataSource = Nothing


            End Try


            If rowsAffected <> 1 Then




                UpdateButton.Visible = False


                Dim appointmentDate As Label = _


                Dim appointmentTime As Label = _


                Dim smtpClient As SmtpClient = New SmtpClient()

                Dim message As MailMessage = New MailMessage()


                Dim toAddress As New MailAddress( _


                Dim fromAddress As New MailAddress( _

                "", "My Organization")


                message.From = fromAddress



                Dim CCAddress As TextBox

                CCAddress = EmailAddressTextBox

                If Not String.IsNullOrEmpty(EmailAddressTextBox.Text) Then


                End If


                message.Subject = "Confirmation Update"

                message.IsBodyHtml = True

                message.Body = _

                "<html><head><title>Confirmation of Update</title></head><body>" & _

                "<p>Hello " & _

                HttpUtility.HtmlEncode(FirstNameTextBox.Text) & " " & _

                HttpUtility.HtmlEncode(LastNameTextBox.Text) & ",</p><br /><p>This email was sent to confirm your update <strong>on " & _

                HttpUtility.HtmlEncode(appointmentDate.Text) & " at " & _

                HttpUtility.HtmlEncode(appointmentTime.Text) & ".</p></strong><br /><p>Regards,</p><br /><p>Name</p>" & _



                message.Headers.Add("X-MimeOLE", "Produced")

                ' Set server details

                smtpClient.Host = ""

                ' Send the email

                smtpClient.ServicePoint.MaxIdleTime = 1








        Catch ex As System.Net.Mail.SmtpException

            ' Display error message

            EmailProblemsLabel.Visible = True

            EmailProblemsLabel.Text = "You have successfully made change, but there was a problem sending the email."" & ex.Message

            Exit Sub

        End Try


        End If


    End Sub

Open in new window

LVL 15

Expert Comment

Comment Utility
Can you just print the exception message in the second try.
Also instead of catching Smtp exception,just catch it as an exception object and let me know what is the exception
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.


Author Comment

Comment Utility
Both times it hits the second try it has the same exception, which is "Failure sending mail."

It doesn't matter if I use Exit Sub or Return in the catch.  Neither one of them helps.

Any ideas?
LVL 48

Expert Comment

Comment Utility
But are you checking that on debug mode or after the deploy ?
You should also have a Try ... Catch block for all the sub and specially because you're connection to a database before the send method. Also follow the spprivate advice to include another catch for a general exception.

Author Comment

Comment Utility
I added a try...catch for the entire sub.  I also have all 3 try...catch blocks checking for general exceptions.


1) The try...catch that wraps around the entire sub never catches anything.
2) The next try catch works fine, but since the database is being updated, never catches anything.
3)The last try...catch catches "Failure sending mail.". On the unexplainable 2nd time through the sub it catches the same thing.

Any ideas?


Author Comment

Comment Utility
The code I submitted was actually fine. The problem was with the asp:Button, which had the OnClick attribute of UpdateButton_Click.  Since I am using VB code and not C# I didnt need the OnClick attribute. Once I got rid of the OnClick attribute everything worked fine.

Thanks for the comments.
LVL 48

Expert Comment

Comment Utility
Ok, it was really strange because that code looks right!

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now