How to create Contact Us page in Asp.net



Description:

In this article I have explained how we can create contact us page in Asp,net. 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:

Default.aspx:-

<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>

Namespaces:-

using System.Net.Mail;

Default.aspx.cs:-

    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);
    }

Related Posts

Previous
Next Post »

Thanks for comments.....