Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 15th, 2011, 03:47 PM
Registered User
 
Join Date: Apr 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Very good book

Hello Imar,

Just wanted to say that I have been having a good time following this book on my journey to learn ASP.NET. I like your writing and find the examples to be rather easy to follow with great explanations. I am really glad you follow each example with a How It Works section as it pulls everything together.

I just finished up Chapter 9 and I could not help but wonder why you put in the code:

myMessage.From = new MailAddress("you@yourprovider.com", "Sender Name")

and then asking it to replace it with our own information. Would it have not been better to use the Name and Email information from the ContactForm.aspx?

Such as:

myMessage.From = new MailAddress(EmailAddress.Text, Name.Text);

This would have then added the name and the email address of the person sending the information into the From part of the email.

Just thought I would share this with other users, just in case someone was wondering.

Thanks
Reply With Quote
  #2 (permalink)  
Old April 15th, 2011, 03:59 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

Good to hear you like the book so much.

The reason I am not using the user's address in the From address is the fact that it could be an invalid address which may cause the code to crash and the e-mail to fail. By using my own address, I can be sure the address is valid.

One solution I use is to wrap the code in a try catch block. In the try I try to send the e-mail using the user's address, and in the catch I send it again using my own.

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!
Reply With Quote
  #3 (permalink)  
Old April 15th, 2011, 04:26 PM
Registered User
 
Join Date: Apr 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Imar View Post
Hi there,

Good to hear you like the book so much.

The reason I am not using the user's address in the From address is the fact that it could be an invalid address which may cause the code to crash and the e-mail to fail. By using my own address, I can be sure the address is valid.

One solution I use is to wrap the code in a try catch block. In the try I try to send the e-mail using the user's address, and in the catch I send it again using my own.

Cheers,

Imar
Thanks Imar for the quick reply,

However, is not the RegularExpressionValidator for the Email address pretty much validating the text entered to ensure that the user will at least put in some text that conforms to the standards of an email address? Whether or not the user using the Contact form wants to put in their real email address or not (as long as it conforms to the email address standard) is not really a factor on whether or not the email makes it into my (web site admin) email account.

The To.add email address is hard-coded so I know it will always arrive in my inbox. Whether or not I can send a reply back to the user making the comment though may be another issue all together.

Also whether or not I would want to use a try catch block to validate the email address of the user is another matter and I am not sure if I would use it here. Not sure how I would validate any differently then the validation rule being set on the form?

Thanks for the quick reply. I am also very glad to see you being so active in your forum for the book. Also, I hope you do not mind me asking the questions above regarding your code etc... as I do use it to help me get a better understanding of the logic of the program.

Richard
Reply With Quote
  #4 (permalink)  
Old April 16th, 2011, 04:20 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Richard,

An e-mail address with valid syntax doesn't always make it a working address. So, while aaa@aaa.aa would be valid, it certainly wouldn't be a working e-mail address. Depending on the mail server used you could still run into troubles, In addition, your From user may now get bounce messages in case your target mail server is (temporarily) unavailable.

I think the cleanest solution is to use the ReplyToList property like this:

Code:
myMessage.From = new MailAddress(AppConfiguration.FromAddress, AppConfiguration.FromName);
myMessage.To.Add(new MailAddress(AppConfiguration.ToAddress, AppConfiguration.ToName));
myMessage.ReplyToList.Add(new MailAddress(EmailAddress.Text, Name.Text));
As an alternative, you could use the try/catch block I mentioned like this:

Code:
 
SmtpClient mySmtpClient = new SmtpClient();
MailMessage myMessage = new MailMessage();
try
{
  // ...
  myMessage.From = new MailAddress(EmailAddress.Text, Name.Text);
  mySmtpClient.Send(myMessage);
  // ...
}
catch (Exception ex)
{
  myMessage.From = new MailAddress(AppConfiguration.FromAddress, AppConfiguration.FromName);
  mySmtpClient.Send(myMessage);
  // ...
}
This tries to send the message using the user's details, and then resends it using your data when it fails.

Hope this helps,

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!
Reply With Quote
  #5 (permalink)  
Old April 18th, 2011, 09:49 AM
Registered User
 
Join Date: Apr 2011
Location: Delhi(INDIA)
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,
For check whether email address is working or not...
you can use following free web service from webservicex.net

http://webservicex.net/ws/WSDetails....SID=51&CATID=4
Reply With Quote
  #6 (permalink)  
Old April 18th, 2011, 06:56 PM
Registered User
 
Join Date: Apr 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the replies. I will need to check out the link for the e-mail testing service. I never knew it existed. I also like the try-catch block Imar. I can see how it works.

I guess my only concern was the amount of time it would take to verify whether an e-mail was actually valid, using the code in the try statements. How long will the method stay open and produce an exception if the e-mail was not valid? I guess I would need to write some code and check that out.

Once again thanks for the quick replies. Much appreciated.
Reply With Quote
  #7 (permalink)  
Old April 19th, 2011, 04:10 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi MrWisker,

Make sure you understand what the web service does before you start using it. It's not fail proof.

Many mail servers (including my own) use a concept called grey-listing. On the first receipt of an e-mail from an untrusted source, they bounce the message and reply with a "An error occurred while accepting your message. Please try again within 15 minutes". This is done to prevent spam. Legit mail servers will indeed send the message again, while spammers don't or so is the idea.

I tested a number of known addresses against the service below and they all got rejected.

Just a thought....

>> How long will the method stay open and produce an exception if the e-mail was not valid?

Just as long as it takes to deliver the message. ASP.NET tries to deliver the message directly, and whether it succeeds or not then ends the method. So, this try / catch is at the most twice as slow as normally (because you might need to send two messages instead of one). But a single message takes only from a few milliseconds to a second or two, depending on the responding mail server.

Hope this helps,

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!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
This book is very good. Shawdren BOOK: Professional ASP.NET Design Patterns 6 February 5th, 2011 11:04 AM
Good book Ciupaz BOOK: Beginning Microsoft SQL Server 2008 Administration 0 April 9th, 2010 06:35 AM
Good In-between book? wilbo ASP.NET 2.0 Basics 0 April 18th, 2008 09:37 AM



All times are GMT -4. The time now is 01:32 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.