WorldofASP.NET : ASP.NET Directory, Tutorial, Hosting, and Source Code
You are 1 of 56 users


WorldofASP.NET >> ASP.NET >> Applications

Creating Contact Us Form easily using ASP.NET and SMTP

This article explain how to create a simple contact us form by using ASP.NET and System.Net.Mail to send email to the website owner
Published Date : 03 Dec 2007
Author : James Douglas
Language : VB.NET,C#
Platform : .NET
Technology : Visual Studio,ASP.NET
Views : 13084
Rating : (2 votes so far)



Introduction

A lot of times, we as a web master want to include a contact us form in our web site to gather sales questions from our site visitors. I can see that most of the people actually just include an email like sales@domainname in their Contact Us Page. I know that one works well if you just starting your site. However as your site is getting popular, you will notice that lots of spam email comes to your inbox and you probably has to spend hours and hours just to delete each of the spam email and also you might accidentally delete one of the legitimate email and thus you will lose your website sales.

So the workaround is to create a simple Contact Us Form with Captcha Image to prevent robot from sending spam to you and the result of the Contact Us Form will be sending email to you as a website owner, or you can save into database if you like to. I will show you code sample on sending email from your contact us form. For saving into database, you might need to check my other article on Basic ADO.NET.

Contact Us Form in ASP.NET 2 with Captcha

First, open Visual Studio 2005 and Add New Page called ContactUs.aspx
In the ASPX Page, please copy and paste the code below.

 <FORM ID="form1" RUNAT="server">
<H2>Contact Us Form</H2>
<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0 WIDTH=80%>
<TR>
<TD>Full Name</TD>
<TD>:</TD>
<TD><ASP:TEXTBOX ID=txtName RUNAT=server></ASP:TEXTBOX></TD>
</TR>
<TR>
<TD>Email Address</TD>
<TD>:</TD>
<TD><ASP:TEXTBOX ID=txtEmail RUNAT=server></ASP:TEXTBOX></TD>
</TR>
<TR>
<TD>Subject </TD>
<TD>:</TD>
<TD><ASP:TEXTBOX ID=txtSubject RUNAT=server></ASP:TEXTBOX></TD>
</TR>
<TR>
<TD VALIGN=top>Body</TD>
<TD VALIGN=top>:</TD>
<TD><ASP:TEXTBOX ID=txtBody RUNAT=server TEXTMODE=multiLine ROWS=10 COLUMNS=50></ASP:TEXTBOX></TD>
</TR>
<TR>
<TD VALIGN=top>Verification Image</TD>
<TD VALIGN=top>:</TD>
<TD><IMG src="CaptchaImage.aspx" /></TD>
</TR>
<TR>
<TD COLSPAN=3><ASP:BUTTON ID=btnSubmit RUNAT=server TEXT="Submit" /></TD>
</TR>
</TABLE>
</FORM>

Add New Page Called CaptchaImage.aspx and paste the code below.  If you don't understand what is CAPTCHA or don't understand on how the code below works. You probably need to refer to my other Article in GDI + and Captcha.
protected void Page_Load(object sender, EventArgs e)
{
Random Rand = new Random();int iNum = Rand.Next(10000, 99999);
Bitmap Bmp = new Bitmap(90, 50);
Graphics Gfx = Graphics.FromImage(Bmp);
Font Fnt = new Font("Verdana", 12, FontStyle.Bold);
Gfx.DrawString(iNum.ToString(), Fnt, Brushes.Yellow, 15, 15);// Create random numbers for the first line int RandY1 = Rand.Next(0, 50);int RandY2 = Rand.Next(0, 50);// Draw the first line Gfx.DrawLine(Pens.Yellow, 0, RandY1, 90, RandY2);// Create random numbers for the second lineRandY1 = Rand.Next(0, 50);
RandY2 = Rand.Next(0, 50);// Draw the second lineGfx.DrawLine(Pens.Yellow, 0, RandY1, 90, RandY2);
Bmp.Save(Response.OutputStream, ImageFormat.Gif);
Session["Number"] = iNum;
}



From the screenshot above, we can see that there is a CAPTCHA image shown, and we are storing the random generated Number into Session Variables. So that we can easily compare it from any other page in our web application. The Code Behind for the Contact Us Page will be like this.
 protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Session["Number"].ToString().Trim() == txtNumber.Text)
{
MailMessage m = new MailMessage(txtEmail.Text, ConfigurationManager.AppSettings["SalesEmail"].Trim());
m.Subject = txtSubject.Text;
m.Body = txtBody.Text;
SmtpClient o = new SmtpClient("mail.domainname.com");
o.Send(m);
Response.Write("Email has been sent to our Sales Team");
}
else
{
Response.Write("Verification Email not match. Please re-enter");
}
}

That's all you need to build a simple Contact Us Form by using Captcha Image and SmtpMail to send email to you. You can see that if you fail to type the right verification image code, the image will be generated again and the email won't get sent.

If you need further info on how to send email in ASP.NET, please click here.

    Download Source Code




    Other Related and Popular Articles :

    Sending Email in ASP.NET 2.0
    Send email in ASP.NET 2.0 Framework with or without SMTP Authentication

    URL Rewriting with ASP.NET 2.0
    How to implement URL Rewriting and Improve your SEO rankings.

    Building a Photo Tagging Application using ASP.NET 2.0, LINQ, and Atlas
    In this article, I will examines how to build a photo tagging application using ASP.NET 2.0, LINQ and Atlas framework.

    Using LINQ to XML (and how to build a custom RSS Feed Reader with it)
    In this article, Scott examines how to work with LINQ using XML. He also demonstrates how to build a custom RSS Feed Reader using these technologies.

    Build a DotNetNuke FileManager in ASP.NET
    Tutorial and Code Sample on building a DotNetNuke FileManager in ASP.NET


    Author Profile : James Douglas

    I work in a Software House Company in Malaysia (Kuala Lumpur) and I am MCP Certified in C# and Web Application course.
    I originally started my programming in Java but later on changed to Microsoft platform because of the simplicity and ease of use.
    I love .NET programming and am doing it almost every day now.

    Click here to view Author Profile


    How would you rate the quality of this content?
    Poor Excellent

    Comments

    #.
    11 Jun 2010 1:24 by : bushra.b

    thx alot

    but where we can put our email ? (our Sales Team)

    can i replace mail.domainname.com withe my email

    but it not running

    thx again

    #Simply Excellent
    21 Jan 2010 1:58 by : Rishav Jain

    Very Helpful.....Thanx.....

    Leave New Comments


    Article Content copyright by James Douglas
    Everything else Copyright © by WorldofASP.NET 2010

    Category
    .NET 3.5
    AJAX and ATLAS
    ASP.NET
    C# Programming
    Classic ASP
    Enterprise Systems
    General .NET
    VB.NET Programming
    Announcements
    Earn Cash by writing an article or review
    For more info Click here







    Legend : - Within 3 Days - Within 6 Days - Within 9 Days

    Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 | Link To Us | Contact Us
    © 2002-2010 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
    Our Partners : ASP.NET Web Hosting | ASP Hosting | ASP.NET Hosting | Phone Card | Calling Card |Stock Investing