Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Introduction:

In this article I will explain how to create contact us form or page in asp.net using c# and vb.net.

Description:

In previous posts I explained send mail using asp.net, send mail using gmail account in asp.net, send mail with images using gmail in asp.net, send html page as mail body in asp.net and many articles on send mail in asp.net. Now I will explain how to create Contact Us form in asp.net using c# and vb.net.

Now we are going to use our gmail account credentials to send mail for that first you need to enable POP enable option in your Gmail account for that you need to open your gmail account and go to Settings --> Forwarding and POP/IMAP 

After that design your aspx page like this


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Create Asp.net contact us page in c#, VB.NET</title>
<style type="text/css">
.Button
{
background-color :#FF5A00;
color: #FFFFFF;
font-weight: bold;
margin-right: 2px;
padding: 4px 20px 4px 21px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellspacing="2" cellpadding="2" border="0">
<tr><td></td><td><b>Contact Us Form</b></td></tr>
<tr><td><b>Name</b></td><td><asp:TextBox ID="txtName" runat="server" /></td></tr>
<tr><td><b>Email</b></td><td><asp:TextBox ID="txtEmail" runat="server" /></td></tr>
<tr><td><b>Subject</b></td><td><asp:TextBox ID="txtSubject" runat="server" /></td></tr>
<tr><td valign="top"><b>Message</b></td><td> <asp:TextBox ID="txtMessage" Rows="5" Columns="40" TextMode="MultiLine" runat="server"/></td></tr>
<tr><td></td><td><asp:button ID="btnSubmit" Text="Submit"  runat="server" onclick="btnSubmit_Click" CssClass="Button"/></td></tr>
<tr><td colspan="2" style=" color:red"><asp:Label ID="lbltxt" runat="server"/></td></tr>
</table>
</div>
</form>
</body>
</html>
After that add following namespaces in your codebehind

C# Code


using System;
using System.Net.Mail;

After that write the following code in button click


protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtEmail.Text);
// Recipient e-mail address.
Msg.To.Add("administrator@aspdotnet-suresh.com");
Msg.Subject = txtSubject.Text;
Msg.Body = txtMessage.Text;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("yourgmailemail@gmail.com", "yourpassword");
smtp.EnableSsl = true;
smtp.Send(Msg);
//Msg = null;
lbltxt.Text = "Thanks for Contact us";
// Clear the textbox valuess
txtName.Text = "";
txtSubject.Text = "";
txtMessage.Text = "";
txtEmail.Text = "";
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
VB.NET Code


Imports System.Net.Mail

Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim Msg As New MailMessage()
' Sender e-mail address.
Msg.From = New MailAddress(txtEmail.Text)
' Recipient e-mail address.
Msg.[To].Add("administrator@aspdotnet-suresh.com")
Msg.Subject = txtSubject.Text
Msg.Body = txtMessage.Text
' your remote SMTP server IP.
Dim smtp As New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.Port = 587
smtp.Credentials = New System.Net.NetworkCredential("yourgmailemail@gmail.com", "yourgmailpassword")
smtp.EnableSsl = True
smtp.Send(Msg)
'Msg = null;
lbltxt.Text = "Thanks for Contact us"
' Clear the textbox valuess
txtName.Text = ""
txtSubject.Text = ""
txtMessage.Text = ""
txtEmail.Text = ""
Catch ex As Exception
Console.WriteLine("{0} Exception caught.", ex)
End Try
End Sub
End Class
Demo



Download Sample code Attached

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

36 comments :

vipin pandey said...

hi suresh ji! you r doing great job.thanks for you articles.

sir,its my request to u ,please teach us MVC and WCF.
highly thankful to you.
please post the arctiles related to WCF and MVC,OTHER THAN EXISTING articles of WCF

Suresh Dasari said...

@Vipin pandey...
Thanks for your valuable suggestions sure i will write it but now a days i am not getting much time to do research with new concepts that's why it's taking time to update new concepts...

vipin pandey said...

Ok sir!! Thank u so much for giving me reply.

Shubham Choudhary said...

hi.. Sir great article by u... thanks to u
sir can u please give article for how to send message through website

Abhinavsingh993 said...

Hello Sir, This is Abhinav Kumar Singh (abhinavsingh993) one of your biggest fan of yours ..................as always I told about you ,you provide us a great medium for learning new things and everyday you realieses me through your new post there is no limit of knowledge ...............I am excited for your new post

Anonymous said...

hello sir,
gud article sir, but when i implement this code i found that the senders email is not displaying in recipient mail id.....will u please help me...it doesnt make any sense to get the feedback without senders mailid

Anonymous said...

please sir tell me this is not working means m not getting senders mailid

Msg.From = new MailAddress(txtEmail.Text);

Anonymous said...

we can include this in our body part so that it will display senders name email address and body content
msg.Body="Sender Name: " + txtname.Text + "

Sender Email: " + txtmail.Text + "

" +TextBox4.Text;

ankit ankitsaxena22@rediffmail.com

Anonymous said...

my complete coding is as follows

using System.Net.Mail;

public partial class feedback_form : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("ankitsaxena22@rediffmail.com");
msg.To.Add("ankitsaxena22@rediffmail.com");
msg.Subject = txtsub.Text;
msg.Body = "Sender Name: " + txtname.Text + "

