How to Create Contact Us Page Form in C#, VB.NET


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


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
Create contact us page in c#, VB.NET

contact us form

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)
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtEmail.Text);
// Recipient e-mail address.
Msg.Subject = txtSubject.Text;
Msg.Body = txtMessage.Text;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("", "yourpassword");
smtp.EnableSsl = true;
//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);


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)
Dim Msg As New MailMessage()
' Sender e-mail address.
Msg.From = New MailAddress(txtEmail.Text)
' Recipient e-mail address.
Msg.Subject = txtSubject.Text
Msg.Body = txtMessage.Text
' your remote SMTP server IP.
Dim smtp As New SmtpClient()
smtp.Host = ""
smtp.Port = 587
smtp.Credentials = New System.Net.NetworkCredential("", "yourgmailpassword")
smtp.EnableSsl = True
'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

Download Sample code Attached

| Tags:
  • 2725 View
  • Pin It