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.
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
<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
No comments:
Post a Comment