Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
503 views

Create Contact Us Form in ASP - Net Using C# and VB NET

This document explains how to create a contact us form in ASP.NET using C# and VB.NET that sends email. It describes placing text boxes and validators on an ASPX page to collect a name, email, subject, and message from users. It then shows code to send an email with the form data to an administrator's email using Gmail SMTP. The code creates an email message with the form values, sets SMTP server properties, and sends the message.

Uploaded by

wasim ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
503 views

Create Contact Us Form in ASP - Net Using C# and VB NET

This document explains how to create a contact us form in ASP.NET using C# and VB.NET that sends email. It describes placing text boxes and validators on an ASPX page to collect a name, email, subject, and message from users. It then shows code to send an email with the form data to an administrator's email using Gmail SMTP. The code creates an email message with the form values, sets SMTP server properties, and sends the message.

Uploaded by

wasim ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

In this example i'm explaining how to Create Contact Us Form In Asp.Net Using C# And VB.NET.

ContactUs Page is essential part of any web site or application, through which users send comments or feedback
about site or can contact site admins.
To create contact form in asp.net web applications we need a working SMTP server through which aspx page can
send the form data to admins email id.
For this example i'll use Gmail account to send mail on behalf of application to admins gmail id.
Place three Textbox on the page for users to input their name, email id, and subject, add another textbox for entering
feedback or comment and set it's TextMode property to MultiLine.
Add RequiredFieldValidators to ensure entry in every field, add one button to send the mail.
HTML SOURCE OF PAGE

1: <html xmlns="http://www.w3.org/1999/xhtml">
2: <head runat="server">
3: <title>Contact Us</title>
4: <link href="StyleSheet.css" rel="stylesheet"
type="text/css" />
5: </head>
6: <body>
7: <form id="ContactForm" runat="server">
8: <div>
9:
<fieldset>
10: <legend>Contact us</legend>
11: <div class='short_explanation'>* required fields</div>
12:
13: <div class='container'>
14: <asp:Label ID="lblName" runat="server"
15:
Text="Your Name*:" CssClass="label"/><br/>
16: <asp:TextBox ID="txtName" runat="server"/>
17: <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
18:
runat="server"
19:
ControlToValidate="txtName"
20:
ErrorMessage="Enter Your Name"
21:
SetFocusOnError="True">*
22: </asp:RequiredFieldValidator><br />
23: </div>
24:
25: <div class='container'>
26: <asp:Label ID="lblEmail" runat="server"
27:
Text="Email*:" CssClass="label"/><br/>
28: <asp:TextBox ID="txtMail" runat="server"/>
29: <asp:RequiredFieldValidator ID="RequiredFieldValidator2"
30:
runat="server"
31:
ControlToValidate="txtMail"
32:
ErrorMessage="Please Provide

33:
Your Email
Address"
34:
SetFocusOnError="True">*
35: </asp:RequiredFieldValidator><br />
36: </div>
37:
38: <div class='container'>
39: <asp:Label ID="lblSubject" runat="server"
40:
Text="Subject*:" CssClass="label"/><br/>
41: <asp:TextBox ID="txtSubject" runat="server"/>
42: <asp:RequiredFieldValidator ID="RequiredFieldValidator3"
43:
runat="server"
44:
ControlToValidate="txtSubject"
45:
ErrorMessage="Please provide
46:
reason to contact
us"
47:
SetFocusOnError="True">*
48: </asp:RequiredFieldValidator><br />
49: </div>
50:
51: <div class='container'>
52: <asp:Label ID="lblMessage" runat="server"
53:
Text="Feedback:" CssClass="label"/><br/>
54: <asp:TextBox ID="txtMessage" runat="server"
55:
TextMode="MultiLine" Width="268px"/>
56: <asp:RequiredFieldValidator ID="RequiredFieldValidator4"
57:
runat="server"
58:
ControlToValidate="txtMessage"
59:
ErrorMessage="Write your
feedback"
60:
SetFocusOnError="True">*
61: </asp:RequiredFieldValidator><br />
62: </div>
63:
64: <div class='container'>
65: <asp:Button ID="btnSubmit" runat="server"
66:
Text="Submit" onclick="btnSubmit_Click"/>
67: </div>
68: <asp:ValidationSummary ID="ValidationSummary1"
69:
runat="server" CssClass="error"/>
70: </fieldset>
71: <asp:Label ID="Label1" runat="server" Text=""/>
72: </div>
73:
</form>
74: </body>
75: </html>

Write following code in Click Event of submit button.


C#
01using System;
02using System.Net.Mail;
03protected void btnSubmit_Click(object sender, EventArgs e)
04 {
05
MailMessage feedBack = new MailMessage();
06
feedBack.To.Add("YourGmailId@gmail.com");
07
feedBack.From = new MailAddress("YourGmailId@gmail.com");
08
feedBack.Subject = txtSubject.Text;
09
10
feedBack.Body = "Sender Name: " + txtName.Text + "<br><br>Sender Email: " + txtMail.Text
+ "<br><br>" + txtMessage.Text;
11
feedBack.IsBodyHtml = true;
12
SmtpClient smtp = new SmtpClient();
13
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
14
smtp.Port = 587;
15
smtp.EnableSsl = true;
16
smtp.Credentials = new System.Net.NetworkCredential("YourGmailId@gmail.com",
"YourGmailPassword");
17
//Or your Smtp Email ID and Password
18
smtp.Send(feedBack);
19
Label1.Text = "Thanks for contacting us";
20 }
VB.NET
01Protected Sub btnSubmit_Click(sender As Object, e As EventArgs)
02 Dim feedBack As New MailMessage()
03 feedBack.[To].Add("YourGmailId@gmail.com")
04 feedBack.From = New MailAddress("YourGmailId@gmail.com")
05 feedBack.Subject = txtSubject.Text
06
07 feedBack.Body = (("Sender Name: " + txtName.Text & "<br><br>Sender Email: ") + txtMail.Text &
"<br><br>") + txtMessage.Text
08 feedBack.IsBodyHtml = True
09 Dim smtp As New SmtpClient()
10 smtp.Host = "smtp.gmail.com"
11 'Or Your SMTP Server Address
12 smtp.Port = 587
13 smtp.EnableSsl = True
14 smtp.Credentials = New System.Net.NetworkCredential("YourGmailId@gmail.com",
"YourGmailPassword")
15 'Or your Smtp Email ID and Password
16 smtp.Send(feedBack)
17 Label1.Text = "Thanks for contacting us"
18End Sub

You might also like