Пример #1
0
        public RetrievedData <QueuedEmail> Handle(SearchQueuedEmailsQuery theQuery)
        {
            theQuery.FromEmail = (theQuery.FromEmail ?? "").Trim();
            theQuery.ToEmail   = (theQuery.ToEmail ?? "").Trim();

            var session = BuildSession();
            var query   = session.Query <QueuedEmail>();

            if (!string.IsNullOrWhiteSpace(theQuery.FromEmail))
            {
                query = query.Where(qe => qe.Sender.Contains(theQuery.FromEmail));
            }

            if (!string.IsNullOrWhiteSpace(theQuery.ToEmail))
            {
                query = query.Where(qe => qe.Receivers.Contains(theQuery.ToEmail));
            }

            if (theQuery.CreatedFromUtc.HasValue && theQuery.CreatedFromUtc.Value > DateTime.MinValue)
            {
                query = query.Where(qe => qe.CreatedOnUtc >= theQuery.CreatedFromUtc);
            }

            if (theQuery.CreatedToUtc.HasValue && theQuery.CreatedToUtc.Value > DateTime.MinValue)
            {
                query = query.Where(qe => qe.CreatedOnUtc <= theQuery.CreatedToUtc);
            }

            if (theQuery.LoadNotSentItemsOnly)
            {
                query = query.Where(qe => qe.SentOnUtc == null || qe.SentOnUtc == DateTime.MinValue);
            }

            if (theQuery.LoadOnlyItemsToBeSent)
            {
                var nowUtc = DateTime.UtcNow;
                query = query.Where(qe => qe.DontSendBeforeDateUtc == null || qe.DontSendBeforeDateUtc == DateTime.MinValue ||
                                    qe.DontSendBeforeDateUtc <= nowUtc);
            }

            query = query.Where(qe => qe.SentTries <= theQuery.MaxSendTries);
            query = theQuery.LoadNewest ?
                    //load the newest records
                    query.OrderByDescending(qe => qe.CreatedOnUtc) :
                    //else, load by priority
                    query.OrderByDescending(qe => qe.Priority)
                    .ThenBy(qe => qe.CreatedOnUtc);

            var result = RetrieveUsingPaging(query, theQuery.PageIndex, theQuery.PageSize, true);

            return(result);
        }
Пример #2
0
        public async Task Execute(string institutionCode)
        {
            var logger = Utilities.Logger;

            logger.SetNLogLogger("QueuedMessagesSendTask");
            logger.Trace("QueuedMessagesSendTask: Executing now...");
            try
            {
                var _queuedEmailService = new QueuedEmailLogic(institutionCode);
                var query = new SearchQueuedEmailsQuery
                {
                    LoadNotSentItemsOnly  = true,
                    LoadOnlyItemsToBeSent = true,
                    MaxSendTries          = 3,
                    PageSize = 500
                };
                var processor = Utilities.QueryProcessor;
                processor.InstitutionCode = institutionCode;
                var queuedEmails = processor.Process(query);
                logger.Trace($"QueuedMessagesSendTask: {queuedEmails.DataBatch.Count} queued emails to be processed now");
                if (queuedEmails.DataBatch.Count == 0)
                {
                    return;
                }

                var emailSender = new EmailSender();
                foreach (var queuedEmail in queuedEmails.DataBatch)
                {
                    var bcc = string.IsNullOrWhiteSpace(queuedEmail.Bcc)
                                ? null
                                : queuedEmail.Bcc.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
                    var cc = string.IsNullOrWhiteSpace(queuedEmail.CC)
                                ? null
                                : queuedEmail.CC.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
                    var attachment = string.IsNullOrWhiteSpace(queuedEmail.AttachmentFilePath)
                                        ? null
                                        : new[] { new EmailAttachment {
                                                      FilePath  = queuedEmail.AttachmentFilePath,
                                                      MediaType = Utilities.GetMimeType(queuedEmail.AttachmentFilePath),
                                                  } };
                    try
                    {
                        await emailSender.SendEmail(queuedEmail, bcc, cc);

                        queuedEmail.SentOnUtc = DateTime.UtcNow;
                    }
                    catch (Exception exc)
                    {
                        logger.Error($"QueuedMessagesSendTask: Error sending e-mail. {exc.Message}.\n{exc.GetFullExceptionMessage()}");
                    }
                    finally
                    {
                        queuedEmail.SentTries++;
                        _queuedEmailService.Update(queuedEmail);
                    }
                }
            }
            finally
            {
                logger.Trace("QueuedMessagesSendTask: All done. Winding up now...");
                logger.SetNLogLogger(null);
            }
        }