You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/JobSendAgentMail/SendMailJob.cs

155 lines
6.7 KiB
C#

8 months ago
using System.Data.Entity;
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 JobSendAgentMail
{
/// <summary>
/// 发送邮件任务
/// </summary>
public class SendMailJob : IJob
{
private const int MailSendTryTimes = 3;
private ILog logger = LogManager.GetLogger(typeof(SendMailJob));
public void Execute(IJobExecutionContext context)
{
string connStr = context.JobDetail.JobDataMap.GetString("ConnectString");
string MAILSENDACCOUNT = context.JobDetail.JobDataMap.GetString("MAILSENDACCOUNT");
string MAILSENDPASSWORD = context.JobDetail.JobDataMap.GetString("MAILSENDPASSWORD");
string MAILSENDSERVICE = context.JobDetail.JobDataMap.GetString("MAILSENDSERVICE");
string MAILSENDPORT = context.JobDetail.JobDataMap.GetString("MAILSENDPORT");
string MAILISSSL = context.JobDetail.JobDataMap.GetString("MAILISSSL");
MailDataContext commonDataContext = new MailDataContext(connStr);
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 message = new MimeMessage();
message.From.Add(MailboxAddress.Parse(MAILSENDACCOUNT));
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));
}
if (sendMail.CCTo.IndexOf(",") > -1)
{
var arrSendTo = sendMail.CCTo.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (var s in arrSendTo)
{
message.To.Add(MailboxAddress.Parse(s));
}
}
else if (sendMail.CCTo.IndexOf(";") > -1)
{
var arrSendTo = sendMail.CCTo.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (var s in arrSendTo)
{
message.To.Add(MailboxAddress.Parse(s));
}
}
else
{
message.To.Add(MailboxAddress.Parse(sendMail.CCTo));
}
message.Subject = sendMail.Title;
var html = new TextPart("html")
{
Text = sendMail.Body
};
MimeEntity entity = html;
//if (!string.IsNullOrEmpty(sendMail.Sender))
//{
// message.Sender = MailboxAddress.Parse(sendMail.Sender);
//}
message.Body = entity;
using (var client = new SmtpClient())
{
client.Connect(MAILSENDSERVICE, Convert.ToInt16(MAILSENDPORT), Convert.ToBoolean(MAILISSSL));
client.Authenticate(MAILSENDACCOUNT, MAILSENDPASSWORD);
client.Send(message);
client.Disconnect(true);
}
logger.Debug($"发送成功:{sendMail.GID} {sendMail.Title}");
sendMail.SendStatus = MailSend.MailSendStatusSuccess;
//commonDataContext.SaveChanges();
var OpMailLog = new OpMailLog();
OpMailLog.SUBJECT = sendMail.Title;
OpMailLog.RECEIVER = sendMail.SendTo;
OpMailLog.BSNO = sendMail.RelativeId;
OpMailLog.DESCRIPTION = sendMail.Body;
OpMailLog.SENDUSER = MAILSENDACCOUNT;
OpMailLog.BLTYPE = "舱单回执转发";
commonDataContext.OpMailLogs.Add(OpMailLog);
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);
}
}
}
}
}
}