SendEmailJob.cs 2.4 KB

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