Example #1
0
        private void event_worker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                EmailSend mailSend = new EmailSend();
                DetailGroupBUS dgBus = new DetailGroupBUS();
                ContentSendEventBUS cseBus = new ContentSendEventBUS();
                SendContentBUS scBus = new SendContentBUS();
                EventBUS eventBUS = new EventBUS();

                DataTable tblSendList = dgBus.getGroupListByEventList();
                if (tblSendList.Rows.Count > 0)
                {

                    List<SendMailDTO> sendList = mailSend.convetToSendMail(tblSendList);
                    int totalCnt = sendList.Count;
                    foreach (SendMailDTO sendItem in sendList)
                    {
                        int eventId = sendItem.eventId;
                        DataTable dtContentSendEvent = cseBus.GetByEventId(eventId);
                        if (dtContentSendEvent.Rows.Count > 0)
                        {
                            if (sendItem.countReceivedMail == 0
                                && sendItem.lastReceivedMail == "") // Gui lan dau
                            {
                                int id = int.Parse(dtContentSendEvent.Rows[0]["id"].ToString());
                                int contentId = int.Parse(dtContentSendEvent.Rows[0]["ContentId"].ToString());
                                int hourSend = int.Parse(dtContentSendEvent.Rows[0]["HourSend"].ToString());
                                DataTable dtSendContent = scBus.GetByID(contentId);
                                subject = dtContentSendEvent.Rows[0]["Subject"].ToString();
                                body = dtContentSendEvent.Rows[0]["Body"].ToString();

                                bool sendRs = false;
                                // Send normal.
                                bool send = sendMail(sendRs, sendItem.port, sendItem.hostName, sendItem.userSmtp, sendItem.passSmtp, sendItem.mailFrom, sendItem.senderName,
                                        subject, body, sendItem.recieveName, sendItem.mailTo, 0, id);
                                logs_info.Info("Status: " + send + ", sendRegisterId:" + sendRegisterId + ", MailTo: " + sendItem.mailTo + ", mailFrom: " + mailFrom + ", Name: " + sendItem.recieveName);

                                // Update tblDetailGroup.
                                DetailGroupDTO dgDto = new DetailGroupDTO();
                                dgDto.CustomerID = sendItem.customerId;
                                dgDto.GroupID = sendItem.groupId;
                                dgDto.CountReceivedMail = sendItem.countReceivedMail + 1;
                                dgDto.LastReceivedMail = DateTime.Now;
                                dgBus.tblDetailGroup_Update(dgDto);

                                eventBUS.tblEventCustomer_Update(eventId, sendItem.customerId, sendItem.countReceivedMail + 1);

                                logHistoryForSendEvent(id, sendItem.mailTo, mailFrom, sendItem.recieveName, send);
                            }
                            else if (sendItem.countReceivedMail < dtContentSendEvent.Rows.Count)
                            {
                                string strReceivedMail = sendItem.lastReceivedMail;
                                DateTime lastReceivedMail = DateTime.Now;
                                if (strReceivedMail != "")
                                {
                                    lastReceivedMail = DateTime.Parse(strReceivedMail);
                                }
                                int hourSend = int.Parse(dtContentSendEvent.Rows[sendItem.countReceivedMail]["HourSend"].ToString());
                                DateTime currentTime = lastReceivedMail.AddHours(hourSend);
                                TimeSpan time = currentTime - DateTime.Now;
                                /*if (DateTime.Now.Year == currentTime.Year
                                    && DateTime.Now.Month == currentTime.Month
                                    && DateTime.Now.Day == currentTime.Day
                                    && DateTime.Now.Hour == currentTime.Hour
                                    && DateTime.Now.Minute == currentTime.Minute)*/
                                if (time.Days < 0 || time.Hours < 0 || time.Minutes < 0 || time.Seconds < 0)
                                {
                                    int contentId = int.Parse(dtContentSendEvent.Rows[sendItem.countReceivedMail]["ContentId"].ToString());
                                    //DataTable dtSendContent = scBus.GetByID(contentId);
                                    //if (dtSendContent.Rows.Count > 0)
                                    //{
                                    //    subject = dtSendContent.Rows[0]["Subject"].ToString();
                                    //    body = dtSendContent.Rows[0]["Body"].ToString();
                                    //}
                                    subject = dtContentSendEvent.Select("hoursend=" + hourSend)[0]["Subject"].ToString();
                                    body = dtContentSendEvent.Select("hoursend=" + hourSend)[0]["Body"].ToString();

                                    int id = Convert.ToInt32(dtContentSendEvent.Select("hoursend=" + hourSend)[0]["id"] + "");
                                    bool sendRs = false;
                                    // Send normal.
                                    bool send = sendMail(sendRs, sendItem.port, sendItem.hostName, sendItem.userSmtp, sendItem.passSmtp, sendItem.mailFrom, sendItem.senderName,
                                            subject, body, sendItem.recieveName, sendItem.mailTo, 0, id);

                                    logHistoryForSendEvent(id, sendItem.mailTo, mailFrom, sendItem.recieveName, send);
                                    logs_info.Info("Status: " + send + ", sendRegisterId:" + sendRegisterId + ", MailTo: " + sendItem.mailTo + ", mailFrom: " + mailFrom + ", Name: " + sendItem.recieveName);
                                    // Update tblDetailGroup.
                                    DetailGroupDTO dgDto = new DetailGroupDTO();
                                    dgDto.CustomerID = sendItem.customerId;
                                    dgDto.GroupID = sendItem.groupId;
                                    dgDto.CountReceivedMail = sendItem.countReceivedMail + 1;
                                    dgDto.LastReceivedMail = DateTime.Now;
                                    dgBus.tblDetailGroup_Update(dgDto);
                                    eventBUS.tblEventCustomer_Update(eventId, sendItem.customerId, sendItem.countReceivedMail + 1);
                                }
                            }
                        }
                    }// End forEach.

                }
            }
            catch (Exception ex)
            {
                logs.Error("timer2_tick", ex);
            }
        }