DS7/DSWeb.Job.Common/SendMailJob.cs

129 lines
5.1 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using DSWeb.Common.DB;
using log4net;
using MailKit.Net.Smtp;
using MimeKit;
using Quartz;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
namespace DSWeb.Job.Common
{
/// <summary>
/// 发送邮件任务
/// </summary>
public class SendMailJob : IJob
{
private const int MailSendTryTimes = 3;
private CommonDataContext commonDataContext = new CommonDataContext();
private ILog logger = LogManager.GetLogger("SendMailJob");
public void Execute(IJobExecutionContext context)
{
var sendMailList = commonDataContext.MailSend.Where(sm => sm.SendStatus == MailSend.MailSendStatusCreate).OrderBy(sm => sm.CreateTime).Take(20).ToList();
if (sendMailList.Count > 0)
{
//先更改状态,防止其他线程重复发送
foreach (var sendMail in sendMailList)
{
logger.Debug($"mail ready to send:{sendMail.GID} {sendMail.Title}");
sendMail.SendStatus = MailSend.MailSendStatusSending;
sendMail.SendTime = DateTime.Now;
sendMail.TryCount++;
commonDataContext.SaveChanges();
}
//逐个发送
foreach (var sendMail in sendMailList)
{
try
{
var smtpCfg = commonDataContext.MailSendSmtp.AsNoTracking().FirstOrDefault(s => s.GID == sendMail.SmtpConfig);
if (smtpCfg == null)
{
logger.Error($"邮件未发送原因是找不到ID{sendMail.SmtpConfig}为的smtp配置");
continue;
}
var message = new MimeMessage();
if (!string.IsNullOrEmpty(sendMail.ShowName))
{
message.From.Add(new MailboxAddress(sendMail.ShowName, smtpCfg.Account));
}
else
{
message.From.Add(MailboxAddress.Parse(smtpCfg.Account));
}
if (sendMail.SendTo.IndexOf(",") > -1)
{
var arrSendTo = sendMail.SendTo.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (var s in arrSendTo)
{
message.To.Add(MailboxAddress.Parse(s));
}
}
else if (sendMail.SendTo.IndexOf(";") > -1)
{
var arrSendTo = sendMail.SendTo.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (var s in arrSendTo)
{
message.To.Add(MailboxAddress.Parse(s));
}
}
else
{
message.To.Add(MailboxAddress.Parse(sendMail.SendTo));
}
message.Subject = sendMail.Title;
var html = new TextPart("html")
{
Text = sendMail.Body
};
if (!string.IsNullOrEmpty(sendMail.Sender))
{
message.Sender = MailboxAddress.Parse(sendMail.Sender);
}
message.Body = html;
using (var client = new SmtpClient())
{
client.Connect(smtpCfg.Server, smtpCfg.Port, smtpCfg.UseSSL);
client.Authenticate(smtpCfg.Account, smtpCfg.Password);
client.Send(message);
client.Disconnect(true);
}
sendMail.SendStatus = MailSend.MailSendStatusSuccess;
commonDataContext.SaveChanges();
logger.Debug($"mail send success:{sendMail.GID} {sendMail.Title}");
}
catch (Exception ex)
{
if (sendMail.TryCount < MailSendTryTimes)
{
sendMail.SendStatus = MailSend.MailSendStatusCreate; //会再次尝试发送
}
else
{
sendMail.SendStatus = MailSend.MailSendStatusFail;
}
commonDataContext.SaveChanges();
logger.Error($"mail send fail:{sendMail.GID} {sendMail.Title}times:{sendMail.TryCount}");
logger.Error(ex.Message);
logger.Error(ex.StackTrace);
}
}
}
}
}
}