Beispiel #1
0
        /// <summary>
        /// 单条删除邮件
        /// </summary>
        /// <param name="id">要删除的邮件ID</param>
        public void Delete(int id)
        {
            //若能获取到则删除
            EmailQueueEntry deleteEntry = emailQueueRepository.Get(id);

            if (deleteEntry != null)
            {
                EventBus <EmailQueueEntry> .Instance().OnBefore(deleteEntry, new CommonEventArgs(EventOperationType.Instance().Delete()));

                emailQueueRepository.Delete(deleteEntry);
                EventBus <EmailQueueEntry> .Instance().OnAfter(deleteEntry, new CommonEventArgs(EventOperationType.Instance().Delete()));
            }
        }
Beispiel #2
0
        /// <summary>
        /// 将EmailQueueEntry 转换为 MailMessage
        /// </summary>
        public MailMessage AsMailMessage(EmailQueueEntry emailEntry)
        {
            MailMessage currentMessage = new MailMessage();

            currentMessage.Priority   = (MailPriority)emailEntry.Priority;
            currentMessage.IsBodyHtml = emailEntry.IsBodyHtml;
            String2MailAddressCollection(currentMessage.To, emailEntry.MailTo);
            String2MailAddressCollection(currentMessage.CC, emailEntry.MailCc);
            String2MailAddressCollection(currentMessage.Bcc, emailEntry.MailBcc);
            currentMessage.From    = new MailAddress(emailEntry.MailFrom);
            currentMessage.Subject = emailEntry.Subject;
            currentMessage.Body    = emailEntry.Body;
            return(currentMessage);
        }
Beispiel #3
0
        /// <summary>
        /// 新建实体时使用
        /// </summary>
        public static EmailQueueEntry New()
        {
            EmailQueueEntry emailQueueEntry = new EmailQueueEntry()
            {
                MailFrom    = string.Empty,
                MailTo      = string.Empty,
                MailBcc     = string.Empty,
                MailCc      = string.Empty,
                Body        = string.Empty,
                Subject     = string.Empty,
                NextTryTime = DateTime.UtcNow
            };

            return(emailQueueEntry);
        }
Beispiel #4
0
        /// <summary>
        /// 在队列中对发送失败的邮件更新下次发送时间及发送次数,并且删除超过最大发送次数的邮件
        /// </summary>
        /// <param name="ids">发送失败的邮件Id集合</param>
        /// <param name="retryInterval">失败的Email过多少分钟再次重新发送</param>
        /// <param name="maxNumberOfTries">尝试发送的最大次数,超过该次数将设置为失败状态</param>
        public void SendFailed(IEnumerable <int> ids, int retryInterval, int maxNumberOfTries)
        {
            //根据当前的TryTime,若maxNumberOfTries <= TryTime+1 则:设置邮件状态IsFailed为:True
            //                   若maxNumberOfTries >  TryTime+1 则:设置邮件NumberOfTries,根据retryInterval,修改NextTryTime

            //操作步骤:
            //1 根据ids获取所有的实体
            //2 逐个进行判断,然后,编辑修改
            //由于发送失败属于异常情况,暂不考虑性能问题

            if (ids == null)
            {
                return;
            }

            foreach (int id in ids)
            {
                EmailQueueEntry emailQueueEntry = emailQueueRepository.Get(id);
                if (emailQueueEntry == null)
                {
                    continue;
                }
                else
                {
                    //尝试次数大于等于最多尝试次数,设置为发送失败
                    if ((emailQueueEntry.NumberOfTries + 1) >= maxNumberOfTries)
                    {
                        emailQueueEntry.IsFailed = true;
                    }
                    else//修改尝试时间和下次尝试时间
                    {
                        emailQueueEntry.NumberOfTries = emailQueueEntry.NumberOfTries + 1;
                        emailQueueEntry.NextTryTime   = DateTime.UtcNow.AddMinutes(retryInterval);
                    }
                    //更新到数据库
                    emailQueueRepository.Update(emailQueueEntry);
                }
            }
        }
Beispiel #5
0
 public MailMessage AsMailMessage(EmailQueueEntry emailEntry)
 {
     MailMessage mailMessage = new MailMessage();
     mailMessage.Priority = (MailPriority)emailEntry.Priority;
     mailMessage.IsBodyHtml = emailEntry.IsBodyHtml;
     this.String2MailAddressCollection(mailMessage.To, emailEntry.MailTo);
     this.String2MailAddressCollection(mailMessage.CC, emailEntry.MailCc);
     this.String2MailAddressCollection(mailMessage.Bcc, emailEntry.MailBcc);
     mailMessage.From = new MailAddress(emailEntry.MailFrom);
     mailMessage.Subject = emailEntry.Subject;
     mailMessage.Body = emailEntry.Body;
     return mailMessage;
 }