SendEmailJob.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using FCS.Common;
  2. using FCS.Interface;
  3. using FCS.Models;
  4. using Quartz;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. namespace FCS.Crawler
  9. {
  10. /// <summary>
  11. /// 发送邮件的作业
  12. /// </summary>
  13. public class SendEmailJob : IJob
  14. {
  15. private IEmail email = null;
  16. private LogHelper log = null;
  17. public SendEmailJob()
  18. {
  19. email = IOC.Resolve<IEmail>();
  20. log = new LogHelper();
  21. }
  22. public void Execute(IJobExecutionContext context)
  23. {
  24. try
  25. {
  26. log.Info(typeof(SendEmailJob), "正在执行发送邮件作业!");
  27. var mailConfig = ConfigHelper.GetConfigValue<bool>("MailTurnOn");
  28. if (mailConfig)
  29. {
  30. List<EmailModel> sendList = email.GetAllNeedSendEmail();
  31. if (sendList.Count == 0) return;
  32. var emailSubject = string.Format("{0}第{1}期等一共{2}条抓取失败日志", sendList[0].LotteryName, sendList[0].QiHao, sendList.Count);
  33. StringBuilder emailBody = new StringBuilder();
  34. emailBody.Append("抓取失败的彩种及期号列表:\r\n");
  35. emailBody.Append("<table style='border-collapse:collapse'><tr><th style='border:1px solid gray;'>彩种</th><th style='border:1px solid gray;'>期号</th><th style='border:1px solid gray;'>开奖时间</th></tr>");
  36. foreach (var mail in sendList)
  37. {
  38. emailBody.Append(string.Format("<tr><td style='border:1px solid gray;'>{0}</td><td style='border:1px solid gray;'>{1}</td><td style='border:1px solid gray;'>{2}</td></tr>", mail.LotteryName, mail.QiHao, mail.OpenTime.ToString("yyyy-MM-dd HH:mm:ss")));
  39. }
  40. emailBody.Append("</table>");
  41. email.UpdateEmailToSend(sendList);
  42. log.Info(typeof(SendEmailJob), "今日发送邮件成功!");
  43. }
  44. }
  45. catch (Exception ex)
  46. {
  47. log.Error(typeof(SendEmailJob), string.Format("发送邮件列表时发生错误,错误信息【{1}】", ex.Message));
  48. }
  49. }
  50. }
  51. }