/// <summary> /// 测试按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnTestSendMail_Click(object sender, EventArgs e) { MailAttachmentsList.Clear(); string[] MailPathArry = MailAttachmentsPath.Split(';'); if (MailPathArry != null) { for (int i = 0; i < MailPathArry.Length; i++) { //获取一个文件夹内全部Excel文件 List <string> mailAttachmentsLastOne = new List <string>(); DirectoryInfo folder = new DirectoryInfo(MailPathArry[i].ToString()); foreach (FileInfo file in folder.GetFiles("*.csv")) { mailAttachmentsLastOne.Add(file.FullName); Config.log.Info("当前路径:" + folder + " 中 的文件为:" + file.Name); Runtime.ShowLog("当前路径:" + folder + " 中 的文件为:" + file.Name); } //获取最新的一个文件 mailAttachmentsLastOne.Sort(); if (mailAttachmentsLastOne != null && mailAttachmentsLastOne.Count > 0) { string sql = @"SELECT COUNT(1) FROM SendEmailResult WHERE Attachments LIKE '%" + mailAttachmentsLastOne.Last() + "%';"; int sqlResult = Convert.ToInt16(SqliteHelper.ExecuteScalar(sql, null)); if (sqlResult == 0) { MailAttachmentsList.Add(mailAttachmentsLastOne.Last()); Runtime.ShowLog("当前路径:" + folder + " 中 的最新一个文件为:" + mailAttachmentsLastOne.Last() + " 未发送! 已添加到附件中"); Config.log.Info("当前路径:" + folder + " 中 的最新一个文件为:" + mailAttachmentsLastOne.Last() + " 未发送! 已添加到附件中"); } else { Runtime.ShowLog("当前路径:" + folder + " 中 的最新一个文件为:" + mailAttachmentsLastOne.Last() + " 已发送"); Config.log.Info("当前路径:" + folder + " 中 的最新一个文件为:" + mailAttachmentsLastOne.Last() + " 已发送"); } } } } ProcessState processState = new ProcessState(); List <ProcessState> processStates = processState.GetProcessState(); int processStopCount = 0; foreach (var ps in processStates) { if (ps.State == 0) { processStopCount++; } Runtime.ShowLog("当前软件运行状态:" + " 软件名称:" + ps.ProcessName + "; 运行状态: " + ps.State + "; 检查时间: " + ps.UpdateTime); } if (processStopCount >= 2) { //SendProcesssReport(processStates); } //发送附件 SendMailBy163MailService(MailAttachmentsList); }
//定时发送邮件 public static void RunSendMailLoop() { while (Runtime.m_IsRunning) { try { DateTime sendTime = Convert.ToDateTime(MailSendTime); DateTime nowTime = DateTime.Now; if (nowTime.Hour.Equals(sendTime.Hour) && nowTime.Minute.Equals(sendTime.Minute)) { MailAttachmentsList.Clear(); string[] MailPathArry = MailAttachmentsPath.Split(';'); if (MailPathArry != null) { for (int i = 0; i < MailPathArry.Length; i++) { //获取一个文件夹内全部Excel文件 List <string> mailAttachmentsLastOne = new List <string>(); DirectoryInfo folder = new DirectoryInfo(MailPathArry[i].ToString()); foreach (FileInfo file in folder.GetFiles("*.csv")) { mailAttachmentsLastOne.Add(file.FullName); Config.log.Info("当前路径:" + folder + " 中 的文件为:" + file.Name); Runtime.ShowLog("当前路径:" + folder + " 中 的文件为:" + file.Name); } //获取最新的一个文件 mailAttachmentsLastOne.Sort(); if (mailAttachmentsLastOne != null && mailAttachmentsLastOne.Count > 0) { string sql = @"SELECT COUNT(1) FROM SendEmailResult WHERE Attachments LIKE '%" + mailAttachmentsLastOne.Last() + "%';"; int sqlResult = Convert.ToInt16(SqliteHelper.ExecuteScalar(sql, null)); if (sqlResult == 0) { MailAttachmentsList.Add(mailAttachmentsLastOne.Last()); Runtime.ShowLog("当前路径:" + folder + " 中 的最新一个文件为:" + mailAttachmentsLastOne.Last() + " 未发送! 已添加到附件中"); Config.log.Info("当前路径:" + folder + " 中 的最新一个文件为:" + mailAttachmentsLastOne.Last() + " 未发送! 已添加到附件中"); } else { Runtime.ShowLog("当前路径:" + folder + " 中 的最新一个文件为:" + mailAttachmentsLastOne.Last() + " 已发送"); Config.log.Info("当前路径:" + folder + " 中 的最新一个文件为:" + mailAttachmentsLastOne.Last() + " 已发送"); } } } } Email myEmail = new Email(); myEmail.host = "smtp.163.com"; myEmail.mailSshPwd = MailSshPwd; myEmail.mailFrom = MailFrom; myEmail.mailToArray = MailToStr.Split(';'); myEmail.mailCcArray = MailToCcStr.Split(';'); myEmail.mailSubject = MailSubject + "(" + nowTime.AddDays(-1).ToString("yyyy-MM-dd") + ")"; //判断附件是否为空 if (MailAttachmentsList.Count == 0 || MailAttachmentsList == null) { myEmail.mailBody = "内容:尊敬的用户,昨日" + "(" + nowTime.AddDays(-1).ToString("yyyy-MM-dd") + ")" + "的用能报表尚未生成,请检查系统是否运行正常"; } else { myEmail.mailBody = MailBody + "(" + nowTime.AddDays(-1).ToString("yyyy-MM-dd") + ")"; } myEmail.attachmentsPath = MailAttachmentsList.ToArray(); if (myEmail.Send()) { Config.log.Info("Email 发送 给:" + MailToStr + "成功"); Config.log.Info("Email 抄送 给:" + MailToCcStr + "成功"); string emailAttachmnets = null; foreach (var item in MailAttachmentsList) { emailAttachmnets += item; } // 将发送成功的邮件 存入数据库 EmailModel eModel = new EmailModel(); eModel.Sender = myEmail.mailFrom; eModel.Receiver = MailToStr; eModel.CarbonCopy = MailToCcStr; eModel.SendTime = nowTime.ToString("yyyy-MM-dd HH:mm:ss"); eModel.SendState = 1; eModel.Subject = myEmail.mailSubject; eModel.Body = myEmail.mailBody; eModel.Attachment = emailAttachmnets; int sqlResult = SqliteHelper.SetEmailToDB(eModel); if (sqlResult == 1) { Config.log.Info("Email 用能日报表 写入数据库 完成!"); Runtime.ShowLog("Email 用能日报表 写入数据库 完成!"); } else { Config.log.Info("Email 用能日报表 写入数据库 失败!"); Runtime.ShowLog("Email 用能日报表 写入数据库 失败!"); } //Config.log.Info("Email 附件为:"+ MailAttachmentsLastOne.ToString()); Runtime.ShowLog("Email 发送 给:" + MailToStr + "成功"); //Runtime.ShowLog("Email 附件为:"+ MailAttachmentsLastOne.ToString()); } } //定时监测软件是否正常运行 if (nowTime.Minute % CheckTime == 0) { ProcessState processState = new ProcessState(); List <ProcessState> processStates = processState.GetProcessState(); int processStopCount = 0; foreach (var ps in processStates) { if (ps.State == 0) { processStopCount++; } Runtime.ShowLog("当前软件运行状态:" + " 软件名称:" + ps.ProcessName + "; 运行状态: " + ps.State + "; 检查时间: " + ps.UpdateTime); Config.log.Info("当前软件运行状态:" + " 软件名称:" + ps.ProcessName + "; 运行状态: " + ps.State + "; 检查时间: " + ps.UpdateTime); } //出现2个以上的软件没有运行则发送邮件提示 if (processStopCount >= 2) { SendProcesssReport(processStates); } } System.Threading.Thread.Sleep(60000 * 1); continue; } catch (Exception ex) { Runtime.ShowLog("!!! 发送邮件失败 !!! 详细:" + ex.Message); Config.log.Error("!!! 发送邮件失败!!! 详细:" + ex.Message); System.Threading.Thread.Sleep(60000 * 30); continue; } } }