SendEmailJob.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. namespace SCC.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. var 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. var 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. if (CommonHelper.SendEmail(emailSubject, emailBody.ToString()))
  42. {
  43. email.UpdateEmailToSend(sendList);
  44. log.Info(typeof(SendEmailJob), "今日发送邮件成功!");
  45. }
  46. }
  47. }
  48. catch (Exception ex)
  49. {
  50. log.Error(typeof(SendEmailJob), string.Format("发送邮件列表时发生错误,错误信息【{1}】", ex.Message));
  51. }
  52. }
  53. }
  54. }