Saturday, 17 March 2012

Activation Email for user registration in asp.net using c#

Registration page.aspx::

using System;
using System.Collections;
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;
using System.Net;


public partial class Home : System.Web.UI.Page
{
    public static string status = "false";
    protected void Page_Load(object sender, EventArgs e)
    {


    }


    protected void ImageButtonSubmit_Click(object sender, ImageClickEventArgs e)
    {
        Dal info = new Dal();
        int count = Convert.ToInt32(info.PatientEmailCount(txt_email.Value));
        if (count > 0)
        {
            Response.Write("<script language='javascript'>alert('Email Already Exists');</script>");
            
        }
        else
        {
            info.InserPatient(txt_email.Value, txt_password.Value, txt_username.Value,status);




            string strbody = "";
            strbody += "<table style=width: 653px><tr><td style=width: 163px><span style=font-weight :bold>Hi " + txt_username.Value + ",</span><br /><br /></td></tr>";
            strbody += "<tr><td>Thank you for registering into our site.<br /></td></tr><tr><td> To verify your account <a href='http://localhost:3572/PatientsGlucoseLog/Activate.aspx?_email=" + txt_email.Value + "'>click here</a><br /><br /></td></tr>";
            strbody += "<tr><td style=width: 163px><span style=text-decoration: underline>If the above link does not work:Copy the below link</span></td></tr>";
            strbody += "<tr><td>http://localhost:3572/PatientsGlucoseLog/Activate.aspx?_email=" + txt_email.Value + "<br/><br /></td></tr>";
            strbody += "<tr><td style=height: 12px align=left> Paste it in the address bar of your browser</td></tr>";
            strbody += "<tr><td align=left>and click Go to activate your account.<br /><br /></td></tr><tr><td style=width: 163px valign=bottom><span style=text-decoration: underline>";
            strbody += "Once verified, you can login with the following credentials:<br /></span><br /></td></tr><tr><td style=width: 163px>Username:" + txt_username.Value + "</td></tr><tr><td style=width: 163px>Password:" + txt_password.Value + "<br /><br /></td></tr>";
            strbody += "<tr><td align=left>Regards,<br/></td></tr><tr><td>NollywoodShops.</td></table>";




            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            NetworkCredential NetworkCred = new NetworkCredential();
            NetworkCred.UserName = "nanna.nagireddy.vsp@gmail.com";
            NetworkCred.Password = "thanks123";
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            msg.To.Add(txt_email.Value);
            msg.From = new MailAddress("nanna.nagireddy.vsp@gmail.com");
            msg.Subject = "My Exported GridView";
            msg.Body = strbody;
            msg.IsBodyHtml = true;
            smtp.Send(msg);


            Response.Write("<script language='javascript' type='text/javascript'> alert('A link has been sent to your Email-id to activate your account.Please go through It.')</script>");




            Response.Redirect("Login.aspx");
        }
    }
}

Activate.aspx::
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Activate : System.Web.UI.Page
{
    Dal info = new Dal();
    protected void Page_Load(object sender, EventArgs e)
    {       

        string Email = Request.QueryString["_email"].ToString();
        string status="true";
        info.UpdatePatientStatus(Email, status);
    }
    protected void LinkButton_clickhere_Click(object sender, EventArgs e)
    {
        string Email = Request.QueryString["_email"].ToString();
        string status = "true";
        info.UpdatePatientStatus(Email, status);

        Response.Redirect("Login.aspx");
    }
}

Dal.cs::

public void InserPatient(string Email, string Name, string Password, string status)
    {
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        cmd = new SqlCommand("SP_patientregister", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = Email;
        cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = Name;
        cmd.Parameters.Add("@Password", SqlDbType.NVarChar).Value = Password;
        cmd.Parameters.Add("@status", SqlDbType.NVarChar).Value = status;       
        cmd.ExecuteNonQuery();
        con.Close();
    }

    public int PatientEmailCount(string Email)
    {
        

        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        cmd = new SqlCommand("Sp_patientemailcount", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = Email;
        int count = Convert.ToInt32(cmd.ExecuteScalar());
        cmd.ExecuteNonQuery();
        con.Close();
        return count;
    }

    public void UpdatePatientStatus(string Email, string status)
    {
        
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        cmd2 = new SqlCommand("SP_UpdatePatientStatus", con);
        cmd2.CommandType = CommandType.StoredProcedure;
        cmd2.Parameters.Add("@Email", SqlDbType.NVarChar).Value = Email;
        cmd2.Parameters.Add("@status", SqlDbType.NChar).Value = status;
        cmd2.ExecuteNonQuery();
        con.Close();
    }

1 comment:

  1. Please provide SP_patientregister stored procedure.

    ReplyDelete