public string CheckAlreadySubscribeEmail(string sEmail)
    {
        PreferencesBL objPreferencesBL = new PreferencesBL();
        DataSet dssub = new DataSet();

        dssub = objPreferencesBL.GetEmailPreferencesbyEmail(sEmail);
        string sPreferenceID = string.Empty;

        if (dssub.Tables[0].Rows.Count > 0)
        {
            sPreferenceID = dssub.Tables[0].Rows[0]["UserPreferID"].ToString();
        }
        else
        {
            sPreferenceID = "";
        }

        return sPreferenceID;
    }
    protected void imgBtnSubscribe_Click(object sender, ImageClickEventArgs e)
    {
        PreferencesBL objPreferencesBL = new PreferencesBL();

        DataSet dssub = new DataSet();

        dssub = objPreferencesBL.GetEmailPreferencesbyEmail(txtSub.Text);

        if (dssub.Tables[0].Rows.Count > 0)
        {
            Response.Redirect("EmailPreferences.aspx?PreferID=" + dssub.Tables[0].Rows[0]["UserPreferID"].ToString() + "");
        }
        else
        {
            FillMakes();

            FillWithin();

            txtEmailAlert.Text = txtSub.Text;

            mpesubscribe.Show();
        }
    }
    public string SubScribe(string PreferenceID, string sZip, string sName, string sEmail, string sPhoneNo)
    {
        Session[Constants.Subscribe] = 1;

        PreferencesBL objPreferencesBL = new PreferencesBL();

        DataSet dssub = new DataSet();

        PreferenceInfo ObjPreferncesInfo = new PreferenceInfo();

        PreferncesItemsInfo ObjPreferncesItemsInfo = new PreferncesItemsInfo();

        dssub = objPreferencesBL.GetEmailPreferencesbyEmail(sEmail);

        string sPreferenceID = string.Empty;

        if (dssub.Tables[0].Rows.Count == 0)
        {
            ObjPreferncesInfo.PreferenceID = PreferenceID;

            if (sZip != "Zip")
            {
                ObjPreferncesInfo.Zip = sZip;
            }
            if (sName != "Your Name")
            {
                ObjPreferncesInfo.Name = sName;
            }

            if (sEmail != "Your Email")
            {
                ObjPreferncesInfo.Email = sEmail;
            }

            if (sPhoneNo != "Your Phone")
            {
                ObjPreferncesInfo.Phone = sPhoneNo;
            }

            String strHostName = HttpContext.Current.Request.UserHostAddress.ToString();

            ObjPreferncesInfo.IPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

            if (HttpContext.Current.Request.Cookies["UserSettings"] == null)
            {
                HttpCookie myCookie = new HttpCookie("UserSettings");

                myCookie.Expires = DateTime.Now.AddDays(500);

                myCookie.Values.Add("Email", ObjPreferncesInfo.Email);
                myCookie.Values.Add("Name", ObjPreferncesInfo.Name);
                myCookie.Values.Add("Phone", ObjPreferncesInfo.Phone);

                HttpContext.Current.Response.Cookies.Add(myCookie);
            }

            DataSet dsPreferences = new DataSet();

            dsPreferences = objPreferencesBL.SaveSubscribe(ObjPreferncesInfo, 1);

            clsMailFormats format = new clsMailFormats();
            MailMessage msg = new MailMessage();

            msg.From = new MailAddress(CommonVariable.FromInfoMail);

            msg.To.Add(sEmail);

            //msg.Bcc.Add("*****@*****.**");

            msg.Subject = "Successfully modified your personalized weekly email alerts - United Car Exchange.";

            msg.IsBodyHtml = true;

            string text = string.Empty;

            string VerifyPreferences = string.Empty;

            string ModifyPreferences = string.Empty;

            string PreferencesID = string.Empty;

            //VerifyPreferences = "www.unitedcarexchange.com/VerifyPreferences.aspx?Preferce=" + dssub.Tables[0].Rows[0]["ID"].ToString();

            //ModifyPreferences = "www.unitedcarexchange.com/EmailPreferences.aspx?Preferce=" + dssub.Tables[0].Rows[0]["ID"].ToString();

            PreferencesID = dsPreferences.Tables[0].Rows[0]["UserPreferID"].ToString();

            text = format.SendEMailPreferencesEdit(dsPreferences.Tables[0].Rows[0]["Name"].ToString(), dsPreferences.Tables[0].Rows[0]["UserPreferID"].ToString(), ref text);

            msg.Body = text.ToString();

            SmtpClient smtp = new SmtpClient();

            //smtp.Host = "smtp.gmail.com";
            //smtp.Port = 587;
            //smtp.Credentials = new System.Net.NetworkCredential("*****@*****.**", "hugomirad");
            //smtp.EnableSsl = true;
            //smtp.Send(msg);

            smtp.Host = "127.0.0.1";
            smtp.Port = 25;
            smtp.Send(msg);

            return PreferencesID;
        }
        else
        {
            return "true";

        }
    }
    protected void btnSubscribe_Click(object sender, EventArgs e)
    {
        mpesubscribe.Hide();

        PreferencesBL objPreferencesBL = new PreferencesBL();

        DataSet dssub = new DataSet();

        PreferenceInfo ObjPreferncesInfo = new PreferenceInfo();

        PreferncesItemsInfo ObjPreferncesItemsInfo = new PreferncesItemsInfo();

        ObjPreferncesInfo.PreferenceID = "0";

        ObjPreferncesInfo.Zip = txtZip.Text;

        ObjPreferncesInfo.Name = txtName.Text;

        if (txtEmailAlert.Text != "Your Email")
        {
            ObjPreferncesInfo.Email = txtEmailAlert.Text;
        }

        if (txtPhoneNo.Text != "Your Phone")
        {
            ObjPreferncesInfo.Phone = txtPhoneNo.Text;
        }

        String strHostName = HttpContext.Current.Request.UserHostAddress.ToString();

        ObjPreferncesInfo.IPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

        if (Request.Cookies["UserSettings"] == null)
        {
            HttpCookie myCookie = new HttpCookie("UserSettings");

            myCookie.Expires = DateTime.Now.AddDays(500);

            myCookie.Values.Add("Email", ObjPreferncesInfo.Email);
            myCookie.Values.Add("Name", ObjPreferncesInfo.Name);
            myCookie.Values.Add("Phone", ObjPreferncesInfo.Phone);

            Response.Cookies.Add(myCookie);
        }

        ObjPreferncesItemsInfo.Makeid = ddlMake1.SelectedValue;
        ObjPreferncesItemsInfo.ModelID = ddlModel1.SelectedValue;
        ObjPreferncesItemsInfo.PriceRange = ddlRange1.SelectedValue;

        DataSet dsEmailExist = new DataSet();

        DataSet dsPreferences = new DataSet();

        dsEmailExist = objPreferencesBL.GetEmailPreferencesbyEmail(txtEmailAlert.Text);

        if (dsEmailExist.Tables[0].Rows.Count == 0)
        {
            dsPreferences = objPreferencesBL.SaveSubscribe(ObjPreferncesInfo, 1);

            for (int i = 1; i < 6; i++)
            {
                string SelectedMake = string.Empty;
                string SelectedModel = string.Empty;
                string SelectedRange = string.Empty;

                if (i == 1)
                {
                    SelectedMake = ddlMake1.SelectedValue;
                    SelectedModel = ddlModel1.SelectedValue;
                    SelectedRange = ddlRange1.SelectedValue;
                }
                else if (i == 2)
                {
                    SelectedMake = ddlMake2.SelectedValue;
                    SelectedModel = ddlModel2.SelectedValue;
                    SelectedRange = ddlRange2.SelectedValue;
                }
                else if (i == 3)
                {
                    SelectedMake = ddlMake3.SelectedValue;
                    SelectedModel = ddlModel3.SelectedValue;
                    SelectedRange = ddlRange3.SelectedValue;
                }
                else if (i == 4)
                {
                    SelectedMake = ddlMake4.SelectedValue;
                    SelectedModel = ddlModel4.SelectedValue;
                    SelectedRange = ddlRange4.SelectedValue;
                }
                else if (i == 5)
                {
                    SelectedMake = ddlMake5.SelectedValue;
                    SelectedModel = ddlModel5.SelectedValue;
                    SelectedRange = ddlRange5.SelectedValue;
                }
                if (SelectedMake != "0" && SelectedModel != "0")
                {

                    ObjPreferncesItemsInfo.Makeid = SelectedMake;

                    ObjPreferncesItemsInfo.ModelID = SelectedModel;

                    ObjPreferncesItemsInfo.PreferenceID = "0";

                    ObjPreferncesItemsInfo.PriceRange = SelectedRange;

                    ObjPreferncesItemsInfo.UserPreferID = dsPreferences.Tables[0].Rows[0]["UserPreferID"].ToString();

                    dssub = objPreferencesBL.SaveSubScribeItems(ObjPreferncesItemsInfo, true);
                }
            }
            clsMailFormats format = new clsMailFormats();

            MailMessage msg = new MailMessage();

            msg.From = new MailAddress(txtEmailAlert.Text);

            msg.To.Add("*****@*****.**");

            //msg.Bcc.Add();

            msg.Subject = "Welcome to your personalized weekly email alerts preferences.";

            msg.IsBodyHtml = true;

            string text = string.Empty;

            string VerifyPreferences = string.Empty;

            string ModifyPreferences = string.Empty;

            string PreferencesID = string.Empty;

            //VerifyPreferences = "www.unitedcarexchange.com/VerifyPreferences.aspx?Preferce=" + dssub.Tables[0].Rows[0]["ID"].ToString();

            //ModifyPreferences = "www.unitedcarexchange.com/EmailPreferences.aspx?Preferce=" + dssub.Tables[0].Rows[0]["ID"].ToString();

            PreferencesID = dsPreferences.Tables[0].Rows[0]["UserPreferID"].ToString();

            GeneralFunc obj = new GeneralFunc();

            text = format.SendEMailPreferences(GeneralFunc.ToProper(dsPreferences.Tables[0].Rows[0]["Name"].ToString()), dsPreferences.Tables[0].Rows[0]["UserPreferID"].ToString(), ref text);

            msg.Body = text.ToString();

            SmtpClient smtp = new SmtpClient();

            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("*****@*****.**", "hugomirad");
            smtp.EnableSsl = true;
            smtp.Send(msg);

            //smtp.Host = "127.0.0.1";
            //smtp.Port = 25;
            //smtp.Send(msg);

            // Progress111.Visible = false;

            Type cstype = GetType();

            ClientScriptManager cs = Page.ClientScript;

            cs.RegisterStartupScript(cstype, "KyRST", "<script type='text/javascript' language='javascript'>pageLoad();</script>");

            lblAlertMsg.Text = "Thank you for signing up for automatic email alerts..";
            //mpealert.Show();
        }
        else
        {

            Type cstype = GetType();

            ClientScriptManager cs = Page.ClientScript;

            cs.RegisterStartupScript(cstype, "KyRST", "<script type='text/javascript' language='javascript'>pageLoad();</script>");

            lblAlertMsg.Text = "Email ID Already Subscribed..";
            //mpealert.Show();

        }
    }