Tuesday, October 29, 2013

How to create Contact Us page in Asp.net

Introduction: In this article I have explained how we can create contact us page in Asp,net.
Contact Us

Contact us page is a major part of websites through which users send comments, queries feedback etc. of website to admin/website owner.

Add a new webform to website. Drag and drop the Textbox, button, validation controls from Toolbox and desgin the .aspx page as shown below:
<table align="center">
    <tr><td><h1>Contact Us</h1></td><td></td></tr>
    <tr><td>Name:</td><td><asp:TextBox ID="txtname" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="rfvName" runat="server"
                                        ControlToValidate="txtname" Display="None"
                                        ErrorMessage="Enter Name" EnableTheming="True"></asp:RequiredFieldValidator></td></tr>
     <tr><td>Email:</td><td><asp:TextBox ID="txtemail" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="rfvrequireemail" runat="server"
                                        ControlToValidate="txtemail" Display="None"
                                        ErrorMessage="Enter Email" EnableTheming="True"></asp:RequiredFieldValidator>
         <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
             ControlToValidate="txtemail" ErrorMessage="Enter Valid Email"
             ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
         </td></tr>
      <tr><td>Subject:</td><td><asp:TextBox ID="txtsubject" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="rfvsubject" runat="server"
                                        ControlToValidate="txtsubject" Display="None"
                                        ErrorMessage="Enter Subject" EnableTheming="True"></asp:RequiredFieldValidator></td></tr>
       <tr><td>Description:</td><td><asp:TextBox ID="txtdescription" runat="server" TextMode="MultiLine" textl></asp:TextBox> <asp:RequiredFieldValidator ID="rfvdescription" runat="server"
                                        ControlToValidate="txtdescription" Display="None"
                                        ErrorMessage="Enter Description" EnableTheming="True"></asp:RequiredFieldValidator></td></tr>
                                        <asp:ValidationSummary ID="ValidationSummary1" runat="server"  ShowMessageBox="true" ShowSummary="false"/>
        <tr><td>&nbsp;</td><td><asp:button ID="btnsend" runat="server" Text="Send"
                onclick="btnsend_Click" /></td></tr>
    </table>

After that on button click (.aspx.cs) write the given code:
using System.Net.Mail;

protected void btnsend_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage msg = new MailMessage("Sender Email Id","Recevier Email Id");
            msg.Subject = txtsubject.Text;
            msg.Body = "Name:"+txtname.Text+"<br> Email:"+txtemail.Text+"<br>" + txtsubject.Text;
            msg.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("Email Id", "Password");
                smtp.EnableSsl = true;
            smtp.Send(msg);
            Clear();
            Messagebox("Mail send Successfully");
        }
        catch (Exception ex)
        {
        }
    }
    private void Clear()
    {
        txtname.Text = "";
        txtsubject.Text = "";
        txtemail.Text = "";
        txtdescription.Text = "";
    }
    //Show Message
    private void Messagebox(string Message)
    {
        Label lblMessageBox = new Label();

        lblMessageBox.Text =
            "<script language='javascript'>" + Environment.NewLine +
            "window.alert('" + Message + "')</script>";
        Page.Controls.Add(lblMessageBox);
    }

In VB (.aspx.vb)
Imports System.Net.Mail

Protected Sub btnsend_Click(sender As Object, e As System.EventArgs) Handles btnsend.Click
        Try
            Dim msg As New MailMessage("Sender Email Id","Recevier Email Id ")
            msg.Subject = txtsubject.Text
            msg.Body = (("Name:" + txtname.Text & "<br> Email:") + txtemail.Text & "<br>") + txtsubject.Text
            msg.IsBodyHtml = True
            Dim smtp As New SmtpClient()
            smtp.Host = "smtp.gmail.com"
            smtp.Port = 587
            smtp.Credentials = New System.Net.NetworkCredential("Email Id", "Password")
            smtp.EnableSsl = True
            smtp.Send(msg)
            Clear()
            Messagebox("mail Send Successfully")
        Catch ex As Exception
        End Try
    End Sub
    Private Sub Clear()
        txtname.Text = ""
        txtsubject.Text = ""
        txtemail.Text = ""
        txtdescription.Text = ""
    End Sub
    'Show Message
    Private Sub Messagebox(Message As String)
        Dim lblMessageBox As New Label()
        lblMessageBox.Text = "<script language='javascript'>" + Environment.NewLine & "window.alert('" & Message & "')</script>"
        Page.Controls.Add(lblMessageBox)
    End Sub


Run the project and check the result.

Is it helpful?

If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment