Monday, 30 March 2015

Send email using Gmail SMTP Using Asp.net || How to send mail by using smtp in asp.net

Sending e-mails with ASP.NET is pretty straight forward. The .NET framework comes with an entire namespace for handling e-mails, the System.Net.Mail namespace




HTML Markup
The HTML Markup has a form with some fields such as Recipient Email address, Subject, Body, Attachment, Gmail account email address, Gmail account password and a Button to send the email.
 
<body>
    <form id="form1" runat="server">
    <div align="center">
    <h3>Send Mail To Your's Gmail Using ASp.net</h3>
    <table>
   
    <tr>
    <td>Gmail User</td>
    <td>:</td>
    <td>
    <asp:TextBox ID="txtfrom" runat="server" ></asp:TextBox>
    </td>
   
   
    </tr>
    <tr>
    <td>Gmail Password</td>
    <td>:</td>
    <td>
    <asp:TextBox ID="txtpassword" TextMode="Password" runat="server" ></asp:TextBox>
    </td>
   
   
    </tr>
    <tr>
    <td>Subject</td>
    <td>:</td>
    <td>
    <asp:TextBox ID="txtsub" runat="server" ></asp:TextBox>
    </td>
   
   
    </tr>
    <tr>
    <td>To</td>
    <td>:</td>
    <td>
    <asp:TextBox ID="txtto" runat="server" ></asp:TextBox>
    </td>
   
   
    </tr>
    <tr>
    <td>Body</td>
    <td>:</td>
    <td>
    <asp:TextBox ID="txtbody" runat="server"  TextMode="MultiLine" Columns="30" Rows="10"></asp:TextBox>
    </td>
   
   
    </tr>
    <tr>
   
    <td></td><td></td>
    <td>
   
        <br />
   
    <asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="btnsubmit_Click" />
    </td>
    </tr>
    </table>
   
    </div>
    </form>
</body>
 
 

MailMessage Class Properties

Following are the required properties of the MailMessage class.
From – Sender’s email address
To – Recipient(s) Email Address
CC – Carbon Copies (if any)
BCC – Blind Carbon Copies (if any)
Subject – Subject of the Email 
Body – Body of the Email
IsBodyHtml – Specify whether body contains text or HTML mark up.
Attachments – Attachments (if any)
ReplyTo – ReplyTo Email address.
 
SMTP Class Properties
Following are the properties of the SMTP class.
Host – SMTP Server URL (Gmail: smtp.gmail.com)
EnableSsl – Specify whether your host accepts SSL Connections (Gmail: True)
UseDefaultCredentials – Set to True in order to allow authentication based on the Credentials of the Account used to send emails
Credentials – Valid login credentials for the SMTP server (Gmail: email address and password)
Port – Port Number of the SMTP server (Gmail: 587)
 
Namespaces
You will need to import the following namespaces
C#

using System.Net;
using System.Net.Mail;
 
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
 
ASPX.CS Code:



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

    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
       MailMessage newmsg = new MailMessage();

       newmsg.From = new MailAddress(txtfrom.Text);
      
        newmsg.Subject = txtsub.Text;
        newmsg.To.Add(txtto.Text);
        newmsg.Body = txtbody.Text;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.Credentials = new System.Net.NetworkCredential(txtfrom.Text, txtpassword.Text);
         
        smtp.EnableSsl = true;
        smtp.Send(newmsg);
        Response.Write("msg is send");

    }
}





Wednesday, 4 February 2015

Simple Login Form example in ASP.Net - Dotnetprograms

Hai friends now i am going to explain about Login page by checking availability in database or not.If the user enter the username and password it check in the database if it is valid it redirect to ("HomePage.aspx") otherwise it dispaly the message as "Invalid User Name or Password".

Step 1: Login Page which is give below:



 

To implement this one first design table like this:

 

 




Step :2  Writing Stored Procedure For Login Control

The COUNT() function returns the number of rows that matches a specified criteria

Stored Procedure for Login Form:-
create procedure splogin
@spemailid varchar(30),@sppassword varchar(30)
as
Begin
Select COUNT(*)from student1 where email=@spemailid and password=@sppassword
End



Step 3: Designing  Login Page using asp.net and html controls


<div align="center">
            <fieldset style="width: 350px">
                <legend>Login Page   </legend>
                &nbsp;
   


            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
                <br />
                <br />
                <table>

                    <tr>
                        <td><b>Enter User Name</b>
                        </td>
                        <td>:
                        </td>
                        <td>
                            <asp:TextBox ID="TextBox1" runat="server" BackColor="#FF5050"></asp:TextBox></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><b>Enter password</b>
                        </td>
                        <td>:
                        </td>
                        <td>
                            <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"
                                BackColor="#FF5050"></asp:TextBox></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td></td>
                    </tr>

                    <tr>
                        <td></td>
                        <td>
                            <br />
                        </td>
                    </tr>

                    <tr>
                        <td></td>
                        <td>
                            <br />
                        </td>

                        <td>
                            <asp:Button ID="Button1" runat="server" Text="Submit"  OnClick="Button1_Click1"/>&nbsp;&nbsp;&nbsp;

<asp:Button ID="Button2"  runat="server" Text="Cancel" />
                        </td>


                    </tr>
                </table>
                <h3>Check Username and Password availability in database</h3>

            </fieldset>
        </div>


 Step :4  Code For Login Page.aspx.cs page
 
protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connection);
            SqlCommand cmd = new SqlCommand("splogin", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@spemailid", TextBox1.Text);
            cmd.Parameters.AddWithValue("@sppassword", TextBox2.Text);


            con.Open();

            int usercount = (Int32)cmd.ExecuteScalar();// for taking single value

            if (usercount == 1)  // comparing users from table
            {
                Response.Redirect("HomePage.aspx");  //for sucsseful login
            }
            else
            {
                con.Close();
                Label1.Text = "Invalid User Name or Password";  //for invalid login
            }
        }