Create contact us form

Create contact us form When you create your web site you may require to have contact us kind of functionality on site. This improves trust with users. Its very simple in Here we will instantly notify the end user and web owner or support team about user query.

I will use my Gmail credentials to configure email functionality on my web site.
You can watch the video above or refer the code below. Form to accept user name, email and message.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Contact.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
<title>Contact us</title>
<form id="form1" runat="server">
<strong>Contact Us:
<br />
<br />
<table style="width: 673px; height: 137px">
<td style="width: 107px; height: 4px">
<td style="height: 4px">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
<td style="width: 107px; height: 5px">
Your Email:</td>
<td style="height: 5px">
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
<td style="width: 107px; height: 32px">
<td style="height: 32px">
<asp:TextBox ID="txtMessage" runat="server" Height="236px" TextMode="MultiLine" Width="360px"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send" /><br />
<asp:Label ID="lblMessage" runat="server" ForeColor="LimeGreen"></asp:Label></td>

Code behind file of Contact.Aspx

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net;
public partial class _Default : System.Web.UI.Page 
protected void Page_Load(object sender, EventArgs e)
protected void Button1_Click(object sender, EventArgs e)
string url = Request.Url.AbsoluteUri;
string hyperlink = "<a href='" + url + "'>"+url+"</a>";
NetworkCredential loginInfo = new NetworkCredential("", "Passw@rd");
MailMessage msg = new MailMessage();
msg.From = new MailAddress("");
msg.To.Add(new MailAddress(txtEmail.Text));
msg.Bcc.Add(new MailAddress(""));
msg.Subject = "Notification from:" + url;
msg.Body = "Hi," + txtName.Text + "<br/> Thanks for contacting us. We will contact you soon. <br/>"+ "Our System has recorded a message"+ txtMessage.Text + " <br/> Thanks and Regards <br/>" + hyperlink ;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
catch (Exception ex)
lblMessage.Text = ex.Message;
lblMessage.Text = "Thanks for contacting us. We will get back to you in 24Hrs.";

It requires two namespace. using System.Net.Mail; and using System.Net.
System.Net.Mail provides functionality to configure your SMTP client and Mail Message From,To,Subject and
message fields.
System.Net provides Network Credential configuration functionality.

I have created an instance of Network credentials and used my Gmail user name password.
You can store the hard coded values in Web.Config or in database. It’s better to store those values in
Web.Config file because it becomes easier to update website for changed password or changed email address.
For demo purpose I have hardcoded it and its bad practice, you should not follow it.

How to send email using Gmail

First you need to configure Gmail settings to send email in or applications.
Send Email
What password should I use for configuring Gmail in my application.
Login to Google and click on account settings then configure Authorizing applications & sites .
Create one password for you and configure that in your application. You need not remember the
password generated for application. It is disposable and has one time use.

Description of SMTP Client

The instance of SMTP client uses Gmail SMTP address.  SMTP is simple email transport protocol
used for sending email. It listens on port 25.
while POP is post office protocol used for reading the mail messages and it listens on port number 110.
To know more about how to read email programmatically using and C#
Follow below article: 
Read hotmail email:

Read Gmail email:

The application explained above sends notification to end user and website owner.
You can write the code to store the received data from end user into data base.
The stored data can be distributed to your system appropriate department.

Tags: 2.0 step by step Membership Provider ListBox Binding csharpSql Server MirroringDateTimeTryParse with IFormatProvider culture information using c#This command requires an asynchronous connection Set Asynchronous Processing3dtrue in the connection Convert String To forms authenticationFailed to execute request because the App Domain could not be createdMemberShip Count Online Create Folder On ServerIFormatProvider DateTime


My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.

I'm Satalaj.