How to send mail in C#?

.........................login soure...........

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>

<!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>
    <style type="text/css">
        .style1
        {
            width: 100%;
            background-color: #FFCCFF;
        }
        .style2
        {
            width: 107px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td class="style2">
                    LoginId</td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                        Height="16px" Width="158px">
                        <asp:ListItem>Admin</asp:ListItem>
                        <asp:ListItem>User</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Username</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" Height="16px" Width="155px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Password</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server" Height="16px" Width="155px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Login" />
                </td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>
..........................................end login source code.................
..................code behind.........................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Text;
using System.Data.SqlClient;
using System.IO;

public partial class login : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd;
    SqlDataReader dr;
    DataSet ds = new DataSet();
    SqlDataAdapter da;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {

            if (DropDownList1.SelectedValue == "User")
            {
                con.Open();
                Session["user"] = "1";
                cmd = new SqlCommand("select*from textinsert where name='" + TextBox2.Text + "' and pwd='" + TextBox3.Text + "'", con);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    Session["name"]=TextBox2.Text;
                    Session["pwd"]=TextBox3.Text;
                    Response.Redirect("registration.aspx?name="+Session["name"]+" and pwd="+ Session["pwd"]+"");
                }

            }
            if (DropDownList1.SelectedValue == "Admin")
            {
                Session["user"] = "2";
                con.Open();
                cmd = new SqlCommand("select*from textinsert where name='" + TextBox2.Text + "' and pwd='" + TextBox3.Text + "'", con);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    Response.Redirect("registration.aspx");
                }
            }
        }
        catch(Exception ex)
        {
        }
    }

    }


    
....................................registration source code......................
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="registration.aspx.cs" Inherits="registration" %>

<!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>
    <style type="text/css">
        .style1
        {
            width: 100%;
            background-color: #FF9999;
        }
        .style2
        {
            width: 109px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table class="style1">
            <tr>
                <td class="style2">
                    Name</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Password</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Address</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
                </td>
            </tr>
        </table>
    
    </div>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </p>
    </form>
</body>
</html>
..........................code behind..........
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Net.Mail;
public partial class registration : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd;
    SqlDataReader dr;
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {   
            string str=Session["user"].ToString();

            if (str=="1")
            {
                user();
            }
            if(str=="2")
            {
                adminbind();
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        cmd = new SqlCommand("insert into textinsert values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')", con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        //Response.Write("successfully submitted!");
        cmd = new SqlCommand("select count(id) as id from textinsert", con);
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        if(dt.Rows.Count>0)
        {
            
            Int64 count=(Convert.ToInt64(dt.Rows[0]["id"].ToString()));
            string message;

            if (count > 4)
            {
                if (count == 4)
                {
                    message="you got first step";
                    send_email(message);
                }
                if (count == 16)
                {
                    message="you got second step";
                    send_email(message);
                }

                if (count == 50)
                {
                    message = "you third first step";
                    send_email(message);
                }
            }

        }
      
    }

    public void send_email(string msg)
    {
        MailMessage ms = new MailMessage("kulngr21@gmail.com", "kulngr@hotmail.com");
        SmtpClient ab = new SmtpClient();

        ms.Body = msg;
        ms.IsBodyHtml = true;
        ab.EnableSsl = true;
        ab.Send(ms);

        Response.Write("mmail sent...");
    }
     
    protected void adminbind()
    {
        con.Open();
        // string name=Request.QueryString["name"].ToString();
        // string pwd=Request.QueryString["pwd"].ToString();
        string str = "select * from textInsert ";
        da = new SqlDataAdapter(str, con);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
    SqlDataAdapter da1;
    protected void user()
    {
        try
        {
            con.Open();
            string name = Session["name"].ToString();
            string str = "select*from textinsert where name='" + name + "'";
            da1 = new SqlDataAdapter(str,con);
            DataTable dt = new DataTable();
            da1.Fill(dt);
          
          
                GridView1.DataSource = dt;
                GridView1.DataBind();
          
            con.Close();
        }
        catch(Exception ex)
        {

        }
    }
}
.........................................end code behind.....................

Share:

No comments:

Post a Comment

Friday, 21 December 2012

How to send mail in C#?

.........................login soure...........

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>

<!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>
    <style type="text/css">
        .style1
        {
            width: 100%;
            background-color: #FFCCFF;
        }
        .style2
        {
            width: 107px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td class="style2">
                    LoginId</td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                        Height="16px" Width="158px">
                        <asp:ListItem>Admin</asp:ListItem>
                        <asp:ListItem>User</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Username</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" Height="16px" Width="155px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Password</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server" Height="16px" Width="155px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Login" />
                </td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>
..........................................end login source code.................
..................code behind.........................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Text;
using System.Data.SqlClient;
using System.IO;

public partial class login : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd;
    SqlDataReader dr;
    DataSet ds = new DataSet();
    SqlDataAdapter da;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {

            if (DropDownList1.SelectedValue == "User")
            {
                con.Open();
                Session["user"] = "1";
                cmd = new SqlCommand("select*from textinsert where name='" + TextBox2.Text + "' and pwd='" + TextBox3.Text + "'", con);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    Session["name"]=TextBox2.Text;
                    Session["pwd"]=TextBox3.Text;
                    Response.Redirect("registration.aspx?name="+Session["name"]+" and pwd="+ Session["pwd"]+"");
                }

            }
            if (DropDownList1.SelectedValue == "Admin")
            {
                Session["user"] = "2";
                con.Open();
                cmd = new SqlCommand("select*from textinsert where name='" + TextBox2.Text + "' and pwd='" + TextBox3.Text + "'", con);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    Response.Redirect("registration.aspx");
                }
            }
        }
        catch(Exception ex)
        {
        }
    }

    }


    
