Beispiel #1
0
        private void timerPro(object sender, System.Timers.ElapsedEventArgs e)
        {//DateTime.Parse(DateTime.Now.ToLongTimeString())>= DateTime.Parse(dtp_timeLimit.Value.ToLongTimeString())
            bool onTime = false;

            if ((precision) == DatePart.HOUR && DateTime.Now.Hour == dtp_timeLimit.Value.Hour)
            {
                onTime = true;
            }
            else
            if ((precision) == DatePart.MINUTE && DateTime.Now.Hour == dtp_timeLimit.Value.Hour && DateTime.Now.Minute == dtp_timeLimit.Value.Minute)
            {
                onTime = true;
            }
            else
            if ((precision) == DatePart.SECEND && DateTime.Now.Hour == dtp_timeLimit.Value.Hour && DateTime.Now.Minute == dtp_timeLimit.Value.Minute && DateTime.Now.Second == dtp_timeLimit.Value.Second)
            {
                onTime = true;
            }
            //return;
            if (bol_running && !bol_alarming && ((cb_isLimitSend.Checked && onTime) || !cb_isLimitSend.Checked))
            {
                bol_alarming = true;
                string        loc_file     = "";
                int           Dcount       = 0;
                StringBuilder sb           = new StringBuilder();
                Random        rand         = new Random(DateTime.Now.Second);
                string        loc_fileName = string.Format(fileName, DateTime.Now.ToString("yyyy-MM-dd") + DateTime.Now.Second + rand.Next());
                try
                {
                    using (ksoaContext db = new ksoaContext())
                    {
                        //var query = (from q in db.alarm_MailAddr where q.IsDeleted==false&& DbFunctions.DiffMinutes(q.lastSendDate, DateTime.Now ) >= q.sendTimeSpan select q).ToList();
                        var                    query    = addr_List.AsEnumerable().Where(q => q.IsDeleted == false && DateTime.Now.AddMinutes(-q.sendTimeSpan) > q.lastSendDate).ToList();
                        sendMailAddr           loc_addr = new sendMailAddr();
                        tag_terminal           loc_point;
                        List <stock_alarm_GSA> list_goods;

                        Subject = string.Format(Subject, DateTime.Now.ToString("yyyy-MM-dd"));
                        if (query.Count > 0)
                        {
                            var loc_item = query.First(q =>
                                                       !string.IsNullOrEmpty(q.SMTPPuser) &&
                                                       !string.IsNullOrEmpty(q.mailBody) &&
                                                       !string.IsNullOrEmpty(q.senderAddr) &&
                                                       !string.IsNullOrEmpty(q.senderName) &&
                                                       !string.IsNullOrEmpty(q.SMTPHost) &&
                                                       !string.IsNullOrEmpty(q.SMTPPass) &&
                                                       !string.IsNullOrEmpty(q.subject)
                                                       );
                            checkTimes(DateTime.Now, loc_item.fileBasePath, loc_fileName, out Dcount, out list_goods);
                            if (Dcount > 0)
                            {
                                loc_file = file = loc_item.fileBasePath + loc_fileName;

                                loc_addr.mailBody      = string.Format(loc_item.mailBody, Dcount.ToString());
                                loc_addr.SMTPHost      = loc_item.SMTPHost;
                                loc_addr.SMTPPass      = loc_item.SMTPPass;
                                loc_addr.SMTPPuser     = loc_item.SMTPPuser;
                                loc_addr.subject       = string.Format(loc_item.subject, DateTime.Now.ToString("yyyy-MM-dd"));
                                loc_addr.from.addr     = loc_item.senderAddr;
                                loc_addr.from.showName = loc_item.senderName;
                                loc_addr.list_Toer     = new List <tag_terminal>();
                                loc_addr.list_files    = new List <string>();
                                loc_addr.list_files.Add(loc_file);
                                foreach (var item in query)
                                {
                                    loc_point          = new tag_terminal();
                                    loc_point.addr     = item.mailAddress;
                                    loc_point.showName = item.toer;
                                    loc_addr.list_Toer.Add(loc_point);
                                }
                                // return;
                                if (sendMail(loc_addr))
                                {
                                    //times++;

                                    try {
                                        alarm_mailaddr loc_ma;
                                        //int aapo = 0;
                                        //Console.Write(1 / aapo);
                                        foreach (var item in query)
                                        {
                                            loc_ma = db.alarm_MailAddr.Find(item.ID);
                                            loc_ma.lastSendDate = DateTime.Now;
                                        }
                                        foreach (var item in list_goods)
                                        {
                                            db.stock_alarm_GSA.Find(item.ID).last_alarmDate = (DateTime.Now);
                                        }
                                        db.SaveChanges();
                                        dataRefresh();
                                        sb = new StringBuilder();
                                        sb.Append(DateTime.Now).Append("|商品库存预警邮件发送成功!共(").Append(Dcount).Append(")个商品.");
                                        log_operate.writeLog(string.Format(logFile, DateTime.Now.ToString("yyyy-MM-dd")), sb.ToString());
                                        stock_alarm_GSA_his hisModel;
                                        foreach (var item in list_goods)
                                        {
                                            hisModel                 = new stock_alarm_GSA_his();
                                            hisModel.IDS             = item.ID;
                                            hisModel.hw              = item.hw;
                                            hisModel.lastCalcuDate   = item.lastCalcuDate;
                                            hisModel.last_alarmDate  = item.last_alarmDate;
                                            hisModel.notice          = item.notice;
                                            hisModel.ordering_cycle  = item.ordering_cycle;
                                            hisModel.restock_count   = item.restock_count;
                                            hisModel.safty_days      = item.safty_days;
                                            hisModel.saledIn1Month   = item.saledIn1Month;
                                            hisModel.saledIn7Days    = item.saledIn7Days;
                                            hisModel.spbh            = item.spbh;
                                            hisModel.spid            = item.spid;
                                            hisModel.spmch           = item.spmch;
                                            hisModel.stock_5DLatter  = item.stock_5DLatter;
                                            hisModel.stock_transit   = item.stock_transit;
                                            hisModel.threshold_value = item.threshold_value;
                                            hisModel.delivery_cycle  = item.delivery_cycle;
                                            hisModel.amount_static   = item.amount_static;
                                            hisModel.alarm_type      = item.alarm_type;
                                            hisModel.alarm_state     = item.alarm_state;
                                            hisModel.alarmSpan       = item.alarmSpan;
                                            hisModel.add_date        = item.add_date;
                                            db.stock_alarm_GSA_his.Add(hisModel);
                                        }
                                        db.SaveChanges();
                                    }
                                    catch (Exception e1)
                                    {
                                        sb = new StringBuilder();
                                        sb.Append(DateTime.Now.ToString()).Append(":").
                                        Append(e1.Message).Append("\r\n\t\t").
                                        Append(e1.InnerException == null?"": (e1.InnerException.InnerException == null? e1.InnerException.Message: e1.InnerException.InnerException.Message));
                                        loc_addr.mailBody = sb.ToString();
                                        loc_addr.list_Toer.Clear();
                                        loc_addr.list_Toer.Add(loc_addr.from);
                                        sendMail(loc_addr);
                                        this.Invoke(new dosth(() => { btn_startWork.PerformClick(); }));
                                        log_operate.writeLog(string.Format(logFile, DateTime.Now.ToString("yyyy-MM-dd")), sb.ToString());
                                    }
                                }
                                else
                                {
                                    dataRefresh();
                                    sb = new StringBuilder();
                                    sb.Append(DateTime.Now).Append("|商品库存预警邮件发送失败!共(").Append(Dcount).Append(")个商品.");
                                    log_operate.writeLog(string.Format(logFile, DateTime.Now.ToString("yyyy-MM-dd")), sb.ToString());
                                }
                            }
                        }
                    }
                }
                catch (Exception e1)
                {
                    sb = new StringBuilder();
                    try {
                        sb.Append(DateTime.Now.ToString()).Append(":").Append(e1.Message).Append("\r\n\t\t").Append(e1.InnerException == null ? "" : e1.InnerException.Message);
                        log_operate.writeLog(string.Format(logFile, DateTime.Now.ToString("yyyy-MM-dd")), sb.ToString());
                    }
                    catch (Exception) { }
                    bol_alarming = false;
                    return;
                }
                bol_alarming = false;
            }
        }
