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

219 lines
13 KiB
C#

3 years 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 JobSendBooking : IJob
{
private ILog log = LogManager.GetLogger(typeof(JobSendBooking));
public void Execute(IJobExecutionContext context)
{
string connStr = context.JobDetail.JobDataMap.GetString("ConnectString");
string querySql = context.JobDetail.JobDataMap.GetString("QuerySql");
string hblquerySql = context.JobDetail.JobDataMap.GetString("HblQuerySql");
string DocType = context.JobDetail.JobDataMap.GetString("DocType");
string D7FilePath = context.JobDetail.JobDataMap.GetString("D7FilePath");
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");
2 years ago
string MAILTITLEJOB = context.JobDetail.JobDataMap.GetString("MAILTITLE");
3 years ago
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("MBLNO") && table.Columns.Contains("AGENTID"))
{
if (table.Rows.Count > 0)
{
foreach (DataRow row in table.Rows)
{
if (row["MBLNO"].ToString() != "" && row["AGENTID"].ToString() != "")
{
var htmlFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, MAILTEMPLATE);
var content = File.ReadAllText(htmlFile);
2 years ago
var REMARKSTR = row["BLREMARK"].ToString();
REMARKSTR = REMARKSTR.Replace("\r\n", "<br />");
REMARKSTR = REMARKSTR.Replace("\n", "<br />");
REMARKSTR = REMARKSTR.Replace("\r", "");
3 years ago
content = content.Replace("$MBLNO$", row["MBLNO"].ToString());
content = content.Replace("$ETD$", row["ETD"].ToString());
content = content.Replace("$BLFRT$", row["BLFRT"].ToString());
content = content.Replace("$PORTLOAD$", row["PORTLOAD"].ToString());
content = content.Replace("$PORTDISCHARGE$", row["PORTDISCHARGE"].ToString());
content = content.Replace("$DESTINATION$", row["DESTINATION"].ToString());
content = content.Replace("$ATD$", row["ATD"].ToString());
content = content.Replace("$HBLNO$", row["HBLNO"].ToString());
content = content.Replace("$ORDERNO$", row["ORDERNO"].ToString());
content = content.Replace("$ETA$", row["ETA"].ToString());
content = content.Replace("$CNTRTOTAL$", row["CNTRTOTAL"].ToString());
content = content.Replace("$CARRIERID$", row["CARRIERID"].ToString());
content = content.Replace("$ISSUETYPE$", row["ISSUETYPE"].ToString());
2 years ago
content = content.Replace("$BLREMARK$", REMARKSTR);
3 years ago
2 years ago
var MAILTITLE = MAILTITLEJOB;
3 years ago
MAILTITLE = MAILTITLE.Replace("$MBLNO$", row["MBLNO"].ToString());
MAILTITLE = MAILTITLE.Replace("$ETD$", row["ETD"].ToString());
MAILTITLE = MAILTITLE.Replace("$BLFRT$", row["BLFRT"].ToString());
MAILTITLE = MAILTITLE.Replace("$PORTLOAD$", row["PORTLOAD"].ToString());
MAILTITLE = MAILTITLE.Replace("$PORTDISCHARGE$", row["PORTDISCHARGE"].ToString());
MAILTITLE = MAILTITLE.Replace("$DESTINATION$", row["DESTINATION"].ToString());
MAILTITLE = MAILTITLE.Replace("$ATD$", row["ATD"].ToString());
MAILTITLE = MAILTITLE.Replace("$HBLNO$", row["HBLNO"].ToString());
MAILTITLE = MAILTITLE.Replace("$ORDERNO$", row["ORDERNO"].ToString());
MAILTITLE = MAILTITLE.Replace("$ETA$", row["ETA"].ToString());
MAILTITLE = MAILTITLE.Replace("$CNTRTOTAL$", row["CNTRTOTAL"].ToString());
MAILTITLE = MAILTITLE.Replace("$CARRIERID$", row["CARRIERID"].ToString());
MAILTITLE = MAILTITLE.Replace("$ISSUETYPE$", row["ISSUETYPE"].ToString());
3 years ago
MAILTITLE = MAILTITLE.Replace("$BLREMARK$", row["BLREMARK"].ToString());
3 years ago
SqlDataAdapter hbladapter = new SqlDataAdapter(hblquerySql + " where BSNO='" + row["BSNO"].ToString() + "'", dbcon);
DataTable hbltable = new DataTable();
hbladapter.Fill(hbltable);
var hblstr = "";
if (hbltable.Rows.Count > 0)
{
foreach (DataRow hblrow in hbltable.Rows)
{
hblstr = hblstr + "HB/L:" + hblrow["HBLNO"].ToString() + "-" + hblrow["ISSUETYPE"].ToString() + ":hold<br />";
}
}
content = content.Replace("$HBLCONTENT$", hblstr);
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["OPNAME"].ToString(), row["OPEMAIL"].ToString()));
var maillist = row["AGENTEMAIL"].ToString().Split(';');
foreach (var mailaddr in maillist)
{
message.To.Add(MailboxAddress.Parse(mailaddr));
}
message.To.Add(MailboxAddress.Parse(row["OPEMAIL"].ToString()));
// message.Subject = "Pre-alert/" + row["ORDERNO"].ToString() + "/" + row["HBLNO"].ToString();
message.Subject = MAILTITLE;
var html = new TextPart("html")
{
Text = str
};
MimeEntity entity = html;
//SendEmail se = new SendEmail(row["AGENTEMAIL"].ToString(), row["OPEMAIL"].ToString(), "", row["OPNAME"].ToString(), "Pre-alert/" + row["ORDERNO"].ToString() + "/" + row["HBLNO"].ToString(), str,true);
SqlDataAdapter fileadapter = new SqlDataAdapter("SELECT * FROM Receipt_Doc where RECEIPTTYPE='" + DocType + "' and BSNO='" + row["BSNO"].ToString() + "'", dbcon);
DataTable filetable = new DataTable();
fileadapter.Fill(filetable);
if (filetable.Rows.Count > 0)
{
var mult = new Multipart("mixed") { html };
foreach (DataRow filerow in filetable.Rows)
{
var attfile = filerow["Driect_URL"].ToString();
if (!string.IsNullOrEmpty(attfile))
{
attfile = attfile.Replace("../../", D7FilePath);
while ((attfile.IndexOf("/") >= 0))
{
attfile = attfile.Replace("/", @"\");
}
if (File.Exists(attfile))
{
var tmpDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "temp");
if (!Directory.Exists(tmpDir))
{
Directory.CreateDirectory(tmpDir);
}
var tmpFile = Path.Combine(tmpDir, filerow["URL"].ToString());
File.Copy(attfile, tmpFile, true);
var attPart = new MimePart();
attPart.Content = new MimeContent(new FileStream(attfile, FileMode.Open));
attPart.ContentDisposition = new ContentDisposition(ContentDisposition.Attachment);
attPart.FileName = filerow["URL"].ToString();
mult.Add(attPart);
//se.Attachments(attfile);
}
else
{
log.Debug($"附件文件:{attfile}不存在");
// logger.Error($"邮件{sendMail.GID}的附件文件{att.FilePath}不存在");
}
}
}
entity = mult;
}
//se.SetSmtp(MAILSENDACCOUNT, MAILSENDPASSWORD, MAILSENDSERVICE, Convert.ToInt16(MAILSENDPORT), Convert.ToBoolean(MAILISSSL), System.Net.Mail.MailPriority.Normal); //必须在所有参数设置完后调用此方法
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);
}
log.Debug($"邮件发送成功");
}
catch (Exception ex)
{
log.Debug($"邮件发送失败:错误信息{ex.Message}");
}
}
else {
log.Debug($"资料不全:{row["BSNO"].ToString()}");
}
}
}
else
{
log.Debug($"未查询到数据SQL语句{querySql}");
}
}
else
{
log.Error($"未包含所需的列(mblno,carrierid,portloadid)SQL语句{querySql}");
}
}
}
}
}