Solved

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

Posted on 2009-04-09
9
274 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
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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