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.
116 lines
4.5 KiB
C#
116 lines
4.5 KiB
C#
using BookingJieFeng.DB;
|
|
using log4net;
|
|
using MimeKit;
|
|
using MimeKit.Utils;
|
|
using MailKit.Net.Smtp;
|
|
using Quartz;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Configuration;
|
|
|
|
namespace BookingJieFeng.Job
|
|
{
|
|
public class SendMailJob : IJob
|
|
{
|
|
private static readonly string SendMailServer = ConfigurationManager.AppSettings["sendMailServer"];
|
|
private static readonly int SendMailPort = Convert.ToInt32(ConfigurationManager.AppSettings["sendMailPort"]);
|
|
private static readonly bool SendMailUseSSL = Convert.ToBoolean(ConfigurationManager.AppSettings["sendMailUseSSL"]);
|
|
private static readonly string SendMailAccount = ConfigurationManager.AppSettings["sendMailAccount"];
|
|
private static readonly string SendMailPassword = ConfigurationManager.AppSettings["sendMailPassword"];
|
|
|
|
private const int MailSendTryCount = 3;
|
|
|
|
private BookingDB bookingDB = new BookingDB();
|
|
private ILog log = LogManager.GetLogger("SendMailJob");
|
|
|
|
public void Execute(IJobExecutionContext context)
|
|
{
|
|
var staCreate = SendMailStatus.Create.ToString();
|
|
var staSending = SendMailStatus.Sending.ToString();
|
|
var staSuccess = SendMailStatus.Success.ToString();
|
|
var staFail = SendMailStatus.Fail.ToString();
|
|
|
|
var sendMail = bookingDB.SendMails.Where(m => m.STATUS == staCreate).OrderBy(m => m.CREATE_TIME).FirstOrDefault();
|
|
if (sendMail != null)
|
|
{
|
|
//置发送状态
|
|
log.Debug($"准备发送邮件:{sendMail.TITLE}");
|
|
sendMail.STATUS = staSending;
|
|
sendMail.SEND_TIME = DateTime.Now;
|
|
sendMail.TRY_COUNT += 1;
|
|
bookingDB.SaveChanges();
|
|
|
|
//获取配置并发送
|
|
if (string.IsNullOrEmpty(SendMailServer)
|
|
|| string.IsNullOrEmpty(SendMailAccount)
|
|
|| string.IsNullOrEmpty(SendMailPassword)
|
|
|| SendMailPort <= 0)
|
|
{
|
|
log.Error($"邮件<{sendMail.TITLE}>发送失败:未正确配置邮件发送服务器");
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
var message = new MimeMessage();
|
|
message.Subject = sendMail.TITLE;
|
|
message.To.Add(new MailboxAddress(sendMail.SEND_TO));
|
|
message.MessageId = MimeUtils.GenerateMessageId();
|
|
message.Date = DateTimeOffset.Now;
|
|
message.From.Add(new MailboxAddress(SendMailAccount));
|
|
if (!string.IsNullOrEmpty(sendMail.CC_TO))
|
|
{
|
|
message.Cc.Add(new MailboxAddress(sendMail.CC_TO));
|
|
}
|
|
|
|
if (sendMail.BODY_TYPE == SendMailBodyType.Text.ToString())
|
|
{
|
|
var plain = new TextPart("plain")
|
|
{
|
|
Text = sendMail.BODY
|
|
};
|
|
|
|
message.Body = plain;
|
|
}
|
|
|
|
|
|
try
|
|
{
|
|
using (var client = new SmtpClient())
|
|
{
|
|
client.Connect(SendMailServer, SendMailPort, SendMailUseSSL);
|
|
client.Authenticate(SendMailAccount, SendMailPassword);
|
|
client.Send(message);
|
|
|
|
log.Debug($"已发送邮件:{message.Subject}");
|
|
|
|
client.Disconnect(true);
|
|
}
|
|
|
|
sendMail.STATUS = staSuccess;
|
|
bookingDB.SaveChanges();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (sendMail.TRY_COUNT < MailSendTryCount) //会继续尝试发送
|
|
{
|
|
sendMail.STATUS = staCreate;
|
|
bookingDB.SaveChanges();
|
|
}
|
|
else
|
|
{
|
|
sendMail.STATUS = staFail;
|
|
bookingDB.SaveChanges();
|
|
}
|
|
|
|
log.Error($"发送邮件<{message.Subject}>时出错:");
|
|
log.Error($"{ex.Message}");
|
|
log.Error($"{ex.StackTrace}");
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
} |