Contact us form in Asp.net c#

In this article, we send a mail to the admin after filling the contact us . Maximum website or blog have contained the contact us. Many time, we think that how to send a mail for contact us form. Below code it will help you for this.


Design:


Source Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="contactus.aspx.cs" Inherits="contactus" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <fieldset style="width: 300px;">
            <legend>Contact Us Form</legend>
            <table>
                <tr>
                    <td>
                        Name
                    </td>
                    <td>
                        <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        Conatct Number
                    </td>
                    <td>
                        <asp:TextBox ID="txtcontact" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        Email
                    </td>
                    <td>
                        <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        Message
                    </td>
                    <td>
                        <asp:TextBox ID="txtmessage" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="center" colspan="2">
                        <asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClick="btnsubmit_Click" />
                    </td>
                </tr>
            </table>
        </fieldset>
    </div>
    </form>
</body>
</html>

Code behind(C#):-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Text;

public partial class contactus : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    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("sender@gmail.com");


            //Meaages Subject
            Msg.Subject = "Contact Us Form-Neerajcodesolutions";
            StringBuilder sb = new StringBuilder();
            sb.Append("Name :" + txtname.Text + "\r\n");
            sb.Append("Contact:" + txtcontact.Text + "\r\n");
            sb.Append("Email:" + txtemail.Text + "\r\n");
            sb.Append("Message:" + txtmessage.Text + "\r\n");

            Msg.Body = sb.ToString();
            // SMTP server IP.
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 25;
            smtp.Credentials = new System.Net.NetworkCredential("sender@gmail.com", "password");
            smtp.EnableSsl = true;
            smtp.Send(Msg);
            //Mail Message
            Response.Write("<Script>alert('Thanks for contact us,our team will be contact you as soon as possible')</Script>");
            // Clear the textbox values
            txtname.Text = "";
            txtcontact.Text = "";
            txtemail.Text = "";
            txtmessage.Text = "";
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);

        }
    }
}

Out-Put:-



Contact us form in Asp.net c# Contact us form in Asp.net c# Reviewed by NEERAJ SRIVASTAVA on 9:38:00 PM Rating: 5

12 comments:

  1. This is the best tutorial I have seen for ASP.NET contact form thank you!

    ReplyDelete
  2. Thank to learn me that how to make real life example of contact us

    ReplyDelete
  3. Thanks Bro, i'm a beginner in asp programing and your article really awesome.

    ReplyDelete
  4. Replies
    1. dear @netsys , this code 100% working , please check once again in your code

      Delete
  5. not working code
    eroor comes :-Service not available, closing transmission channel. The server response was: Cannot connect to SMTP server 74.125.200.108 (74.125.200.108:25), NB connect error 1460

    ReplyDelete
  6. Sir,
    but there is no textbox field for entering password by user.
    plz help thanx

    ReplyDelete
  7. hii sir can u plz upload a tutorial about admin panel in dynamic website tnx...

    ReplyDelete
  8. thanks man i was stuck for weeks u deserve millions hugs keep up the good job

    ReplyDelete
  9. Hi, thanks for this code!

    I'm a newbie trying to finish my assignment and I'm just a bit confused about where to actually put the code in my project. I have put the first half of the code on my actual 'Contact.cshtml' page and place the "code behind c#" on my Home Controller page. I am getting an error message "txtemail does not exist in the current context" and the same for txtname, txtmessage etc.

    I don't have a default.aspx page in my project.

    HELP please!

    ReplyDelete
  10. vey helpful! keep up the great work..........

    ReplyDelete

Powered by Blogger.