private void SendCampaign(MessageItem messageItem, List <ContactData> contactsForThisEmail)
        {
            var sendingProcessData = new SendingProcessData(new ID(messageItem.MessageId));

            var dateMessageSent     = _campaignDefinition.StartDate;
            var dateMessageFinished = _campaignDefinition.EndDate;

            _specification.Job.Status = "Adjusting email stats...";

            AdjustEmailStatsWithRetry(messageItem, sendingProcessData, dateMessageSent, dateMessageFinished, 30);

            PublishEmail(messageItem, sendingProcessData);

            var contactIndex = 1;

            foreach (var contact in contactsForThisEmail)
            {
                _specification.Job.Status = $"Sending email to contact {contactIndex++} of {contactsForThisEmail.Count}";
                try
                {
                    SendEmailToContact(contact, messageItem);
                }
                catch (Exception ex)
                {
                    _specification.Job.Status = ex.ToString();
                    Log.Error("Failed", ex, this);
                }
            }

            GenerateEvents(messageItem, _campaignDefinition.Events, contactsForThisEmail);
        }
        private void PublishEmail(MessageItem messageItem, SendingProcessData sendingProcessData)
        {
            var dispatchArgs = new DispatchNewsletterArgs(messageItem, sendingProcessData)
            {
                IsTestSend        = false,
                SendingAborted    = false,
                DedicatedInstance = false,
                Queued            = false
            };

            new PublishDispatchItems().Process(dispatchArgs);
        }
Exemple #3
0
        private void PublishEmail(MessageItem messageItem)
        {
            var sendingProcessData = new SendingProcessData(new ID(messageItem.MessageId));
            var dispatchArgs       = new DispatchNewsletterArgs(messageItem, sendingProcessData)
            {
                IsTestSend        = false,
                SendingAborted    = false,
                DedicatedInstance = false,
                Queued            = false
            };

            new PublishDispatchItems(_itemUtilExt, _logger, _exmCampaignService).Process(dispatchArgs);
        }
        private void AdjustEmailStatsWithRetry(MessageItem messageItem, SendingProcessData sendingProcessData,
                                               DateTime dateMessageSent, DateTime dateMessageFinished, int retryCount)
        {
            int sleepTime = 1000;

            for (var i = 0; i < retryCount; i++)
            {
                try
                {
                    AdjustEmailStats(messageItem, sendingProcessData, dateMessageSent, dateMessageFinished);
                    return;
                }
                catch (Exception)
                {
                    Thread.Sleep(sleepTime);
                    sleepTime += 1000;
                }
            }
        }
        private void AdjustEmailStats(MessageItem messageItem, SendingProcessData sendingProcessData,
                                      DateTime dateMessageSent, DateTime dateMessageFinished)
        {
            var deployAnalytics = new DeployAnalytics();

            deployAnalytics.Process(new DispatchNewsletterArgs(messageItem, sendingProcessData));

            messageItem.Source.StartTime = dateMessageSent;
            messageItem.Source.EndTime   = dateMessageFinished;

            var innerItem = messageItem.InnerItem;

            using (new EditContext(innerItem))
            {
                innerItem.RuntimeSettings.ReadOnlyStatistics = true;
                innerItem[FieldIDs.Updated] = DateUtil.ToIsoDate(dateMessageSent);
            }

            var itemUtil     = new ItemUtilExt();
            var campaignItem = itemUtil.GetItem(messageItem.CampaignId);

            using (new EditContext(campaignItem))
            {
                campaignItem["StartDate"] = DateUtil.ToIsoDate(dateMessageSent);
                campaignItem[CampaignclassificationItem.FieldIDs.Channel] =
                    EcmFactory.GetDefaultFactory().Io.EcmSettings.CampaignClassificationChannel;
                campaignItem["EndDate"] = DateUtil.ToIsoDate(dateMessageFinished);
            }

            EcmFactory.GetDefaultFactory()
            .Gateways.EcmDataGateway.SetMessageStatisticData(messageItem.CampaignId.ToGuid(), dateMessageSent,
                                                             dateMessageFinished, FieldUpdate.Set(messageItem.SubscribersIds.Value.Count),
                                                             FieldUpdate.Set(messageItem.SubscribersIncludeCount.Value),
                                                             FieldUpdate.Set(messageItem.SubscribersExcludeCount.Value),
                                                             FieldUpdate.Set(messageItem.SubscribersGlobalOptOutCount.Value));
        }