Esempio n. 1
0
        private MailClientEntity getClient()
        {
            if (clientList.Count == 0)
            {
                initClient();
            }

            //遍历一个循环后停止3秒
            if (listPoint >= clientList.Count)
            {
                Thread.Sleep(3000);
                listPoint = 0;
            }

            MailClientEntity result = clientList[listPoint++];

            if (result.WaitEndTime != null && result.WaitEndTime.CompareTo(DateTime.Now) > 0)
            {
                result = getClient();
            }

            return(result);
        }
Esempio n. 2
0
        public void send(List <String[]> addressList, String title, String content, List <String> attachmentList)
        {
            sendWaitCnt    = addressList.Count;
            sendSuccessCnt = 0;
            sendErrorCnt   = 0;

            MailMessage mail = getMailMessage(null, null, title, content, attachmentList);

            Int32 maxResendCnt = Convert.ToInt32(ConfigService.get("mail.resendCnt"));

            for (Int32 i = 0; i < addressList.Count; i++)
            {
                String address = addressList[i][0];

                try
                {
                    mail.To.Clear();
                    mail.To.Add(new MailAddress(address));
                }
                catch (Exception ex)
                {
                    sendErrorCnt++;
                    sendWaitCnt--;
                    sendLog.AddFirst(DateUtils.getSysDate() + " 初始化发送邮件路径:" + address + "时出错:" + ex.Message);
                    continue;
                }

                if (addressList.Count > 50 && i == Math.Ceiling(new Decimal(addressList.Count) / 3))
                {
                    recheckReg(addressList, content, attachmentList);
                }

                Int32 resendCnt = 0;

                MailClientEntity sender = null;

                while (resendCnt++ < maxResendCnt)
                {
                    try
                    {
                        sender = getClient();

                        //设置form
                        String fromAddress = !"".Equals(sender.MailAddress) ? sender.MailAddress : ConfigService.get("mail.msg.from");
                        mail.From = new MailAddress(fromAddress, ConfigService.get("mail.msg.fromName"));

                        DateTime startTime = DateTime.Now;
                        sender.SmtpClient.Send(mail);
                        DateTime endTime = DateTime.Now;

                        List <SQLiteParameter> paramList = new List <SQLiteParameter>();
                        paramList.Add(new SQLiteParameter("@mail_address", address));
                        paramList.Add(new SQLiteParameter("@send_date", DateUtils.getSysDate()));
                        SQLiteUtils.execute("update t_mail_address set send_cnt = send_cnt+1, send_date = @send_date where mail_address = @mail_address", paramList);

                        sendLog.AddFirst(DateUtils.getSysDate() + " 邮件:" + address + "发送成功,耗时:" + Convert.ToString(endTime.Subtract(startTime).TotalSeconds) + "秒,主机:" + (sender != null ? sender.Host : "") + ",用户名:" + (sender != null ? sender.UserName : ""));

                        sender.ErrorCnt = 0;
                        sendSuccessCnt++;
                        sendWaitCnt--;
                        break;
                    }
                    catch (Exception ex)
                    {
                        if (maxResendCnt == resendCnt + 1)
                        {
                            sendErrorCnt++;
                            sendWaitCnt--;
                        }

                        //连续发送3次,停止使用此邮箱固定时间
                        if (sender.ErrorCnt++ >= 3)
                        {
                            sender.WaitEndTime = DateTime.Now.AddSeconds(Convert.ToDouble(ConfigService.get("mail.send.senderWaitTime")));
                        }

                        sendLog.AddFirst(DateUtils.getSysDate() + " 邮件:" + address + "发送出错,主机:" + (sender != null ? sender.Host : "") + ",用户名:" + (sender != null ? sender.UserName : "") + ",异常:" + ex.Message);
                        List <SQLiteParameter> paramList = new List <SQLiteParameter>();
                        paramList.Add(new SQLiteParameter("@id", sender.Id));
                        paramList.Add(new SQLiteParameter("@last_error_date", DateUtils.getSysDate() + ex.Message));

                        SQLiteUtils.execute("update t_mail_sender set last_error_date = @last_error_date where id = @id", paramList);
                    }
                }

                while (sendLog.Count > 100)
                {
                    sendLog.RemoveLast();
                }
            }
        }