Pages

Thursday, July 25, 2013

How to Send Email in Asp.net

This article will be explain how to send email in Asp.net. To send email in Asp.net you have to use a namespace "System.Net.Mail".
Step 1
Open "Defaul.aspx" file and following control.
<form id="form1" runat="server">
    <div>
    </div>
        <asp:TextBox ID="FromText" runat="server" style="top: 69px; left: 452px;height: 22px; width: 257px"></asp:TextBox>
        <asp:TextBox ID="ToText" runat="server" style="top: 127px; left: 447px;height: 22px; width: 259px"></asp:TextBox>
        <asp:TextBox ID="SubText" runat="server" style="top: 179px; left: 449px;height: 22px; width: 258px"></asp:TextBox>
        <asp:TextBox ID="MsgText" runat="server" style="top: 231px; left: 454px;height: 148px; width: 252px"></asp:TextBox>
        <asp:Label ID="result" runat="server" style="top: 445px; left: 447px;height: 17px; width: 84px" Text="Label"></asp:Label>
        <asp:Button ID="Button1" runat="server" style="top: 436px; left: 551px;height: 26px; width: 56px" Text="Button" />
    </form>
Step 2
In "Default.aspx.cs" page write following code.
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.Drawing;
 
public partial class Email : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        MailMessage mm = new MailMessage();
 
        SmtpClient smtp = new SmtpClient();
 
        mm.From = new MailAddress(FromText.Text);
 
        mm.To.Add(new MailAddress(ToText.Text));
 
        mm.Subject = SubText.Text;
 
        mm.Body = MsgText.Text;
 
        mm.IsBodyHtml = true;
 
        smtp.Host = "smtp.gmail.com";
 
        smtp.EnableSsl = true;
 
        System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
 
        NetworkCred.UserName = "example@gmail.com";//Your email id will be here
 
        NetworkCred.Password = "Your Password";
        smtp.UseDefaultCredentials = true;
 
        smtp.Credentials = NetworkCred;
 
        smtp.Port = 587;
        try
        {
            smtp.Send(mm);
            result.Text = "your message has been sent.....";
            result.ForeColor = Color.Green;
        }
 
        catch (Exception er)
        {
            result.Text = "Error in sendig message";
            result.ForeColor = Color.Red;
        }
    }
}

Wednesday, July 24, 2013

How to Create Custom Login Page from Database Table in Asp.net

This article will be explain how to create custom login page from database table without using login control in Asp.Net. In this article we are creating login page as well as Registration page.
Step 1
In first step open your webconfig file and write following code in configuration.
<connectionStrings>
    <add name="ConnectionString" connectionString="Your connection string will be here"/>
  </connectionStrings>
  <system.web>
    <authentication mode="Forms">
      <forms loginUrl="Default.aspx"
         />
    </authentication>
    <authorization>
      <deny users="?" />
    </authorization>
  </system.web>
  <location path="Allow">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>
Step 2
In step 2 open Default.aspx page and add following control.
<form id="form1" runat="server">
        <div>
            <p>
                <strong>Email:</strong>
                <br />
                <asp:TextBox ID="txtUName" runat="server" Columns="50"></asp:TextBox>
            </p>
            <p>
                <strong>Password:</strong>
                <br />
                <asp:TextBox ID="txtPass" runat="server" Columns="50" TextMode="Password"></asp:TextBox>
            </p>
            <p>
                <asp:Button ID="btnLogin" runat="server" Text="Logon" OnClick="bnLogon_Click"/>
                <br />
                <asp:Label ID="lblMessage" ForeColor="red" runat="server" />
                <asp:LinkButton ID="BtnReg" runat="server"OnClick="LinkButton1_Click">Registration</asp:LinkButton>
            </p>
        </div>
    </form>
Step 3
In Defalut.aspx.cs page add following code.
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.SqlClient;
using System.Security.Cryptography;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(Request.Params["logout"]))
        {
            FormsAuthentication.SignOut();
            Response.Redirect("./");
        }
    }
    protected void bnLogon_Click(object sender, EventArgs e)
    {
        if (ValidUser(txtUName.Text, txtPass.Text))
        {
             Response.Redirect("welcome.aspx");
        }
        else
        {
            lblMessage.Text = "Incorrect";
        }
    }
    bool ValidUser(string user, string pass)
    {
        string connStr =ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            conn.Open();
            string sql = "select * from LoginTable where email = @email and password = @password";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@email", user);
            cmd.Parameters.AddWithValue("@password", pass);
            return cmd.ExecuteScalar() is string;
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Allow/Registration.aspx");
    }
}