public void ActionAdd(ModSendMailLogsModel model) { if (model.RecordID > 0) { entity = ModSendMailLogsService.Instance.GetByID(model.RecordID); // khoi tao gia tri mac dinh khi update } else { entity = new ModSendMailLogsEntity(); // khoi tao gia tri mac dinh khi insert entity.Activity = CPViewPage.UserPermissions.Approve; } ViewBag.Data = entity; ViewBag.Model = model; }
private static void SendMail() { // Khai bao bien List <ModIncidentEntity> incidents; EmailEntity emailEntity; DateTime dateNow = DateTime.Now; DateTime ngayDauTuan; TimeSpan subtract; WebMenuEntity menuIncident = null; WebMenuEntity chuKyGuiMail = null; bool allowSend = false; string sSuCo = string.Empty, isp = string.Empty, ip = string.Empty; string s2 = string.Empty, s3 = string.Empty, s4 = string.Empty, sName = string.Empty; string tempFolderConfig = ConfigurationManager.AppSettings.Get("TempFolder"); string exportTemp = ConfigurationManager.AppSettings.Get("ExportTemp"); string exportConfig = ConfigurationManager.AppSettings.Get("Export"); if (string.IsNullOrEmpty(tempFolderConfig)) { Console.WriteLine("ERROR: Ban chua cau hinh cho key TempFolder."); Console.WriteLine("===== Ket thuc chuong trinh ====="); Logger.Error($"Ban chua cau hinh cho key TempFolder."); return; } if (string.IsNullOrEmpty(exportConfig)) { Console.WriteLine("ERROR: Ban chua cau hinh cho key Export."); Console.WriteLine("===== Ket thuc chuong trinh ====="); Logger.Error($"Ban chua cau hinh cho key Export."); return; } if (string.IsNullOrEmpty(exportTemp)) { Console.WriteLine("ERROR: Ban chua cau hinh cho key ExportTemp."); Console.WriteLine("===== Ket thuc chuong trinh ====="); Logger.Error($"Ban chua cau hinh cho key ExportTemp."); return; } // Lay ds Dich vu canh bao List <ModDichVuCanhBaoEntity> dvs = ModDichVuCanhBaoService.Instance.CreateQuery() .Where(o => o.Activity == true) .ToList(); if (dvs == null) { Console.WriteLine("INFO: Khong co dich vu canh bao nao."); Console.WriteLine("===== Ket thuc chuong trinh ====="); Logger.Info($"Khong co dich vu canh bao nao."); return; } // Lay template ModEmailTemplateEntity emailTemp = ModEmailTemplateService.Instance.CreateQuery().Where(o => o.Activity == true && o.Code == "Type2").ToSingle(); if (emailTemp == null) { Console.WriteLine("FAILED: Khong co Email template."); Console.WriteLine("===== Ket thuc chuong trinh ====="); Logger.Error($"Khong co Email template."); return; } // Duyet qua cac ban ghi dv canh bao va thuc hien gui mail tuong ung foreach (ModDichVuCanhBaoEntity dv in dvs) { // Lay log gui mail string incidentIds = string.Empty; ModSendMailLogsEntity sendMailLog = ModSendMailLogsService.Instance.CreateQuery().Where(o => o.Activity == true && o.DichVuCanhBaoID == dv.ID).OrderByDesc(o => o.Publish).ToSingle(); if (sendMailLog != null) { // Neu dich vu canh bao da gui mail trong ngay hom nay roi thi khong thuc hien gui mail nua if (sendMailLog.Publish.Date == dateNow.Date) { continue; } incidentIds = sendMailLog.IncidentIDs.Trim(','); } incidents = ModIncidentService.Instance.CreateQuery() .Where(o => o.Activity == true && o.Resolve == false) .WhereIn(incidentIds != "", o => o.ID, incidentIds) .ToList(); if (incidents == null) { continue; } // Lay chu ky gui mail chuKyGuiMail = WebMenuService.Instance.CreateQuery() .Where(o => o.Activity == true && o.ID == dv.MenuID) .ToSingle(); if (chuKyGuiMail == null) { Console.WriteLine("WARN: Dich vu canh bao co ID = " + dv.ID + " khong co chu ky gui mail."); Logger.Warn($"Dich vu canh bao co ID = {dv.ID} khong co chu ky gui mail."); continue; } allowSend = false; subtract = dv.Time.Subtract(dateNow.TimeOfDay); switch (chuKyGuiMail.Code) { case "Nam": if (dateNow.Day.ToString() == "1" && dateNow.Month.ToString() == "1") { if (subtract.Hours == 0 && subtract.Minutes <= 5) { allowSend = true; } } break; case "Quy": if (dateNow.Day.ToString() == "1" && dateNow.Month.ToString() == "4" || dateNow.Day.ToString() == "1" && dateNow.Month.ToString() == "7" || dateNow.Day.ToString() == "1" && dateNow.Month.ToString() == "10" || dateNow.Day.ToString() == "1" && dateNow.Month.ToString() == "1") { if (subtract.Hours == 0 && subtract.Minutes <= 5) { allowSend = true; } } break; case "Thang": if (dateNow.Day.ToString() == "1") { if (subtract.Hours == 0 && subtract.Minutes <= 5) { allowSend = true; } } break; case "Tuan": ngayDauTuan = GetFirstDayOfWeek(dateNow); if (ngayDauTuan == dateNow) { if (subtract.Hours == 0 && subtract.Minutes <= 5) { allowSend = true; } } break; case "Ngay": if (subtract.Hours == 0 && subtract.Minutes <= 5) { allowSend = true; } break; default: allowSend = false; break; } if (allowSend == false) { continue; } try { if (incidents != null && incidents.Count > 0) { int countSuCo = incidents.Count; for (int i = 0; i < countSuCo; i++) { menuIncident = WebMenuService.Instance.CreateQuery().Where(o => o.Activity == true && o.Type == "Incident" && o.ID == incidents[i].MenuID).ToSingle(); if (menuIncident != null) { sName = menuIncident.Code == "Phishing" ? "giả mạo (Phishing)" : menuIncident.Code == "Malware" ? "nhiễm mã độc(Malware)" : "thay đổi giao diện (Deface)"; if (i == 0) { s2 = sName; } else { s3 += sName; if (i < countSuCo - 1) { s3 += ", "; } } } //sSuCo += "<br /> " + incidents[i].Path + "<br />"; } } emailEntity = new EmailEntity { To = dv.ToEmails, Cc = string.IsNullOrEmpty(dv.CcEmails) ? "*****@*****.**" : dv.CcEmails, Subject = string.Format(emailTemp.Subject, dv.Name), Body = string.Format(emailTemp.Content, dv.Name, s2 + (!string.IsNullOrEmpty(s3) ? ", " + s3 : "")), Attach = Export(dv, incidents, "Word") }; // Goi ham send mail Console.WriteLine("INFO: Thuc hien gui mail..."); string sendResult = Mail.SendMailUseSMTP(emailEntity.To, emailEntity.Cc, emailEntity.Subject, emailEntity.Body, emailEntity.Attach); Console.Write("INFO: Ket qua gui mail:\n" + sendResult); Logger.Info($"Ket qua gui mail: {sendResult}"); // Ghi lai log gui mail ModSendMailLogsEntity logEntity = new ModSendMailLogsEntity() { DichVuCanhBaoID = dv.ID, EmailTo = emailEntity.To, EmailCc = emailEntity.Cc, Subject = emailEntity.Subject, Body = emailEntity.Body, IncidentIDs = incidentIds + "," + string.Join(",", incidents.Select(o => o.ID).ToArray()), SendSuccess = string.IsNullOrEmpty(sendResult) ? true : false, Publish = DateTime.Now, Activity = true }; ModSendMailLogsService.Instance.Save(logEntity); } catch (Exception ex) { Logger.Error($"Failed: {ex.Message}\n {ex.StackTrace}"); Console.WriteLine("Exception: " + ex.Message); } } }