....................................registration source code......................
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="registration.aspx.cs" Inherits="registration" %>

<!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>
    <style type="text/css">
        .style1
        {
            width: 100%;
            background-color: #FF9999;
        }
        .style2
        {
            width: 109px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table class="style1">
            <tr>
                <td class="style2">
                    Name</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Password</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Address</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
                </td>
            </tr>
        </table>
    
    </div>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </p>
    </form>
</body>
</html>
..........................code behind..........
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Net.Mail;
public partial class registration : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd;
    SqlDataReader dr;
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {   
            string str=Session["user"].ToString();

            if (str=="1")
            {
                user();
            }
            if(str=="2")
            {
                adminbind();
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        cmd = new SqlCommand("insert into textinsert values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')", con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        //Response.Write("successfully submitted!");
        cmd = new SqlCommand("select count(id) as id from textinsert", con);
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        if(dt.Rows.Count>0)
        {
            
            Int64 count=(Convert.ToInt64(dt.Rows[0]["id"].ToString()));
            string message;

            if (count > 4)
            {
                if (count == 4)
                {
                    message="you got first step";
                    send_email(message);
                }
                if (count == 16)
                {
                    message="you got second step";
                    send_email(message);
                }

                if (count == 50)
                {
                    message = "you third first step";
                    send_email(message);
                }
            }

        }
      
    }

    public void send_email(string msg)
    {
        MailMessage ms = new MailMessage("kulngr21@gmail.com", "kulngr@hotmail.com");
        SmtpClient ab = new SmtpClient();

        ms.Body = msg;
        ms.IsBodyHtml = true;
        ab.EnableSsl = true;
        ab.Send(ms);

        Response.Write("mmail sent...");
    }
     
    protected void adminbind()
    {
        con.Open();
        // string name=Request.QueryString["name"].ToString();
        // string pwd=Request.QueryString["pwd"].ToString();
        string str = "select * from textInsert ";
        da = new SqlDataAdapter(str, con);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
    SqlDataAdapter da1;
    protected void user()
    {
        try
        {
            con.Open();
            string name = Session["name"].ToString();
            string str = "select*from textinsert where name='" + name + "'";
            da1 = new SqlDataAdapter(str,con);
            DataTable dt = new DataTable();
            da1.Fill(dt);
          
          
                GridView1.DataSource = dt;
                GridView1.DataBind();
          
            con.Close();
        }
        catch(Exception ex)
        {

        }
    }
}
.........................................end code behind.....................

No comments:

Post a Comment

Popular

Blog Archive

Total Pageviews

Archive