Exemplo n.º 1
0
        public void DefaultResults()
        {
            var filters  = new DequeueFilterList();

            var result = filters.Filter(Repository).Select(r => r.ID);
            var expected = new int[] {3};

            Assert.That(AreArraysEqual(result, expected));
        }
Exemplo n.º 2
0
        public void CanFilterTwice()
        {
            var filters = new DequeueFilterList();

            var result = filters.And(new IdDequeueFilter(1)).Filter(Repository).Select(r => r.ID);
            var expected = new int[] { 1 };

            Assert.That(AreArraysEqual(result, expected));

            //do it again with the same filterset
            result = filters.Filter(Repository).Select(r => r.ID);
            expected = new int[] { 1 };

            Assert.That(AreArraysEqual(result, expected));
        }
        /// <summary>
        /// Sends the messages which match the filters and applies the overrides prior to sending
        /// </summary>
        /// <param name="filters"></param>
        /// <param name="overrides"></param>
        /// <param name="audit">whether to change the sent status and number of tries for an email</param>
        /// <returns></returns>
        public IEnumerable<DequeueResultItem> SendQueuedMessages(DequeueFilterList filters, OverrideList overrides, bool audit = true)
        {
            if(filters == null) throw new ArgumentNullException("filters");
            if(overrides == null) throw new ArgumentNullException("overrides");

            TroutLog.Log.Info(string.Format("Beginning dequeuing with{0} auditing at", audit ? "" : "out"));

            List<DequeueResultItem> results = new List<DequeueResultItem>();
            var messages = filters.Filter(Repository);

            foreach (var message in messages.ToList())
            {
                MailMessage mailMessage = GetMailMessage(message, overrides);

                foreach (var attachment in AttachmentFileSystem.GetAttachments(message))
                {
                    mailMessage.Attachments.Add(attachment);
                } 

                var result = SmtpClient.Send(mailMessage);
                results.Add(new DequeueResultItem(message, result.IsSuccess, result.Message, mailMessage, result.Tries));

                TroutLog.Log.Info(string.Format("{0} was {1}sent with message - {2} after {3} tries.", message.ID, result.IsSuccess ? "" : "not ", result.Message, result.Tries));

                if (audit)
                {
                    if (result.IsSuccess)
                    {
                        message.IsSent = true;
                        message.SendDate = DateTime.Now;
                    }
                    else
                    {
                        message.IsSent = false;
                        message.SendDate = null;
                    }

                    message.NumberTries++;
                    message.LastTryDate = DateTime.Now;
                }
            }

            TroutLog.Log.Info(string.Format("Saving of dequeue results"));
            Repository.SaveChanges();


            return results;
        }
        /// <summary>
        /// Returns emails which match the filter with the provided overrides applied.
        /// </summary>
        /// <param name="filters"></param>
        /// <param name="overrides"></param>
        /// <returns></returns>
        public IEnumerable<DequeueListItem> GetQueuedMessages(DequeueFilterList filters, OverrideList overrides)
        {
            if (filters == null) throw new ArgumentNullException("filters");
            if (overrides == null) throw new ArgumentNullException("overrides");

            TroutLog.Log.Info(string.Format("Retrieving messages"));

            List<DequeueListItem> results = new List<DequeueListItem>();

            var messages = filters.Filter(Repository);

            foreach (var message in messages.ToList())
            {
                MailMessage mailMessage = GetMailMessage(message, overrides);

                results.Add(new DequeueListItem(message, mailMessage));
            }

            TroutLog.Log.Info(string.Format("Retrieved {0} messages", results.Count));

            return results;
        }