Beispiel #2
0
        public bool sendmail2Many(tag_terminal sfrom, List <tag_terminal> tolist, List <tag_terminal> bcc, string sSubject, string sBody, string [] sfile, string sSMTPHost, string sSMTPuser, string sSMTPpass)
        {
            ////设置from和to地址
            MailAddress from = new MailAddress(sfrom.addr, sfrom.showName);

            ////创建一个MailMessage对象
            MailMessage oMail = new MailMessage();

            oMail.From = from;

            foreach (var item in bcc)
            {
                oMail.Bcc.Add(new MailAddress(item.addr, item.showName));
            }
            foreach (var item in tolist)
            {
                oMail.To.Add(new MailAddress(item.addr, item.showName));
            }
            //// 添加附件
            if (sfile.Length > 0)
            {
                foreach (var item in sfile)
                {
                    if (item.Trim().Length > 0)
                    {
                        oMail.Attachments.Add(new Attachment(item));
                    }
                }
            }



            ////邮件标题
            oMail.Subject = sSubject;


            ////邮件内容
            oMail.Body = sBody;

            ////邮件格式
            oMail.IsBodyHtml = false;

            ////邮件采用的编码
            oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");

            ////设置邮件的优先级为高
            oMail.Priority = MailPriority.High;

            ////发送邮件
            SmtpClient client = new SmtpClient();

            ////client.UseDefaultCredentials = false;
            client.Host           = sSMTPHost;
            client.Credentials    = new NetworkCredential(sSMTPuser, sSMTPpass);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            try
            {
                client.Send(oMail);
                return(true);
            }
            catch (Exception err)
            {
                //Response.Write(err.Message.ToString());
                oMail.Dispose();
                StringBuilder sb = new StringBuilder();
                sb.Append("CRROR INFO|").Append(err.Message);
                sb.Append(DateTime.Now).Append("|邮件发送失败!")
                .Append("       \r\n|sendaddr:").Append(sfrom.addr)
                .Append("       \r\n|sendname:").Append(sfrom.showName)
                .Append("       \r\n|toAddr/toer:");
                foreach (var item in tolist)
                {
                    sb.Append("     \r\n").Append(item.addr).Append("/").Append(item.showName);
                }
                sb
                .Append("       \r\n|sSubject:").Append(sSubject)
                .Append("       \r\n|sBody:").Append(sBody)
                .Append("       \r\n|sSMTPHost:").Append(sSMTPHost)
                .Append("       \r\n|sSMTPuser:"******"       \r\n|sSMTPpass:"******"       \r\n|Files:")
                ;
                foreach (var item in sfile)
                {
                    sb.Append("\r\n").Append(item);
                }
                log_operate.writeLog(string.Format(logFile, DateTime.Now.ToString("yyyy-MM-dd")), sb.ToString());
                return(false);
            }
            finally
            {
                ////释放资源
                oMail.Dispose();
            }
        }
