protected void BtnSubscribe_OnClick(object sender, EventArgs e)
        {
            try
            {
                var subscription = this.MessageService.GetNewsLetterSubscriptionByEmail(txtEmail.Text);
                if(subscription != null)
                {
                    if(rbSubscribe.Checked)
                    {
                        if (!subscription.Active)
                        {
                            this.MessageService.SendNewsLetterSubscriptionActivationMessage(subscription.NewsLetterSubscriptionId, NopContext.Current.WorkingLanguage.LanguageId);
                        }
                        lblResult.Text = GetLocaleResourceString("NewsLetterSubscriptionBox.SubscriptionCreated");
                    }
                    else if(rbUnsubscribe.Checked)
                    {
                        if (subscription.Active)
                        {
                            this.MessageService.SendNewsLetterSubscriptionDeactivationMessage(subscription.NewsLetterSubscriptionId, NopContext.Current.WorkingLanguage.LanguageId);
                        }
                        lblResult.Text = GetLocaleResourceString("NewsLetterSubscriptionBox.SubscriptionDeactivated");
                    }
                }
                else if(rbSubscribe.Checked)
                {
                    subscription = new NewsLetterSubscription()
                    {
                        NewsLetterSubscriptionGuid = Guid.NewGuid(),
                        Email = txtEmail.Text,
                        Active = false,
                        CreatedOn = DateTime.UtcNow
                    };
                    this.MessageService.InsertNewsLetterSubscription(subscription);
                    this.MessageService.SendNewsLetterSubscriptionActivationMessage(subscription.NewsLetterSubscriptionId, NopContext.Current.WorkingLanguage.LanguageId);
                    lblResult.Text = GetLocaleResourceString("NewsLetterSubscriptionBox.SubscriptionCreated");
                }
                else
                {
                    lblResult.Text = GetLocaleResourceString("NewsLetterSubscriptionBox.SubscriptionDeactivated");
                }
            }
            catch(Exception ex)
            {
                lblResult.Text = ex.Message;
            }

            pnlResult.Visible = true;
            pnlSubscribe.Visible = false;
        }
        public string subscribeEmail(string pEmail)
        {
            string strResult = string.Empty;

            try
            {
                var subscription = IoC.Resolve<IMessageService>().GetNewsLetterSubscriptionByEmail(pEmail);
                if (subscription != null)
                {
                    if (!subscription.Active)
                    {
                        IoC.Resolve<IMessageService>().SendNewsLetterSubscriptionActivationMessage(subscription.NewsLetterSubscriptionId, NopContext.Current.WorkingLanguage.LanguageId);
                    }
                    //lblResult.Text = GetLocaleResourceString("NewsLetterSubscriptionBox.SubscriptionCreated");
                    //strResult = GetLocaleResourceString("NewsLetterSubscriptionBox.SubscriptionCreated");
                }
                else
                    //if (rbSubscribe.Checked)
                    //{
                    subscription = new NopSolutions.NopCommerce.BusinessLogic.Messages.NewsLetterSubscription()
                    {
                        NewsLetterSubscriptionGuid = Guid.NewGuid(),
                        Email = pEmail,
                        Active = false,
                        CreatedOn = DateTime.UtcNow
                    };
                IoC.Resolve<IMessageService>().InsertNewsLetterSubscription(subscription);
                IoC.Resolve<IMessageService>().SendNewsLetterSubscriptionActivationMessage(subscription.NewsLetterSubscriptionId, NopContext.Current.WorkingLanguage.LanguageId);
                //lblResult.Text = GetLocaleResourceString("NewsLetterSubscriptionBox.SubscriptionCreated");
                //strResult =  GetLocaleResourceString("NewsLetterSubscriptionBox.SubscriptionCreated");
                //}
                //else
                //{
                //    //lblResult.Text = GetLocaleResourceString("NewsLetterSubscriptionBox.SubscriptionDeactivated");
                //}
            }
            catch (Exception ex)
            {
                //lblResult.Text = ex.Message;
                strResult = ex.Message;

            }

            //pnlResult.Visible = true;
            //pnlSubscribe.Visible = false;

            return strResult;
        }
        /// <summary>
        /// Replaces a message template tokens
        /// </summary>
        /// <param name="subscription">Subscription</param>
        /// <param name="template">Template</param>
        /// <returns>New template</returns>
        public string ReplaceMessageTemplateTokens(NewsLetterSubscription subscription, 
            string template)
        {
            var tokens = new NameValueCollection();

            tokens.Add("Store.Name", IoC.Resolve<ISettingManager>().StoreName);
            tokens.Add("Store.URL", IoC.Resolve<ISettingManager>().StoreUrl);
            tokens.Add("Store.Email", this.DefaultEmailAccount.Email);
            tokens.Add("NewsLetterSubscription.Email", HttpUtility.HtmlEncode(subscription.Email));
            tokens.Add("NewsLetterSubscription.ActivationUrl", String.Format("{0}newslettersubscriptionactivation.aspx?t={1}&active=1", IoC.Resolve<ISettingManager>().StoreUrl, subscription.NewsLetterSubscriptionGuid));
            tokens.Add("NewsLetterSubscription.DeactivationUrl", String.Format("{0}newslettersubscriptionactivation.aspx?t={1}&active=0", IoC.Resolve<ISettingManager>().StoreUrl, subscription.NewsLetterSubscriptionGuid));

            var customer = subscription.Customer;
            if(customer != null)
            {
                template = ReplaceMessageTemplateTokens(customer, template);
            }

            foreach(string token in tokens.Keys)
            {
                template = Replace(template, String.Format(@"%{0}%", token), tokens[token]);
            }

            return template;
        }
        /// <summary>
        /// Inserts the new newsletter subscription
        /// </summary>
        /// <param name="newsLetterSubscription">NewsLetterSubscription entity</param>
        public void InsertNewsLetterSubscription(NewsLetterSubscription newsLetterSubscription)
        {
            if (newsLetterSubscription == null)
                throw new ArgumentNullException("newsLetterSubscription");

            if (!CommonHelper.IsValidEmail(newsLetterSubscription.Email))
            {
                throw new NopException("Email is not valid.");
            }

            newsLetterSubscription.Email = CommonHelper.EnsureNotNull(newsLetterSubscription.Email);
            newsLetterSubscription.Email = newsLetterSubscription.Email.Trim();
            newsLetterSubscription.Email = CommonHelper.EnsureMaximumLength(newsLetterSubscription.Email, 255);

            _context.NewsLetterSubscriptions.AddObject(newsLetterSubscription);
            _context.SaveChanges();
        }
        protected void btnImportCSV_Click(object sender, EventArgs e)
        {
            if (fuCsvFile.PostedFile != null && !String.IsNullOrEmpty(fuCsvFile.FileName))
            {
                try
                {
                    int count = 0;

                    using (StreamReader reader = new StreamReader(fuCsvFile.FileContent))
                    {
                        while (!reader.EndOfStream)
                        {
                            string line = reader.ReadLine();
                            string[] tmp = line.Split('\t');

                            if (tmp.Length == 2)
                            {
                                string email = tmp[0].Trim();
                                bool isActive = Boolean.Parse(tmp[1]);

                                NewsLetterSubscription subscription = this.MessageService.GetNewsLetterSubscriptionByEmail(email);
                                if (subscription != null)
                                {
                                    subscription.Email = email;
                                    subscription.Active = isActive;
                                    this.MessageService.UpdateNewsLetterSubscription(subscription);
                                }
                                else
                                {
                                    subscription = new NewsLetterSubscription()
                                    {
                                        NewsLetterSubscriptionGuid = Guid.NewGuid(),
                                        Email = email,
                                        Active = isActive,
                                        CreatedOn = DateTime.UtcNow
                                    };
                                    this.MessageService.InsertNewsLetterSubscription(subscription);
                                }
                                count++;
                            }
                        }
                        ShowMessage(String.Format(GetLocaleResourceString("Admin.NewsletterSubscribers.ImportEmailsButton.Success"), count));
                    }
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }