Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 21st, 2009, 09:44 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

You're clever.

It does work when a real email address is entered. So what if a user enters an invalid email address but the regular expression validator doesn't pick it up because it has the correct syntax?

Is there a way to catch the error and get the user to put in a real email address?
 
Old March 21st, 2009, 10:10 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
Is there a way to catch the error and get the user to put in a real email address?
Yes, there is. But rather than provide you with the full answer, I'll show you how you find the answer yourself. That way, when other types of exceptions are thrown you know how to find those as well. As there are many, many different Exception types in .NET, understanding when the occur is very useful for other situations, not related to e-mail. Try this:

1. Create a brand new ASPX page

2. In the Code Behind, add this:
Code:
 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
            Handles Me.Load
  Dim myMessage As New MailMessage()
  myMessage.Subject = "Test"
  myMessage.From = New MailAddress("test@dsahcdsssdlkv.com") 
  myMessage.To.Add(New MailAddress("test@dsahcdsssdlkv.com"))
  Dim myClient As New SmtpClient()
  Try
    myClient.Send(myMessage)
  Catch ex As Exception
  End Try
End Sub
3. Put a break point on the line that starts with Catch and hit F5.

4. The code will crash (as the e-mail address is not valid) and the Catch line will be highlighted

5. Put a Watch on the ex variable and expand it in the Watch window. On the first line you'll see from what Exception type it derives.

6. Modify the code as follows:

Code:
 
Dim myMessage As New MailMessage()
myMessage.Subject = "Test"
myMessage.From = New MailAddress("test@dsahcdsssdlkv.com")
myMessage.To.Add(New MailAddress("test@dsahcdsssdlkv.com"))
Dim myClient As New SmtpClient()
Try
  myClient.Send(myMessage)
Catch someException As TheTypeOfExceptionYouFoundInStep5
  myMessage.From = New MailAddress("you@yourprovider.com")
  myClient.Send(myMessage)
Catch ex As Exception
  ' Unknow exception
End Try
Don't forget to change the type of exception in the first Catch block and change you@yourprovider.com to a valid e-mail address.

Now, when you send the message and the exception occurs, the first Catch block with your exception type is hit. Inside that block you can change the To address and try to send the message again. Other exceptions that may occur in the Try block are caught by the general Catch block.

Hope this helps.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old March 21st, 2009, 10:20 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Thanks I will try that now.
 
Old March 21st, 2009, 10:38 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Not wanting to sound stupid but in your code example you have:


Catch someexception as theexceptionyoufoundinstep5

But where do I find the name of this exception? is it the node directly nested in the ex exception in the watch window?

in this case "system.net.mail.exception" ?

Also this method would presumably correct the error input a valid email address and then send the message as it is supposed to?

IS there a way that I cold then notify the user that the email address they used is invalid and if they expect a response they would need to use a valid email address?

Because if the message sends and I have invalid contact details then I wouldn't be able to reply if yo usee what I mean.

Thanks for that useful bit of information.
 
Old March 21st, 2009, 11:08 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

The Exceotion type you're after is the SmtpException:

Code:
 
Try
  myClient.Send(myMessage)
Catch someException As SmtpException
  myMessage.From = New MailAddress("you@yourprovider.com")
  myClient.Send(myMessage)
Catch ex As Exception
 ' Unknown exception
End Try
And rather than changing the To address you could display a Label with an error message inside this Catch block.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old March 21st, 2009, 11:27 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Thanks again thats a good idea. However now my form works I have another problem that it displays a label that says the email address is invalid but it also displays the success message aswell.

Currently I have:

Code:
 Try
                mySmtpClient.Send(myMessage)
            Catch SmtpEx As SmtpException
                lblEmailAdd.Visible = True
                lblSuccess.Visible = False
                contact1.Visible = True
            Catch ex As Exception
                ' Unknow exception
            End Try

            lblSuccess.Visible = True
            contact1.Visible = False
I thought that by putting:

Code:
Catch SmtpEx As SmtpException
                lblEmailAdd.Visible = True
                lblSuccess.Visible = False
                contact1.Visible = True
Would overwrite:
Code:
End Try

            lblSuccess.Visible = True
            contact1.Visible = False
if there was an error and just ignore the try catch statement if there wasn't.

But putting it in the try catch statement doesn't change it.. so any ideas?

Thanks alot for the help so far it's been really..helpful I guess lol.
 
Old March 21st, 2009, 11:43 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You should update the success label in the Try part.

Try is the "happy path" that executes completely if nothing goes wrong. When an error in the Try block occurs, only one of the Catch parts is executed (the one with the Exception that best matches the caught error).

Any code *after* the Try / End Try is executed normally if all errors are handled in a Catch block.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old March 21st, 2009, 01:21 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Thanks works perfectly for me now.
 
Old March 21st, 2009, 01:47 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're welcome....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Intellisence stopped working beddowsm Visual Studio 2008 3 September 5th, 2008 12:32 PM
Logins stopped working after adding to web.config troyb BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 5 September 2nd, 2008 03:34 PM
date function stopped working in report Loralee Access 6 December 8th, 2005 07:11 PM
Help... Smtp.Mail.Send stopped working funtent General .NET 1 October 27th, 2004 07:26 AM
Intellisense stopped working in InterDev 6 riga1966 Classic ASP Professional 3 June 1st, 2004 05:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.