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
ID: 24108099
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
ID: 24108372
I agree with spprivate ... it's the only way to know.

Author Comment

ID: 24108865
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

ID: 24110540
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.


Author Comment

ID: 24113383
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

ID: 24114081
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

ID: 24115321
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

ID: 24130878
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

ID: 24130889
Ok, it was really strange because that code looks right!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

911 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

18 Experts available now in Live!

Get 1:1 Help Now