Solved

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

Posted on 2009-04-09
9
282 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 15

Accepted Solution

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

Assisted Solution

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

Author Comment

by:DanGettel
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
 
            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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

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

Author Comment

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

Expert Comment

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

Author Comment

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

Expert Comment

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

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

617 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