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

205 lines
8.3 KiB
C#

9 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
{
8 months ago
/*
*
*
//待发邮件表 自动发出其中 sendstatus=create的
CREATE TABLE [dbo].[Mail_Send](
[GID] [nvarchar](128) NOT NULL,
[SendTo] [nvarchar](max) NULL,
[CCTo] [nvarchar](max) NULL,
[Title] [nvarchar](max) NULL,
[Body] [nvarchar](max) NULL,
[SendStatus] [nvarchar](40) NULL,
[SendTime] [datetime] NULL,
[TryCount] [int] NOT NULL,
[CreateTime] [datetime] NOT NULL,
[RelativeId] [nvarchar](max) NULL,
[SmtpConfig] [varchar](36) NULL,
[Sender] [varchar](64) NULL,
[ShowName] [nvarchar](50) NULL,
CONSTRAINT [PK_dbo.Mail_Send] PRIMARY KEY CLUSTERED
(
[GID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
//发送日志记录
CREATE TABLE [dbo].[op_mail_log](
[GID] [varchar](60) NOT NULL,
[BSNO] [varchar](60) NULL,
[BLTYPE] [varchar](20) NULL,
[RECEIVER] [varchar](100) NULL,
[SUBJECT] [varchar](300) NULL,
[DESCRIPTION] [varchar](max) NULL,
[ATTACHMENT] [varchar](600) NULL,
[SENDTIME] [datetime] NULL,
[SENDER] [varchar](10) NULL,
[SENDUSER] [varchar](60) NULL,
CCTo varchar(500) null,
CONSTRAINT [PK_op_mail_log] PRIMARY KEY CLUSTERED
(
[GID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
*/
9 months ago
/// <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));
}
8 months ago
if (!string.IsNullOrWhiteSpace(sendMail.CCTo))
9 months ago
{
8 months ago
if (sendMail.CCTo.IndexOf(",") > -1)
9 months ago
{
8 months ago
var arrSendTo = sendMail.CCTo.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (var s in arrSendTo)
{
message.To.Add(MailboxAddress.Parse(s));
}
9 months ago
}
8 months ago
else if (sendMail.CCTo.IndexOf(";") > -1)
9 months ago
{
8 months ago
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));
9 months ago
}
}
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);
}
}
}
}
}
}