// Thêm ngày 16/09 by Viết Hùng //Thêm ngày 26/9 public void callSendBulkMail_BySubGroup(DataRow Row, DataTable listSendContent, DataTable listCustomer, DataTable listMailConfig) { //param for send mail string EmailTo = ""; string customerName = ""; string EmailSubject = ""; string EmailBody = ""; string EmailFrom = ""; string Name = ""; string username = ""; string Password = ""; string HostName = ""; int PortNumber = 0; bool isSSL = false; //param for content int SendContentId; int EmailFromID; int SendRegisterID = 0; int AccountId = 0; SendRegisterBUS srBus = new SendRegisterBUS(); CustomerBUS customerBus = new CustomerBUS(); SendRegisterDetailBUS srdBus = new SendRegisterDetailBUS(); dgBUS = new DetailGroupBUS(); PartSendBUS psBus = new PartSendBUS(); SendContentBUS scBus = new SendContentBUS(); try { if (Row != null) { // Stop timer check SendContentId = int.Parse(Row["SendContentId"].ToString()); AccountId = int.Parse(Row["AccountId"].ToString()); // Lấy nội dung đăng ký gửi đi DataRow[] resultListSendContend = listSendContent.Select(String.Format("Id = {0}", SendContentId)); if (resultListSendContend.Length > 0) { EmailSubject = resultListSendContend[0]["Subject"].ToString(); EmailBody = resultListSendContend[0]["Body"].ToString(); } //Lấy cấu hình mail gửi EmailFromID = int.Parse(Row["MailConfigID"].ToString()); DataRow[] resultListMailConfig = listMailConfig.Select(String.Format("Id = {0}", EmailFromID)); if (resultListMailConfig.Length > 0) { EmailFrom = resultListMailConfig[0]["Email"].ToString(); username = resultListMailConfig[0]["username"].ToString(); Password = resultListMailConfig[0]["Password"].ToString(); HostName = resultListMailConfig[0]["Server"].ToString(); Name = resultListMailConfig[0]["Name"].ToString(); PortNumber = int.Parse(resultListMailConfig[0]["Port"].ToString()); isSSL = bool.Parse(resultListMailConfig[0]["isSSL"].ToString()); } // ID nhóm mail cần gửi đi string groupId = Row["SendType"].ToString(); //Lấy danh sách khách hàng của nhóm DataTable tblCustomerBySubGroup = dgBUS.GetByID(int.Parse(groupId)); if (tblCustomerBySubGroup.Rows.Count > 0) { // Kiểm tra chiến dịch này có thuộc nhóm gửi mail từng phần DataTable tblPartSend = psBus.GetByUserIdAndGroupId(AccountId, int.Parse(groupId)); SendRegisterID = int.Parse(Row["Id"].ToString()); for (int i = 0; i < tblCustomerBySubGroup.Rows.Count; i++) { Stop = true; DataTable tblCustomerByID = customerBus.GetByID(int.Parse(tblCustomerBySubGroup.Rows[i]["CustomerID"].ToString())); if (tblCustomerByID.Rows.Count > 0) { EmailTo = tblCustomerByID.Rows[0]["Email"].ToString(); customerName = (tblCustomerByID.Rows[0]["Name"].ToString() == null || tblCustomerByID.Rows[0]["Name"].ToString() == "") ? EmailTo : tblCustomerByID.Rows[0]["Name"].ToString(); } // Xử lý câu chào cho nội dung mail string body = EmailBody.Replace("[khachhang]", customerName); body = body.Replace("[email]", EmailTo); // Xử lý câu chào cho tiêu đề mail string subject = EmailSubject.Replace("[khachhang]", customerName).ToUpper(); // Thời gian bắt đầu gửi DateTime startDate = DateTime.Now; // Thời gian kết thúc gửi DateTime endDate = DateTime.Now; SendRegisterDetailDTO srdDto = new SendRegisterDetailDTO() { SendRegisterId = SendRegisterID, Email = EmailTo, StartDate = startDate, EndDate = endDate, DayEnd = endDate.Day, HoursEnd = endDate.Hour, MinuteEnd = endDate.Minute, SecondEnd = endDate.Second, MailSend = EmailFrom, Status = false, ErrorType = "0" }; int sendetailID = 0; // Lưu lại thông tin từng mail đã gửi sendetailID = srdBus.tblSendRegisterDetail_insert(srdDto); srdDto.SendRegisterDetailId = sendetailID; body += String.Format("<IMG height=1 src=\"http://fastautomaticmail.com/emailtrack.aspx?emailsentID={0}\" width=1>", sendetailID); //Update thời gian bắt đầu gửi startDate = DateTime.Now; srdDto.StartDate = DateTime.Now; bool send = false; try { //=============================================================================== // Edit by HThanhHai for part send mail //=============================================================================== // Nếu nhóm này nằm trong danh sách gửi từng phần if (tblPartSend.Rows.Count > 0) { // Kiểm tra số lượng mail khách hàng này đã nhận được int countReceivedMail = int.Parse(tblCustomerBySubGroup.Rows[i]["countReceivedMail"].ToString()); // Nội dung mail đã gửi cho nhóm này string hasContentSend = tblPartSend.Rows[0]["hasContentSend"].ToString(); string[] arrayContent = hasContentSend.Split(','); if (arrayContent.Length > 0) { // Nếu số lượng mail khách hàng này nhận ít hơn số nội dung đã gửi đi if (countReceivedMail < arrayContent.Length) { // Duyệt qua những nội dung mà khách hàng này chưa nhận // for (int k = countReceivedMail; k < arrayContent.Length; k++) { string contentId = arrayContent[k]; string contentDetail, subjectDetail; DataTable tblContent = scBus.GetByID(int.Parse(contentId)); if (tblContent.Rows.Count > 0) { subjectDetail = tblContent.Rows[0]["Subject"].ToString(); contentDetail = tblContent.Rows[0]["Body"].ToString(); contentDetail = contentDetail.Replace("[khachhang]", customerName); contentDetail = contentDetail.Replace("[email]", EmailTo); // Xử lý câu chào cho tiêu đề mail subjectDetail = subjectDetail.Replace("[khachhang]", customerName).ToUpper(); } else { subjectDetail = "No subject"; contentDetail = "Nội dung không tồn tại !"; } if (HostName == "Amazon API") { send = SendEmailWithAmazone(EmailFrom, EmailTo, subjectDetail, contentDetail, username, Password, Name); } else { send = SendMail(subjectDetail, contentDetail, HostName, PortNumber, EmailFrom, Password, Name, EmailTo, username, isSSL); } } // Update số lượng mail đã nhận sau khi đã gửi đi đầy đủ DetailGroupDTO dgDto = new DetailGroupDTO(); dgDto.CustomerID = AccountId; dgDto.GroupID = int.Parse(groupId); dgDto.CountReceivedMail = arrayContent.Length; dgBUS.tblDetailGroup_Update(dgDto); } } } else { // Không phải chiến dịch gửi mail từng phần if (HostName == "Amazon API") { send = SendEmailWithAmazone(EmailFrom, EmailTo, subject, body, username, Password, Name); } else { send = SendMail(subject, body, HostName, PortNumber, EmailFrom, Password, Name, EmailTo, username, isSSL); } } } catch (Exception ex) { const string errSubject = "Error from FAMail"; string errBody = ex.Message + DateTime.Now; ; const string errHost = "smtp.gmail.com"; const string errUser = "******"; const string errPass = "******"; SendMail(errSubject, errBody, errHost, 587, errUser, errPass, "Error from FAMail", "*****@*****.**"); SendMail(errSubject, errBody, errHost, 587, errUser, errPass, "Error from FAMail", "*****@*****.**"); } //Update thời gian bắt kết thúc srdDto.EndDate = endDate; endDate = DateTime.Now; srdBus.tblSendRegisterDetail_Update(sendetailID, startDate, endDate, send); } } } else { return; } } catch (Exception ) { throw; } finally { // cập nhật trạng thái đã gửi mail & thời gian kết thúc cho chiến dịch gửi mail này // trạng thái 1 - đã gửi, 0 - chưa gửi srBus.tblSendRegister_UpdateStatus(SendRegisterID, 1, DateTime.Now); Stop = false; } }
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); } }