Exemplo n.º 1
0
    private void SaveActions()
    {
        int sleepTime;

        if (TotalMails < 1000)
        {
            sleepTime = TotalMails * 20;             //Emails are sent roughly 50 per second
        }
        else
        {
            sleepTime = 30000;
        }
        SqlBulkCopy bulkCopy = new SqlBulkCopy(Globals.Settings.ConnectionString);

        bulkCopy.DestinationTableName = "NewsletterAction";
        bulkCopy.ColumnMappings.Add("NewsletterActionTypeID", "NewsletterActionTypeID");
        bulkCopy.ColumnMappings.Add("MailoutID", "MailoutID");
        bulkCopy.ColumnMappings.Add("Timestamp", "Timestamp");
        bulkCopy.ColumnMappings.Add("IPAddress", "IPAddress");
        bulkCopy.ColumnMappings.Add("Email", "Email");
        bulkCopy.ColumnMappings.Add("SubscriberID", "SubscriberID");

        //Let it sleep for at least one second to give all other threads time to finish for a small mailing list
        Thread.Sleep(1000);

        while (!allThreadsFinished || newsletterActions.Rows.Count > 0)
        {
            if (newsletterActions.Rows.Count > 0)
            {
                Lock.AcquireWriterLock(Timeout.Infinite);
                bulkCopy.WriteToServer(newsletterActions);
                newsletterActions.Rows.Clear();
                Lock.ReleaseWriterLock();
            }
            Thread.Sleep(sleepTime);
        }

        if (mailServerError && Subscribers.Count == 0 && numberOfFailedSendAttempts < 2)
        {
            mailServerError    = false;
            allThreadsFinished = false;
            m_subscribers      = SubscriberInfo.GetMailingListSubscribersWithEmailsByListOfMailingListIDs(MailingListIDs, MailoutID);
            numberOfFailedSendAttempts++;
            NotSentMails = 0;
            StartThreads();
        }
        else
        {
            IsSending = false;
        }
    }
Exemplo n.º 2
0
        /// <summary>
        /// Send a Newsletter to a collection of mailing lists, or resend a previously sent Mailout to its collection of mailing lists
        /// </summary>
        /// <param name="newsletterId">The ID of the Newsletter to send</param>
        /// <param name="existingMailoutId">The ID of an existing Mailout to resend</param>
        /// <param name="designId">The ID of the design to wrap the newsletter content</param>
        /// <param name="mailingLists">Collection of mailing lists to send the mailout to</param>
        /// <param name="notSentSubscribers">Collection of subscribers who had invalid email addresses</param>
        /// <returns>True if at least one subscriber was sent the newsletter, false otherwise</returns>
        public static bool SendNewsletter(int?newsletterId, int?existingMailoutId, int designId, List <MailingList> mailingLists, out List <MailingListSubscriber> notSentSubscribers)
        {
            notSentSubscribers = new List <MailingListSubscriber>();

            Mailout mailout;

            if (existingMailoutId.HasValue)
            {
                mailout = Mailout.GetByID(existingMailoutId.Value);
            }
            else if (newsletterId.HasValue)
            {
                Newsletter newsletter = Newsletter.GetByID(newsletterId.Value);
                mailout = SaveNewMailoutForNewsletter(newsletter, mailingLists, designId);
            }
            else
            {
                return(false);
            }

            String htmlBodyWithDesign = GetNewsletterHtml(mailout, true);
            String textBody           = GetNewsletterText(mailout);

            List <int> mailingListIds = mailingLists.Select(ml => ml.MailingListID).ToList();

            Queue <SubscriberInfo> uniqueSubscribers = SubscriberInfo.GetMailingListSubscribersWithEmailsByListOfMailingListIDs(mailingListIds, existingMailoutId);

            if (uniqueSubscribers.Count > 0)
            {
                EmailSender es = new EmailSender(
                    mailout.Title,
                    uniqueSubscribers,
                    Settings.SenderEmail,
                    Settings.MailServer,
                    textBody,
                    htmlBodyWithDesign
                    , null);
                es.IPAddress      = HttpContext.Current.Request.UserHostAddress;
                es.MailoutID      = mailout.MailoutID;
                es.MailingListIDs = mailingListIds;
                es.Send();
                EmailSender.SendEmailJobs.Add(es);

                return(true);
            }
            return(false);
        }