CodersBarn.com
The ASP.NET Community Blog

ASP.NET Contact Forms

November 30, 2008 07:52 by agrace

ASP.NET Contact Forms I've seen some blog postings out there recently on what to do if your contact form fails for some reason. Some have suggested including your email address using JavaScript, which I do not recommend. Some people even suggest substituting "@" with "at"; if I was writing a screenscraper, this is the first thing I'd check for, after "dotcom"! If you're going to try and include your email address, then why bother with a contact form in the first place?

I've been using my own standard contact form for some time now. It displays either a success or error message upon submission. If there is an error, then it generates an email back to me. In fact, I like to have an email generated whenever there is an application error in a client's website, and I include this as part of routine error handling in my projects. This works for small projects where it is unlikely to over-burden the mail server. The strategy pattern helps me to factor out this kind of repetitive functionality; see an earlier post about this. At the end of this post, you can find a link to download a simple contact form.

ASP.NET Contact Forms

 

A lot of people have problems testing email with ASP.NET. The simple answer is to use a drop folder on your local machine when developing. Just create a folder called "maildrop" on your c:/ drive and use the following in your Web.config file:

<mailSettings>
    <smtp deliveryMethod='SpecifiedPickupDirectory'>
        <specifiedPickupDirectory pickupDirectoryLocation="c:\maildrop" />
    </smtp>
 </mailSettings>

 

ASP.NET Contact Forms

 

When you are ready to deploy your application, simply comment out the above entry in the config file and use the usual syntax below. Keep the drop directory section commented out in your config file as you might need it later for testing or further development:

<mailSettings>
    <smtp from="info@customersite.com">
        <network host="smtp.somehost.net"/>
    </smtp>
</mailSettings>

 

For the CSS purists out there, this is created using tables because we are more interested in the functionality here. If you want a CSS contact form layout, just holler in the comments :-)

Depending on the importance of the project you are working on, you may want to log the errors to a database. This is simple to do so I'm not going to delve into it here. Note, that there is nothing to prevent you from grabbing some fine-grain error details and storing them in your errors table also. Anything that helps you identify problems at a later date is a good candidate for a table field.

That just leaves network errors. I haven't had a chance to play with this yet, but it would be nice to know in advance if the mail server was down and I'm wondering if we could somehow tap into the SMTP return status codes before sending the mail? If anyone has any suggestions along these lines, please share them here?

ContactForm.zip (22.61 kb)

kick it on DotNetKicks.com


Tags: ,
Categories: ASP.NET
Actions: E-mail | Permalink | Comments (14) | Comment RSSRSS comment feed

Comments

December 8. 2008 00:38

trackback

Trackback from DotNetKicks.com

ASP.NET Contact Forms

DotNetKicks.com

December 8. 2008 04:10

trackback

Trackback from Web Development Community

ASP.NET Contact Forms

Web Development Community

December 8. 2008 09:46

trackback

Trackback from DotNetLinks.net

ASP.NET Contact Forms

DotNetLinks.net

December 8. 2008 12:57

Amgad Suliman

Yet another informative post Anthony. I had an idea that solved this problem for me a while back, I stopped sending emails with my contact page. Instead I save the user's input in a database. I think databases servers are much more reliable than email servers, specially when the database server and web server are the same machine. I describe it the link below:

amgadhs.com/index.php/2008/07/my-contact-us-idea/

Amgad Suliman

December 8. 2008 13:17

agrace

That's a very neat solution you posted Amgad and I think ti would work great in conjunction with regular emails. One could create a nice admin page with a master-detail view of the databased "emails" Smile

agrace

December 16. 2008 11:04

Speed Dating Company

You know, for some time I had many problems creating contact forms. Even the tutorial at asp.net didn't help since their forms are built on steps. While steps are great, I don't like the emailer to have to go through many steps just to send me a message. Your form looks very useful and easy to setup.

Thanks a lot for this post.

Speed Dating Company

January 26. 2009 14:57

Stonehenge Tours

Nice clean solution... many thanks.

Stonehenge Tours

June 2. 2009 09:45

NYC Speed Dating

Contact forms seem to be one of those simple things that people take for granted. Not being a highly technical developer myself, I find that there are always bugs associated with different servers/hosts/smtp server settings. We all know the problem with GoDaddy hosting, for instance. It would be nice if there was a universal html based/hosted solution, such as mycontactform, etc.

DR

NYC Speed Dating

July 13. 2009 11:43

Paul

Hi,

How do I customize the form from this site?  I need to add some checkboxes and a few more cotnact details.

Thanks

Paul

July 13. 2009 12:20

agrace

Paul,

Is yours an ASP.NET site? If so, the sample download here should get you up and running.

Some cool designs:
www.balkhis.com/.../

Anthony Smile

agrace

July 14. 2009 07:29

Paul

Thanks

Paul

July 14. 2009 15:33

Joseph

Thanks a lot for this post

Joseph

July 21. 2009 20:42

Netcoders

Thanks for this great post

Netcoders

July 31. 2010 06:21

dating online

By visiting your webpage, the first impression for me is strong. I can’t imagine when and why you share this great topic but don’t spread it with social bookmarking. This information can be published as reference in online journal, or even in press release site. An early improvement in your site is great, can give us more time in your website. Would you mind if I capture several screenshot as my collection, because I’ve joined several researches? General purpose for me is to tell you about this discussion. My critical question for us is the resource that you have used to manage this site. In order to make great discussion, you are great because you post new topic in several areas. But, I suggest in giving personal opinion, please refer to big or authority sites, I am sure you will be fine in giving past or future experiences. In my environment, I am sure your capability to enrich people can be strong advantage for your future.

dating online

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading