/// <summary>
 /// Replaces the input text's personalization placeholders
 /// </summary>
 private static string ReplacePersonalizationPlaceholders(string text, SubscriberInfo subscriber, bool isHtml)
 {
     if (isHtml)
     {
         text = Regex.Replace(text, @"&lt;%\s*username\s*%&gt;",
                              subscriber.UserName, RegexOptions.IgnoreCase | RegexOptions.Compiled);
         text = Regex.Replace(text, @"&lt;%\s*email\s*%&gt;",
                              subscriber.Email, RegexOptions.IgnoreCase | RegexOptions.Compiled);
         text = Regex.Replace(text, @"&lt;%\s*firstname\s*%&gt;",
                              (subscriber.FirstName.Length > 0 ? subscriber.FirstName : "reader"),
                              RegexOptions.IgnoreCase | RegexOptions.Compiled);
         text = Regex.Replace(text, @"&lt;%\s*lastname\s*%&gt;",
                              subscriber.LastName, RegexOptions.IgnoreCase | RegexOptions.Compiled);
     }
     else
     {
         text = Regex.Replace(text, @"<%\s*username\s*%>",
                              subscriber.UserName, RegexOptions.IgnoreCase | RegexOptions.Compiled);
         text = Regex.Replace(text, @"<%\s*email\s*%>",
                              subscriber.Email, RegexOptions.IgnoreCase | RegexOptions.Compiled);
         text = Regex.Replace(text, @"<%\s*firstname\s*%>",
                              (subscriber.FirstName.Length > 0 ? subscriber.FirstName : "reader"),
                              RegexOptions.IgnoreCase | RegexOptions.Compiled);
         text = Regex.Replace(text, @"<%\s*lastname\s*%>",
                              subscriber.LastName, RegexOptions.IgnoreCase | RegexOptions.Compiled);
     }
     return(text);
 }
        /// <summary>
        /// Sends the newsletter e-mails to all subscribers
        /// </summary>
        private static void SendEmails(object data)
        {
            object[] parameters = (object[])data;
             string subject = (string)parameters[0];
             string plainTextBody = (string)parameters[1];
             string htmlBody = (string)parameters[2];
             bool isTestCall = (bool)parameters[3];
             HttpContext context = (HttpContext)parameters[4];

             if (isTestCall)
             {
            Lock.AcquireWriterLock(Timeout.Infinite);
            Newsletter.TotalMails = 1256;
            Lock.ReleaseWriterLock();

            for (int i = 1; i <= 1256; i++)
            {
               Thread.Sleep(15);
               Lock.AcquireWriterLock(Timeout.Infinite);
               Newsletter.SentMails = i;
               Newsletter.PercentageCompleted =
                  (double)Newsletter.SentMails * 100 / (double)Newsletter.TotalMails; ;
               Lock.ReleaseWriterLock();
            }
             }
             else
             {
            Lock.AcquireWriterLock(Timeout.Infinite);
            Newsletter.TotalMails = 0;
            Lock.ReleaseWriterLock();

            // check if the plain-text and the HTML bodies have personalization placeholders
            // that will need to be replaced on a per-mail basis. If not, the parsing will
            // be completely avoided later.
            bool plainTextIsPersonalized = HasPersonalizationPlaceholders(plainTextBody, false);
            bool htmlIsPersonalized = HasPersonalizationPlaceholders(htmlBody, true);

            // retrieve all subscribers to the plain-text and HTML newsletter,
            List<SubscriberInfo> subscribers = new List<SubscriberInfo>();
            ProfileCommon profile = context.Profile as ProfileCommon;

            foreach (MembershipUser user in Membership.GetAllUsers())
            {
               ProfileCommon userProfile = profile.GetProfile(user.UserName);
               if (userProfile.Preferences.Newsletter != SubscriptionType.None)
               {
                  SubscriberInfo subscriber = new SubscriberInfo(
                     user.UserName, user.Email, userProfile.FirstName, userProfile.LastName,
                     userProfile.Preferences.Newsletter);
                  subscribers.Add(subscriber);
                  Lock.AcquireWriterLock(Timeout.Infinite);
                  Newsletter.TotalMails += 1;
                  Lock.ReleaseWriterLock();
               }
            }

            // send the newsletter
            SmtpClient smtpClient = new SmtpClient();
            foreach (SubscriberInfo subscriber in subscribers)
            {
               MailMessage mail = new MailMessage();
               mail.From = new MailAddress(Settings.FromEmail, Settings.FromDisplayName);
               mail.To.Add(subscriber.Email);
               mail.Subject = subject;
               if (subscriber.SubscriptionType == SubscriptionType.PlainText)
               {
                  string body = plainTextBody;
                  if (plainTextIsPersonalized)
                     body = ReplacePersonalizationPlaceholders(body, subscriber, false);
                  mail.Body = body;
                  mail.IsBodyHtml = false;
               }
               else
               {
                  string body = htmlBody;
                  if (htmlIsPersonalized)
                     body = ReplacePersonalizationPlaceholders(body, subscriber, true);
                  mail.Body = body;
                  mail.IsBodyHtml = true;
               }
               try
               {
                  smtpClient.Send(mail);
               }
               catch { }

               Lock.AcquireWriterLock(Timeout.Infinite);
               Newsletter.SentMails += 1;
               Newsletter.PercentageCompleted =
                  (double)Newsletter.SentMails * 100 / (double)Newsletter.TotalMails;
               Lock.ReleaseWriterLock();
            }
             }

             Lock.AcquireWriterLock(Timeout.Infinite);
             Newsletter.IsSending = false;
             Lock.ReleaseWriterLock();
        }
 /// <summary>
 /// Replaces the input text's personalization placeholders
 /// </summary>
 private static string ReplacePersonalizationPlaceholders(string text, SubscriberInfo subscriber, bool isHtml)
 {
     if (isHtml)
      {
     text = Regex.Replace(text, @"&lt;%\s*username\s*%&gt;",
      subscriber.UserName, RegexOptions.IgnoreCase | RegexOptions.Compiled);
     text = Regex.Replace(text, @"&lt;%\s*email\s*%&gt;",
        subscriber.Email, RegexOptions.IgnoreCase | RegexOptions.Compiled);
     text = Regex.Replace(text, @"&lt;%\s*firstname\s*%&gt;",
        (subscriber.FirstName.Length > 0 ? subscriber.FirstName : "reader"),
        RegexOptions.IgnoreCase | RegexOptions.Compiled);
     text = Regex.Replace(text, @"&lt;%\s*lastname\s*%&gt;",
        subscriber.LastName, RegexOptions.IgnoreCase | RegexOptions.Compiled);
      }
      else
      {
     text = Regex.Replace(text, @"<%\s*username\s*%>",
        subscriber.UserName, RegexOptions.IgnoreCase | RegexOptions.Compiled);
     text = Regex.Replace(text, @"<%\s*email\s*%>",
        subscriber.Email, RegexOptions.IgnoreCase | RegexOptions.Compiled);
     text = Regex.Replace(text, @"<%\s*firstname\s*%>",
        (subscriber.FirstName.Length > 0 ? subscriber.FirstName : "reader"),
        RegexOptions.IgnoreCase | RegexOptions.Compiled);
     text = Regex.Replace(text, @"<%\s*lastname\s*%>",
        subscriber.LastName, RegexOptions.IgnoreCase | RegexOptions.Compiled);
      }
      return text;
 }
        /// <summary>
        /// Sends the newsletter e-mails to all subscribers
        /// </summary>
        private static void SendEmails(object data)
        {
            object[]    parameters    = (object[])data;
            string      subject       = (string)parameters[0];
            string      plainTextBody = (string)parameters[1];
            string      htmlBody      = (string)parameters[2];
            bool        isTestCall    = (bool)parameters[3];
            HttpContext context       = (HttpContext)parameters[4];

            if (isTestCall)
            {
                Lock.AcquireWriterLock(Timeout.Infinite);
                Newsletter.TotalMails = 1256;
                Lock.ReleaseWriterLock();

                for (int i = 1; i <= 1256; i++)
                {
                    Thread.Sleep(15);
                    Lock.AcquireWriterLock(Timeout.Infinite);
                    Newsletter.SentMails           = i;
                    Newsletter.PercentageCompleted =
                        (double)Newsletter.SentMails * 100 / (double)Newsletter.TotalMails;;
                    Lock.ReleaseWriterLock();
                }
            }
            else
            {
                Lock.AcquireWriterLock(Timeout.Infinite);
                Newsletter.TotalMails = 0;
                Lock.ReleaseWriterLock();

                // check if the plain-text and the HTML bodies have personalization placeholders
                // that will need to be replaced on a per-mail basis. If not, the parsing will
                // be completely avoided later.
                bool plainTextIsPersonalized = HasPersonalizationPlaceholders(plainTextBody, false);
                bool htmlIsPersonalized      = HasPersonalizationPlaceholders(htmlBody, true);

                // retrieve all subscribers to the plain-text and HTML newsletter,
                List <SubscriberInfo> subscribers = new List <SubscriberInfo>();
                ProfileCommon         profile     = context.Profile as ProfileCommon;

                foreach (MembershipUser user in Membership.GetAllUsers())
                {
                    ProfileCommon userProfile = profile.GetProfile(user.UserName);
                    if (userProfile.Preferences.Newsletter != SubscriptionType.None)
                    {
                        SubscriberInfo subscriber = new SubscriberInfo(
                            user.UserName, user.Email, userProfile.FirstName, userProfile.LastName,
                            userProfile.Preferences.Newsletter);
                        subscribers.Add(subscriber);
                        Lock.AcquireWriterLock(Timeout.Infinite);
                        Newsletter.TotalMails += 1;
                        Lock.ReleaseWriterLock();
                    }
                }

                // send the newsletter
                SmtpClient smtpClient = new SmtpClient();
                foreach (SubscriberInfo subscriber in subscribers)
                {
                    MailMessage mail = new MailMessage();
                    mail.From = new MailAddress(Settings.FromEmail, Settings.FromDisplayName);
                    mail.To.Add(subscriber.Email);
                    mail.Subject = subject;
                    if (subscriber.SubscriptionType == SubscriptionType.PlainText)
                    {
                        string body = plainTextBody;
                        if (plainTextIsPersonalized)
                        {
                            body = ReplacePersonalizationPlaceholders(body, subscriber, false);
                        }
                        mail.Body       = body;
                        mail.IsBodyHtml = false;
                    }
                    else
                    {
                        string body = htmlBody;
                        if (htmlIsPersonalized)
                        {
                            body = ReplacePersonalizationPlaceholders(body, subscriber, true);
                        }
                        mail.Body       = body;
                        mail.IsBodyHtml = true;
                    }
                    try
                    {
                        smtpClient.Send(mail);
                    }
                    catch { }

                    Lock.AcquireWriterLock(Timeout.Infinite);
                    Newsletter.SentMails          += 1;
                    Newsletter.PercentageCompleted =
                        (double)Newsletter.SentMails * 100 / (double)Newsletter.TotalMails;
                    Lock.ReleaseWriterLock();
                }
            }

            Lock.AcquireWriterLock(Timeout.Infinite);
            Newsletter.IsSending = false;
            Lock.ReleaseWriterLock();
        }