|
|
using log4net;
|
|
|
using Newtonsoft.Json;
|
|
|
using Quartz;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
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 JobSendMail
|
|
|
{
|
|
|
public class JobSendMail : IJob
|
|
|
{
|
|
|
private ILog log = LogManager.GetLogger(typeof(JobSendMail));
|
|
|
|
|
|
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");
|
|
|
log.Debug($"开始执行代理发送邮件");
|
|
|
using (SqlConnection dbcon = new SqlConnection(connStr))
|
|
|
{
|
|
|
SqlDataAdapter adapter = new SqlDataAdapter(querySql, dbcon);
|
|
|
DataTable table = new DataTable();
|
|
|
adapter.Fill(table);
|
|
|
if (table.Columns.Contains("RECEIVER") && table.Columns.Contains("SUBJECT"))
|
|
|
{
|
|
|
if (table.Rows.Count > 0)
|
|
|
{
|
|
|
|
|
|
foreach (DataRow row in table.Rows)
|
|
|
{
|
|
|
if (row["RECEIVER"].ToString() != "" && row["SUBJECT"].ToString() != "")
|
|
|
{
|
|
|
string sqlexe = "update op_mail_send set SENDSTATUS='Send',TRYCOUNT=TRYCOUNT+1 where GID='" + row["GID"].ToString() + "'";
|
|
|
dbcon.Open();
|
|
|
SqlCommand cmdexe = new SqlCommand(sqlexe, dbcon);
|
|
|
cmdexe.ExecuteNonQuery();
|
|
|
dbcon.Close();
|
|
|
|
|
|
var content = row["DESCRIPTION"].ToString();
|
|
|
|
|
|
|
|
|
var MAILTITLE = row["SUBJECT"].ToString();
|
|
|
|
|
|
|
|
|
StringWriter writer = new StringWriter();
|
|
|
HtmlDocument htmlDoc = new HtmlDocument();
|
|
|
htmlDoc.LoadHtml(content);
|
|
|
htmlDoc.Save(writer);
|
|
|
string str = writer.ToString();
|
|
|
try
|
|
|
{
|
|
|
var message = new MimeMessage();
|
|
|
message.From.Add(new MailboxAddress(row["SENDER"].ToString(), row["SENDER"].ToString()));
|
|
|
var maillist = row["RECEIVER"].ToString().Split(';');
|
|
|
foreach (var mailaddr in maillist)
|
|
|
{
|
|
|
message.To.Add(MailboxAddress.Parse(mailaddr));
|
|
|
}
|
|
|
message.To.Add(MailboxAddress.Parse(row["SENDER"].ToString()));
|
|
|
// message.Subject = "Pre-alert/" + row["ORDERNO"].ToString() + "/" + row["HBLNO"].ToString();
|
|
|
message.Subject = MAILTITLE;
|
|
|
var html = new TextPart("html")
|
|
|
{
|
|
|
Text = str
|
|
|
};
|
|
|
MimeEntity entity = html;
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
string sqlexe2 = "update op_mail_send set SENDSTATUS='Success' where GID='"+ row["GID"].ToString() + "'";
|
|
|
dbcon.Open();
|
|
|
SqlCommand cmdexe2 = new SqlCommand(sqlexe, dbcon);
|
|
|
cmdexe.ExecuteNonQuery();
|
|
|
dbcon.Close();
|
|
|
|
|
|
|
|
|
log.Debug($"邮件发送成功");
|
|
|
|
|
|
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
string sqlexe2 = "update op_mail_send set SENDSTATUS='Fail' where GID='" + row["GID"].ToString() + "'";
|
|
|
dbcon.Open();
|
|
|
SqlCommand cmdexe2 = new SqlCommand(sqlexe, dbcon);
|
|
|
cmdexe.ExecuteNonQuery();
|
|
|
dbcon.Close();
|
|
|
log.Debug($"邮件发送失败:错误信息{ex.Message}");
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
else {
|
|
|
log.Debug($"资料不全:{row["BSNO"].ToString()}");
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
log.Debug($"未查询到数据,SQL语句:{querySql}");
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
log.Error($"未包含所需的列(mblno,carrierid,portloadid),SQL语句:{querySql}");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|