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 asp.net. 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.

Asp.net 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Contact us</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<strong>Contact Us:
<br />
</strong>
<br />
<table style="width: 673px; height: 137px">
<tr>
<td style="width: 107px; height: 4px">
Name:</td>
<td style="height: 4px">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 107px; height: 5px">
Your Email:</td>
<td style="height: 5px">
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 107px; height: 32px">
Message:</td>
<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>
</tr>
</table>
</div>
</form>
</body>
</html>


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)
{
try
{
string url = Request.Url.AbsoluteUri;
string hyperlink = "<a href='" + url + "'>"+url+"</a>";
NetworkCredential loginInfo = new NetworkCredential("revenmerchant.business@gmail.com", "Passw@rd");
MailMessage msg = new MailMessage();
msg.From = new MailAddress("revenmerchant.business@gmail.com");
msg.To.Add(new MailAddress(txtEmail.Text));
msg.Bcc.Add(new MailAddress("revenmerchant.business@gmail.com"));
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("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
lblMessage.Text = "Thanks for contacting us. We will get back to you in 24Hrs.";
}
}


Description:
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 Asp.net or C#.net 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 Asp.net and C#
Follow below article: 
Read hotmail email:
http://revenmerchantservices.com/post/ASPnet-read-email-hotmail.aspx

Read Gmail email:
http://revenmerchantservices.com/post/ASpnet-Read-gmail-email.aspx

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:

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

Author

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 ASP.net 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.