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/JobSendCtn.cs

108 lines
4.0 KiB
C#

1 year ago
using log4net;
using Quartz;
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using MailKit.Net.Smtp;
using MimeKit;
using HtmlAgilityPack;
namespace JobSendAgentMail
{
public class JobSendCtn : IJob
{
private ILog log = LogManager.GetLogger(typeof(JobSendCtn));
public void Execute(IJobExecutionContext context)
{
string connStr = context.JobDetail.JobDataMap.GetString("ConnectString");
string querySql = context.JobDetail.JobDataMap.GetString("QuerySql");
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");
//string MAILTEMPLATE = context.JobDetail.JobDataMap.GetString("MAILTEMPLATE");
string MAILTITLEJOB = context.JobDetail.JobDataMap.GetString("MAILTITLE");
string receiver = context.JobDetail.JobDataMap.GetString("MAILRECEIVER");
log.Debug($"开始执行发送邮件");
string maildescrption = "";
using (SqlConnection dbcon = new SqlConnection(connStr))
{
SqlDataAdapter adapter = new SqlDataAdapter(querySql, dbcon);
DataTable table = new DataTable();
adapter.Fill(table);
if (table.Columns.Contains("箱号"))
{
if (table.Rows.Count > 0)
{
foreach (DataRow row in table.Rows)
{
if (row["箱号"].ToString() != "")
{
maildescrption = maildescrption + Environment.NewLine + row["箱号"].ToString();
}
else {
log.Debug($"资料不全:{row["BSNO"].ToString()}");
}
}
}
else
{
log.Debug($"未查询到数据SQL语句{querySql}");
}
if (!string.IsNullOrEmpty(maildescrption)) {
try
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress(MAILSENDACCOUNT, MAILSENDACCOUNT));
message.To.Add(MailboxAddress.Parse(receiver));
message.Subject = MAILTITLEJOB;
var bodyBuilder = new BodyBuilder();
bodyBuilder.TextBody = maildescrption;
message.Body = bodyBuilder.ToMessageBody();
using (var client = new SmtpClient())
{
client.Connect(MAILSENDSERVICE, Convert.ToInt16(MAILSENDPORT), Convert.ToBoolean(MAILISSSL));
client.Authenticate(MAILSENDACCOUNT, MAILSENDPASSWORD);
client.Send(message);
client.Disconnect(true);
}
log.Debug($"邮件发送成功");
}
catch (Exception ex)
{
log.Debug($"邮件发送失败:错误信息{ex.Message}");
}
}
}
else
{
log.Error($"未包含所需的列(mblno,carrierid,portloadid)SQL语句{querySql}");
}
}
}
}
}