Sender Email: " + txtmail.Text + "

" +TextBox4.Text;
SmtpClient smtp= new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port =587;
smtp.Credentials = new System.Net.NetworkCredential("ankitsxn39@gmail.com", "14821005");
smtp.EnableSsl = true;
smtp.Send(msg);


}
}

we can include this in our body part so that it will display senders name email address and body content
msg.Body="Sender Name: " + txtname.Text + "

Sender Email: " + txtmail.Text + "

" +TextBox4.Text;

ankit ankitsaxena22@rediffmail.com

thanks sir

Anonymous said...

okopidfuhgtrg

Shaik Mushtak said...
This comment has been removed by the author.
SAP - ERP (Ritesh Vyas) said...

Thanks for above mentioned example, It is helpful for new/fresher developer.

Regards,

Anonymous said...

"smtp.Credentials = New ;System.Net.NetworkCredential("yourgmailemail@gmail.com", "yourgmailpassword"):"
=====
i want to sent mail with different emailid(from one or more email id rather thn one mail id as you mentioned gmail id in above code) other thn gmail id ... how do i do tht.. pz help me....

Anonymous said...

Valuable website for C# code.

Sunil Pandya said...

Contact us page is for receiving message from user and not to send message to user. In this case IMAP has to used or SMTP?
Sunil pandya
sunil.pandya02@gmail.com

Anonymous said...

sir Using this code i m getting in both sender as well as reciver same email id and this is my email id(admin's Id) here senario is that customer sending us email so from address shold be customer's id and to address must be admin's id but i m getting same (admin's id ) any suggestion ?

Purv@ng P@ndy@ said...

sir Using this code i m getting in both sender as well as reciver same email id and this is my email id(admin's Id) here senario is that customer sending us email so from address shold be customer's id and to address must be admin's id but i m getting same (admin's id ) any suggestion ?

Atul shukla said...

grt

krishan said...

I used these codes, and made appropriate changes like assign my email id, and password, but it is not sending mail, neighter is showing any error. Please help me out.

Mudasir Hussain Bhat said...

Hi ,

i'm trying the above program in cs but getting error as below, please advice.


Line 2: using System.Net.Mail;
Line 3:
Line 4: public partial class feedbackfm: System.Web.UI.Page
Line 5:
Line 6: {

Anonymous said...

Now is the time to start earning. Be your own boss with http://www.pleasurebuilder.com.

Shank said...

Hi Suresh,
I like your coding. Now i find it very useful for my project. I ve a doubt. In your coding under smtp, you showed example for gmail. What's the code to cater to all types of email?

Miss.Shishil Patil said...

i want to enable another form button in asp.net using vb?? Please reply

Anonymous said...

hello sir,
I did everything as u said but i still didn't receive any email and when i login to my gmail it always says "google prevented a suspicious...blah blah". Pleas help!

In anticipation.

Navdeep saini said...

thank u very much sir...
it works properly.. :-)

Anas manath said...

msg.Body = "Sender Name: " + txtname.Text + "

if it is a Checkboxlist or dropdownlist means which extension will use instead of .TEXT???

Ritika Gupta said...

Dear sir,
This code is working on local host
but how to make it work on server

abhishek said...

dear sir..
thanku so much..

Madhukar Krishna said...

It works fine when I send it from my computer but it gives error " 5.5.1 Authentication Required." when I host it and send it from my website

madiha Naz said...

I download your sample code and I changed this line Msg.[To].Add("myemail@gmail.com") and I changed this line smtp.Credentials = New System.Net.NetworkCredential("myemail@gmail.com", "mypassword") . When after changing I run this I have not get any error after filling and submitting the contact form, but I don't get the " Thanks message" and don't get any email in my gmail account. What is the mistake? plz help.

siddharth said...

Hi suresh sir, I have doubt
New System.Net.NetworkCredential("yourgmailemail@gmail.com", "yourgmailpassword")
what could be in yourgmailemail@gmail.com and yourgmailpassword, I m getting confuse, at this place receiver email address and password or senders email and passwords ? if senders password is this then where is the textbox for password

siddharth said...

Because when I writ receiver email and password, I got the mail which sender and receiver email address is same

Anonymous said...

thnxxxxxx.........

neeraj said...

thanx

Anonymous said...

This is confirmed****
Change the email id fields and password field.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class ContactUs : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
try
{

MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtEmail.Text);
// Recipient e-mail address.
Msg.To.Add("ritadrikc90@gmail.com");
Msg.Subject = txtSubject.Text;
Msg.Body = "\n***Sender Name: " + txtName.Text + "\n***Sender Email: " + txtEmail.Text + "\n***Message:" + txtMessage.Text;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("ritadrikc90@gmail.com", "********");
smtp.EnableSsl = true;
smtp.Send(Msg);
//Msg = null;
lbltxt.Text = "Thanks for Contact us";
// Clear the textbox valuess
txtName.Text = "";
txtSubject.Text = "";
txtMessage.Text = "";
txtEmail.Text = "";
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
}

Anonymous said...

Hai, please help me...i want about or help coding in vb.net....

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.