Пример #1
0
        ///// <summary>
        ///// 发送Email,只要条件符合,立即发送(当处于缓存中时,只要轮询到,也立即尝试发送)
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="sendEmailParameter"></param>
        ///// <param name="lineInCache"></param>
        ///// <param name="useBackupEmail">使用备用Email发送(如果lineInCache=True,则由缓存处理模块决定)</param>
        ///// <returns></returns>
        //public bool Send<T>(T sendEmailParameter, bool lineInCache, bool useBackupEmail) where T : SendEmailParameter
        //{
        //    return this.Send(sendEmailParameter, lineInCache, true, useBackupEmail);
        //}

        /// <summary>
        /// 发送Email
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sendEmailParameter"></param>
        /// <param name="lineInCache"></param>
        /// <param name="sendImmediately">是否立即发送(仅当lineInCache=true时有效)</param>
        /// <param name="useBackupEmail">使用备用Email发送</param>
        /// <returns></returns>
        public bool Send <T>(T sendEmailParameter, bool lineInCache, bool sendImmediately, EmailAccountType emailUserType) where T : SendEmailParameter
        {
            SentSubject = null;
            SentBody    = null;
            try
            {
                if (sendEmailParameter.ToEmail.IsNullOrEmpty() /*|| !sendEmailParameter.ToEmail.ValidateEmailFormat()*/)
                {
                    throw new Exception("ToEmail参数为空");
                }

                //替换
                SentSubject = ReplaceHolderValue(sendEmailParameter, Subject);
                SentBody    = ReplaceHolderValue(sendEmailParameter, Body);

                bool sendImmediatelySuccess = true;
                if (!lineInCache)
                {
                    //立即发送,发送失败的邮件会列入缓存发送
                    sendImmediatelySuccess = DoSendEmail(sendEmailParameter.ToEmail, SentSubject, SentBody, sendEmailParameter.UserName, true, emailUserType, lineInCache);
                }

                //直接放入缓存,或将发送失败的邮件内容放入缓存
                if (lineInCache || !sendImmediatelySuccess)
                {
                    //存入缓存
                    SendEmailCache cache = new SendEmailCache();
                    AutoSendEmail  email = new AutoSendEmail
                    {
                        UserName     = sendEmailParameter.UserName,
                        Address      = sendEmailParameter.ToEmail,
                        Subject      = SentSubject,
                        Body         = this.GetEmailTemplate(SentBody, this.UseEmailTemplate),     //内容 + 模板
                        SendCount    = sendImmediately ? 0 : (SiteConfig.MaxSendEmailTimes + 200), //如果不要求马上发送,则仅存于缓存中
                        LastSendTime = DateTime.Now
                    };
                    bool insertSuccess = cache.InsertEmail(email);
                    if (!insertSuccess)
                    {
                        LogUtility.EmailLogger.Error($"Email加入缓存失败!Email:{sendEmailParameter.ToEmail},Subject:{SentSubject}\r\n内容:\r\n{SentBody}");
                    }
                    return(insertSuccess);
                }
                else
                {
                    return(sendImmediatelySuccess);
                }
            }
            catch (Exception e)
            {
                LogUtility.EmailLogger.ErrorFormat(
                    $"发送Email出错:{e.Message},Email:{sendEmailParameter.ToEmail},Username:{sendEmailParameter.UserName}," +
                    $"Subject:{SentSubject}\r\n内容:\r\n{SentBody}", e);
                return(false);
            }
        }
Пример #2
0
        public void SendEventHandler(object state, bool timeout)
        {
            try
            {
                //LogUtility.EmailLogger.Info("开始Email发送轮询");
                if (!OpenAutoSendEmail)
                {
                    AutoSendEmailStarted = false;
                    SetSleep(10);
                    return; //continue;
                }
                AutoSendEmailStarted = true;


                //HttpContext cont = new HttpContext(new HttpRequest("Default.aspx", "http://www.senparc.com/Default.aspx", ""), new HttpResponse(null));

                SendEmailCache emailCache = new SendEmailCache();
                SendEmail      sendEmail  = new SendEmail(null);
                var            emails     = emailCache.Data.ToList();//这里添加ToList为了防止Data中途有变化,抛出异常:System.InvalidOperationException: 集合已修改;可能无法执行枚举操作。
                foreach (var email in emails)
                {
                    if (!OpenAutoSendEmail)
                    {
                        break;
                    }

                    AutoSendCount++;
                    if (email.SendCount > SiteConfig.MaxSendEmailTimes)
                    {
                        continue;
                    }
                    bool useBackEmail = email.SendCount >= 3;//当3次发送失败后,使用备用邮箱
                    var  accountType  = useBackEmail ? EmailAccountType._163 : EmailAccountType.Default;

                    //if (accountType == EmailUserType.Default && email.SendCount == 5 && email.Address.Contains("@163"))
                    //{
                    //    accountType = EmailUserType._163;
                    //}

                    if (email.SendCount > 2)
                    {
                        accountType = EmailAccountType._163;
                    }
                    else if (email.SendCount > 1)
                    {
                        accountType = EmailAccountType.Souidea;
                    }

                    if (sendEmail.DoSendEmail(email.Address, email.Subject, email.Body, email.UserName, false, accountType, true))
                    {
                        emailCache.SendSuccess(email);
                        SendSuccessCount++;
                        LogUtility.EmailLogger.Info($"缓存自动发送Email成功(by {accountType}):{email.Address},Subject:{email.Subject}");
                    }
                    else
                    {
                        emailCache.SendFail(email);
                        LogUtility.EmailLogger.Error($"缓存自动发送Email失败(by {accountType}):{email.Address},Subject:{email.Subject}");
                    }
                    SetSleep(7);
                }
                if (emailCache.Data.Count == 0)
                {
                    SetSleep(5);
                }
            }
            catch (Exception ex)
            {
                LogUtility.EmailLogger.ErrorFormat("AutoSendEmail发送错误!", ex);
            }
        }