Beispiel #3
0
        private void timerPro(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (bol_running && !bol_alarming)
            {
                bol_alarming = true;
                string loc_file     = "";
                int    Dcount       = 0;
                Random rand         = new Random(DateTime.Now.Second);
                string loc_fileName = string.Format(fileName, DateTime.Now.ToString("yyyy-MM-dd") + DateTime.Now.Second + rand.Next());

                using (ksoaContext db = new ksoaContext())
                {
                    //var query = (from q in db.alarm_MailAddr where q.IsDeleted==false&& DbFunctions.DiffMinutes(q.lastSendDate, DateTime.Now ) >= q.sendTimeSpan select q).ToList();
                    var          query    = addr_List.AsEnumerable().Where(q => q.IsDeleted == false && DateTime.Now.AddMinutes(-q.sendTimeSpan) > q.lastSendDate).ToList();
                    sendMailAddr loc_addr = new sendMailAddr();
                    tag_terminal loc_point;
                    if (query.Count > 0)
                    {
                        List <stock_alarm_GSA> list_goods;

                        Subject = string.Format(Subject, DateTime.Now.ToString("yyyy-MM-dd"));
                        Body    = string.Format(Body, Dcount.ToString());

                        var loc_item = query.First(q => !string.IsNullOrEmpty(q.SMTPPuser) &&
                                                   !string.IsNullOrEmpty(q.mailBody) &&
                                                   !string.IsNullOrEmpty(q.senderAddr) &&
                                                   !string.IsNullOrEmpty(q.senderName) &&
                                                   !string.IsNullOrEmpty(q.SMTPHost) &&
                                                   !string.IsNullOrEmpty(q.SMTPPass) &&
                                                   !string.IsNullOrEmpty(q.subject)
                                                   );
                        checkTimes(DateTime.Now, loc_item.fileBasePath, loc_fileName, out Dcount, out list_goods);
                        loc_file = file = loc_item.fileBasePath + loc_fileName;

                        loc_addr.mailBody      = string.Format(loc_item.mailBody, Dcount.ToString());
                        loc_addr.SMTPHost      = loc_item.SMTPHost;
                        loc_addr.SMTPPass      = loc_item.SMTPPass;
                        loc_addr.SMTPPuser     = loc_item.SMTPPuser;
                        loc_addr.subject       = string.Format(loc_item.subject, DateTime.Now.ToString("yyyy-MM-dd"));
                        loc_addr.from.addr     = loc_item.senderAddr;
                        loc_addr.from.showName = loc_item.senderName;
                        loc_addr.list_Toer     = new List <tag_terminal>();
                        loc_addr.list_files    = new List <string>();
                        loc_addr.list_files.Add(loc_file);
                        foreach (var item in query)
                        {
                            loc_point          = new tag_terminal();
                            loc_point.addr     = item.mailAddress;
                            loc_point.showName = item.toer;
                            loc_addr.list_Toer.Add(loc_point);
                        }
                        if (sendMail(loc_addr))
                        {
                            alarm_mailaddr loc_ma;

                            foreach (var item in query)
                            {
                                loc_ma = db.alarm_MailAddr.Find(item.ID);
                                loc_ma.lastSendDate = DateTime.Now;
                            }
                            foreach (var item in list_goods)
                            {
                                db.stock_alarm_GSA.Find(item.ID).last_alarmDate = (DateTime.Now);
                            }
                            db.SaveChanges();
                            dataRefresh();
                            StringBuilder sb = new StringBuilder();
                            sb.Append(DateTime.Now).Append("|商品库存预警邮件发送成功!共(").Append(Dcount).Append(")个商品.");
                            log_operate.writeLog(string.Format(logFile, DateTime.Now.ToString("yyyy-MM-dd")), sb.ToString());
                        }
                        else
                        {
                            dataRefresh();
                            StringBuilder sb = new StringBuilder();
                            sb.Append(DateTime.Now).Append("|商品库存预警邮件发送失败!共(").Append(Dcount).Append(")个商品.");
                            log_operate.writeLog(string.Format(logFile, DateTime.Now.ToString("yyyy-MM-dd")), sb.ToString());
                        }
                    }
                }
                bol_alarming = false;
            }
        }