Solved

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

Posted on 2009-04-09
9
267 Views
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?

Thanks.

0
Comment
Question by:DanGettel
  • 4
  • 3
  • 2
9 Comments
 
LVL 15

Accepted Solution

by:
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
0
 
LVL 48

Assisted Solution

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

Author Comment

by:DanGettel
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

 

            Try

                rowsAffected = GettelDataSource.Update()

 

            Catch ex As Exception

 

                Server.Transfer("update_problems.aspx")

 

            Finally

                GettelDataSource = Nothing

 

            End Try

 

            If rowsAffected <> 1 Then

                Server.Transfer("update_problems.aspx")

            Else

 

                UpdateButton.Visible = False

 

                Dim appointmentDate As Label = _

                FormView1.FindControl("DateTextShortLabel")

                Dim appointmentTime As Label = _

                FormView1.FindControl("TimeLabel")

                Dim smtpClient As SmtpClient = New SmtpClient()

                Dim message As MailMessage = New MailMessage()

 

                Dim toAddress As New MailAddress( _

                "Confirmations@mydomain.com")

                Dim fromAddress As New MailAddress( _

                "info@mydomain.com", "My Organization")

 

                message.From = fromAddress

                message.To.Add(toAddress)

 

                Dim CCAddress As TextBox

                CCAddress = EmailAddressTextBox

                If Not String.IsNullOrEmpty(EmailAddressTextBox.Text) Then

                    message.CC.Add(CCAddress.Text)

                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>" & _

                "</body></html>"

 

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

                ' Set server details

                smtpClient.Host = "mail.mydomain.com"

                ' Send the email

                smtpClient.ServicePoint.MaxIdleTime = 1

        Try

                smtpClient.Send(message)

 

                Server.Transfer("confirm.aspx")

 

 

 

        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

0
 
LVL 15

Expert Comment

by:spprivate
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
0
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

by:DanGettel
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?
0
 
LVL 48

Expert Comment

by:jpaulino
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.
0
 

Author Comment

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

Results:

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?

0
 

Author Comment

by:DanGettel
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.
0
 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
Ok, it was really strange because that code looks right!
0

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