public bool SaveForgotPassDetails(ForgotPasswordBE forgotpass)
        {
            CommonDAL commondal = new CommonDAL();
            SqlConnection con = commondal.Connection();
            SqlCommand cmd = new SqlCommand();

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Sp_UM_ForgotPasswordInsert";

            cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = forgotpass.UserName;
            cmd.Parameters.Add("@EmailId", SqlDbType.VarChar).Value = forgotpass.PrimaryEmailId;
            cmd.Parameters.Add("@SecretQuestion", SqlDbType.VarChar).Value = forgotpass.SecretQuest;
            cmd.Parameters.Add("@SecretAns", SqlDbType.VarChar).Value = forgotpass.SecretAns;
            cmd.Connection = con;

            //con.Open();
            int count = cmd.ExecuteNonQuery();
            con.Close();

            if (count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool ForgotPassword(ref DataTable dt, ForgotPasswordBE forgotpass)
        {
            CommonDAL commondal = new CommonDAL();
            SqlConnection con = commondal.Connection();


            SqlDataAdapter da = new SqlDataAdapter("Sp_UM_ForgotPasswordGetDetailsofUserByUserName", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;

            SqlParameter p1 = new SqlParameter("@UserName ", SqlDbType.VarChar);
            SqlParameter p2 = new SqlParameter("@PrimaryEmailId", SqlDbType.VarChar);


            p1.Value = forgotpass.UserName;
            p2.Value = forgotpass.PrimaryEmailId;

            da.SelectCommand.Parameters.Add(p1);
            da.SelectCommand.Parameters.Add(p2);


            da.Fill(dt);
            if (dt.Rows.Count == 1)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
 public bool SaveForgotPassDetails(ForgotPasswordBE forgotpass)
 {
     ForgotPasswordDAL forgotDAL = new ForgotPasswordDAL();
     if (forgotDAL.SaveForgotPassDetails(forgotpass))
     {
         return true;
     }
     else
     {
         return false;
     }
 }
 public bool ForgotPassword(ref DataTable dt, ForgotPasswordBE forgotpass)
 {
     ForgotPasswordDAL forgotDAL = new ForgotPasswordDAL();
     if (forgotDAL.ForgotPassword(ref dt, forgotpass))
     {
         return true;
     }
     else
     {
         return false;
     }
 }
        protected void btnNext_Click(object sender, EventArgs e)
        {
            ForgotPasswordBE forgotBE = new ForgotPasswordBE();
            ForgotPasswordBAL forgotBAL = new ForgotPasswordBAL();
            DataTable dtUserDetails = new DataTable();

            forgotBE.UserName = txtUserName.Text;
            forgotBE.PrimaryEmailId = txtPREmailID.Text;
            if (forgotBAL.ForgotPassword(ref dtUserDetails, forgotBE))
            {
                Session["PasswordRequest"] = dtUserDetails;
                PnlGetDetails.Visible = false;
                PnlSubmit.Visible = true;
                lblSecretQuest.Text = dtUserDetails.Rows[0]["SecretQuest"].ToString();

            }
            else
            {
                lblMessage.Text = "Incorrect User Name or EmailId.";
                //General master = (General)this.Master;
                //master.ShowMessage("Incorrect User Name or EmailId.", false);
            }

        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            DataTable dtUserDetails = new DataTable();
            CommonBAL commonBAL = new CommonBAL();
            General master = (General)this.Master;
            ForgotPasswordBE forgotpass = new ForgotPasswordBE();
            string MessageKey = "forgotpassword";

            if (Session["PasswordRequest"] == null)
            {
                Response.Redirect("~/Login.aspx");
                return;
            }

            // 0 - save password request details of 2 panels - log the request
            // 1 - get emailmessagehtml using emailmessagekey - this is a COmmon routine COMMONDAL/BAL
            // 2 - get data to be replaced in emailmessagehtml - this is seperate rtn / function
            // 3 - now you can replace variables in emailmessagehtml by data - this should be separate rtn/func
            // 4 - now you are ready to prepare to send email using application level Session["CompanySetupData"] 
            //    which was prepared in Application_Start event itself - smtp server details
            //    and body of the message will be as in step 3
            //    and send the message ??? how to handle delivery failures???
            // 


            dtUserDetails = (DataTable)Session["PasswordRequest"];

            forgotpass.UserName = txtUserName.Text;
            forgotpass.PrimaryEmailId = txtPREmailID.Text;
            forgotpass.SecretQuest = lblSecretQuest.Text;
            forgotpass.SecretAns = dtUserDetails.Rows[0]["SecretAns"].ToString();


            SaveForgotPassDetails(forgotpass);

            if (txtSecretAns.Text == forgotpass.SecretAns)
            {
                CompanySetupBE setupBE = new CompanySetupBE();

                SmtpServerDetails(ref setupBE);


                string MailAddressFrom = setupBE.UserName;
                string MailAddressTo = forgotpass.PrimaryEmailId;
                MailMessage ms = new MailMessage(MailAddressFrom, MailAddressTo);
                ms.Subject = "Your Password";
                ms.IsBodyHtml = true;
                ms.Body = HttpUtility.HtmlDecode(GetEmailMessageHTML(MessageKey));

                 //Add a carbon copy recipient.
                MailAddress copy = new MailAddress("*****@*****.**");
                ms.CC.Add(copy);

                SmtpClient smtp = new SmtpClient(setupBE.IPAddress, Convert.ToInt32(setupBE.PortNo));
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = new NetworkCredential()
                {
                    UserName = setupBE.UserName,
                    Password = setupBE.Password
                };
                smtp.EnableSsl = true;
                try
                {
                    smtp.Send(ms);

                    ms.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                    Session["PasswordRequest"] = null;
                    Session.Clear();
                    string url = Request.Url.OriginalString.Replace(Request.Url.AbsolutePath, "/Login.aspx?Message=Password has been sent successfully to your Email Address.");
                    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", "alert('Password has been sent successfully to your Email Address.'); location.href='" + url+"'", true);
                 //   Response.Redirect("~/Login.aspx?Message=Password has been sent successfully to your Email Address.");
                    return;
                }
                catch (SmtpFailedRecipientsException ex)
                {
                    for (int i = 0; i < ex.InnerExceptions.Length; i++)
                    {
                        SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
                        if (status == SmtpStatusCode.MailboxBusy ||
                            status == SmtpStatusCode.MailboxUnavailable)
                        {
                            master.ShowMessage("Delivery failed - retrying in 5 seconds.", false);
                            System.Threading.Thread.Sleep(5000);
                            smtp.Send(ms);
                            Session["PasswordRequest"] = null;
                            Session.Clear();
                            string msg = Request.QueryString["Message"];
                            Response.Redirect("~/Login.aspx?Message=Password has been sent successfully to your Email Address.");
                        }
                        else
                        {

                        }
                    }
                }


                catch (Exception k)
                {
                    lblMessage1.Text = "Exception";
                   // master.ShowMessage("Exception", false);
                }
                Session["PasswordRequest"] = null;
                Session.Clear();
                Response.Redirect("~/Login.aspx?Message=Password has been sent successfully to your Email Address.");
            }

            else
            {
                lblMessage1.Text = "Incorrect Answer";
//                master.ShowMessage("Incorrect Answer", false);
            }
        }
 protected string ReplaceVariale(string EmailMessageHTML, ForgotPasswordBE forgotBE)
 {
     EmailMessageHTML = EmailMessageHTML.Replace("#firstname#", forgotBE.FirstName);
     EmailMessageHTML = EmailMessageHTML.Replace("#username#", forgotBE.UserName);
     EmailMessageHTML = EmailMessageHTML.Replace("#password#", forgotBE.EncPass);
     return EmailMessageHTML;
 }
        protected void LoadUserDetails(ForgotPasswordBE forgotBE)
        {
            DataTable dtUserDetails = new DataTable();
            CommonBAL commonBAL = new CommonBAL();

            dtUserDetails = (DataTable)Session["PasswordRequest"];

            forgotBE.FirstName = dtUserDetails.Rows[0]["FirstName"].ToString();
            forgotBE.EncPass = commonBAL.Decrypt(dtUserDetails.Rows[0]["EncPass"].ToString(), false);
            forgotBE.UserName = dtUserDetails.Rows[0]["UserName"].ToString();



            // string Password = commonBAL.Decrypt(forgotBE.EncPass, false);
        }
        protected string GetEmailMessageHTML(string MessageKey)
        {
            //ForgotPasswordBE user = new ForgotPasswordBE();
            //ForgotPasswordBAL userBAL = new ForgotPasswordBAL();
            CommonBAL commonBAL = new CommonBAL();
            ForgotPasswordBE forgotBE = new ForgotPasswordBE();
            // string EmailMessageKey = MessageKey;
            string EmailMessageHTML = "";

            if (commonBAL.GetEmailMessageHTML(MessageKey, ref EmailMessageHTML))
            {

                LoadUserDetails(forgotBE);
                EmailMessageHTML = ReplaceVariale(EmailMessageHTML, forgotBE);


                return EmailMessageHTML;

            }
            else
            {
                return "";
            }

        }
        protected bool SaveForgotPassDetails(ForgotPasswordBE forgotpass)
        {
            ForgotPasswordBAL forgotBAL = new ForgotPasswordBAL();

            if (forgotBAL.SaveForgotPassDetails(forgotpass))
            {
                return true;

            }
            else
            {
                return false;
            }

        }