public bool UpdateEmailCredentials(CompanySetupBE company)
        {
            CommonDAL commonDAL = new CommonDAL();
            SqlConnection con = commonDAL.Connection();

            SqlCommand cmd = new SqlCommand("Sp_UM_CompanySettingCredentialsUpdate", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@UserId", SqlDbType.VarChar).Value = company.UserName;
            cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = company.Password;
            cmd.Parameters.Add("@ServerName", SqlDbType.VarChar).Value = company.IPAddress;
            cmd.Parameters.Add("@PortNo", SqlDbType.VarChar).Value = company.PortNo;

            //con.Open();

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

            if (count == 2)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
 public bool UpdateEmailCredentials(CompanySetupBE company)
 {
     CompanySetupDAL setupDAL = new CompanySetupDAL();
     if (setupDAL.UpdateEmailCredentials(company))
     {
         return true;
     }
     else
     {
         return false;
     }
 }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            CompanySetupBE company = new CompanySetupBE();
            CompanySetupBAL companyBAL = new CompanySetupBAL();
            LoggedIn master = (LoggedIn)this.Master;

            company.UserName = txtUserId.Text;
            company.Password = txtPassword.Text;
            company.IPAddress = txtSMTPServer.Text;
            company.PortNo = txtPortNumber.Text;

            if (companyBAL.InsertEmailCredentials(company))
            {
                master.ShowMessage("Records Added Successfully", true);
            }
        }
        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 void SmtpServerDetails(ref CompanySetupBE setupBE)
        {
            CompanySetupBAL setupBAL = new CompanySetupBAL();
            DataTable dtEmailCredentials = new DataTable();

            if (setupBAL.GetEmailCredentials(ref dtEmailCredentials))
            {
                Session["ComapnySetup"] = dtEmailCredentials;
                setupBE.UserName = dtEmailCredentials.Rows[0]["FieldName1"].ToString();
                setupBE.Password = dtEmailCredentials.Rows[0]["FieldName2"].ToString();
                setupBE.IPAddress = dtEmailCredentials.Rows[0]["FieldName3"].ToString();
                setupBE.PortNo = dtEmailCredentials.Rows[0]["FieldName4"].ToString();

            }
        }