Thursday 9 May 2013

Widgets

Send Mail Using Contact Us Form in Asp.Net

Design Page-:


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 id="Head1" runat="server">
<title>Contact Us In Asp.net</title>
<style type="text/css">
.Button
{
background-color :#0066CC;
color: #FFFFFF;
font-weight: bold;
margin-right: 2px;
padding: 4px 20px 4px 21px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<fieldset style="height: 242px; width: 470px">
<legend>Contact Us</legend>
<table cellspacing="2" cellpadding="2" border="0">


<tr><td>Name:-</td><td><asp:TextBox ID="txtName" runat="server" /></td></tr>


<tr><td>Email:-</td><td><asp:TextBox ID="txtEmail" runat="server" /></td></tr>

<tr><td>Subject:-</td><td><asp:TextBox ID="txtSubject" runat="server" /></td></tr>

<tr><td valign="top">Query:-</td>

<td> <asp:TextBox ID="txtMessage" Rows="5" Columns="40" TextMode="MultiLine" runat="server"/></td></tr>
<tr>
<td><asp:button ID="btnSubmitt" Text="Submit"  runat="server" onclick="btn_Click" CssClass="Button"/></td></tr>
<tr><td colspan="2" style=" color:red"><asp:Label ID="lbltxt" runat="server"/></td></tr>
</table>
</fieldset>
</form>
</body>
</html>

Codebehind-:


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btn_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage Msg = new MailMessage(); // Sender e-mail.
            Msg.From = new MailAddress(txtEmail.Text); // Recipient e-mail.
           
            Msg.To.Add("rahulupadhyay010@gmail.com");
            Msg.Subject = txtSubject.Text;
            Msg.Body = txtMessage.Text;
           
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
                             smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("gmailid@gmail.com", "password");
                                smtp.EnableSsl = true;
            smtp.Send(Msg);
         
            lbltxt.Text = "Thanks for Contacting ";
         
            txtName.Text = "";
            txtSubject.Text = "";
            txtMessage.Text = "";
            txtEmail.Text = "";
        }
        catch (Exception ex)
        {
            Console.WriteLine("{0} Exception caught.", ex);
        }    }}


Demo-:




0 comments:

Post a Comment