IcsFromERPJob
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
12 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using System.Data;
//using NFine.Data.Extensions;
using System.Configuration;
using ICSSoft.FromERP;
//using ICSSoft.SendMail;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
namespace ICSSoft.FromERP
{
//邮件
public class ICSSendEmail : IJob
{
private static object key = new object();
private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public void Execute(IJobExecutionContext context)
{
try
{
lock (key)
{
log.Info("开始……………………………………………………………………");
Execute();
log.Info("结束……………………………………………………………………");
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
public void Execute()
{
string erpName = ICSHelper.GetConfigString()["ERPDB"];
string conStr = ICSHelper.GetConnectString();
string Namespace = this.GetType().Namespace;
//string Class = this.GetType().Name;
DataTable dt = ICSHelper.GetERPDB(conStr);
foreach (DataRow dr1 in dt.Rows)
{
string WorkPoint = dr1["WorkPointCode"].ToString();
string Class = this.GetType().Name + WorkPoint;
erpName = string.Format(erpName, WorkPoint);
string sql = @"SELECT a.ASNID, a.STNO,a.VENDORCODE,b.cVenName AS VENDORNAME,a.Auditor,CONVERT(NVARCHAR(50),a.CREATETIME,23) as CREATETIME,
a.CREATEUSER,a.ADDITION1,a.ADDITION2,CONVERT(NVARCHAR(50),
a.EXPARRIVALDATE,23) as EXPARRIVALDATE,
CASE WHEN c.cCode IS NULL THEN a.STSTATUS ELSE '3' END AS STATUS
FROM dbo.ICSASN a
LEFT JOIN dbo.ICSVendor b ON a.VENDORCODE=b.cVenCode and a.WOrkPoint=b.WorkPoint
LEFT JOIN ICSPOArrive c ON a.STNO = c.STNO
where datediff( dd, EXPARRIVALDATE,getdate())>=3 and a.STSTATUS='2' and c.cCode IS NULL ";
DataTable dtInInfo = ICSHelper.ExecuteTable(conStr, sql);
if (dtInInfo.Rows.Count > 0)
{
string MailOpen = ICSHelper.GetConfigString("MailOpen");
if (MailOpen == "true")
{
string SendHost = ICSHelper.GetConfigString("SendHost");
string StrSendPort = ICSHelper.GetConfigString("SendPort");
int SendPort = 25;
if (!string.IsNullOrEmpty(StrSendPort))
SendPort = Convert.ToInt32(ICSHelper.GetConfigString("SendPort"));
string SendDisplayName = ICSHelper.GetConfigString("SendDisplayName");
string SendAddress = ICSHelper.GetConfigString("SendAddress");
string SendPassword = ICSHelper.GetConfigString("SendPassword");
foreach (DataRow dr in dtInInfo.Rows)
{
string cVenCode = dr["VENDORCODE"].ToString();
string PersonCode = dr["Auditor"].ToString();
string sqls = " SELECT F_Email FROM dbo.Sys_SRM_User WHERE F_Account= '" + cVenCode + "' or F_Account='" + PersonCode + "'";
DataTable dts = ICSHelper.ExecuteTable(conStr, sqls);
string Email = string.Empty;
foreach (DataRow drs in dts.Rows)
{
Email += drs["F_Email"].ToString() + ",";
}
string TOAddress = Email.TrimEnd(',');
string CCAddress = "";
string Subject = "有来自华恒SRM平台送货订单信息";
bool isBodyHtml = false;
string VenName = dr["VENDORNAME"].ToString();
string STNO = dr["STNO"].ToString();
string NowDate = DateTime.Now.GetDateTimeFormats('D')[0].ToString();
string body = VenName + ":";
body += "\r\n";
body += " 您好!有来自华恒SRM送货订单:" + STNO + " ,已超3天未入库,请联系相关人员查询!";
body += "\r\n";
body += " 顺颂商祺!";
body += "\r\n";
body += " 昆山华恒焊接股份有限公司";
body += "\r\n";
body += " " + NowDate;
//string StrConn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
SendEmail(conStr, SendHost, SendPort, SendDisplayName, SendAddress, SendPassword, TOAddress, CCAddress, Subject, isBodyHtml, body);
//ICSHelper.ExecuteTable(conStr, null, sql);
}
}
}
}
}
public static void SendEmail(string ConnectionString, string SendHost, int SendPort, string SendDisplayName, string SendAddress, string SendPassword, string TOAddress, string CCAddress, string Subject, bool IsBodyHtml, string Body)
{
try
{
SmtpClient smtpClient = new SmtpClient
{
//EnableSsl = false,
UseDefaultCredentials = false,
Host = SendHost,
Port = SendPort,
Credentials = new NetworkCredential(SendAddress, SendPassword)
};
MailMessage mailMessage = new MailMessage
{
Subject = Subject,
SubjectEncoding = Encoding.GetEncoding("utf-8"),
BodyEncoding = Encoding.GetEncoding("utf-8"),
From = new MailAddress(SendAddress, SendDisplayName),
IsBodyHtml = IsBodyHtml,
Body = Body
};
string[] array = TOAddress.Split(new char[]
{
','
});
string[] array2 = array;
for (int i = 0; i < array2.Length; i++)
{
string text = array2[i];
if (!string.IsNullOrEmpty(text))
{
mailMessage.To.Add(text);
}
}
string[] array3 = CCAddress.Split(new char[]
{
','
});
array2 = array3;
for (int i = 0; i < array2.Length; i++)
{
string text2 = array2[i];
if (!string.IsNullOrEmpty(text2))
{
mailMessage.CC.Add(text2);
}
}
ServicePointManager.ServerCertificateValidationCallback = ((object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true);
smtpClient.Send(mailMessage);
InsertData(ConnectionString, SendHost, SendPort, SendDisplayName, SendAddress, SendPassword, TOAddress, CCAddress, Subject, IsBodyHtml, Body, "1", null);
}
catch (Exception ex)
{
InsertData(ConnectionString, SendHost, SendPort, SendDisplayName, SendAddress, SendPassword, TOAddress, CCAddress, Subject, IsBodyHtml, Body, "2", ex.Message);
throw;
}
}
private static void InsertData(string ConnectionString, string SendHost, int SendPort, string SendDisplayName, string SendAddress, string SendPassword, string TOAddress, string CCAddress, string Subject, bool IsBodyHtml, string Body, string Status, string Message)
{
try
{
try
{
string text = "INSERT INTO ICSSendMail(ID, SendHost, SendPort, SendDisplayName, SendAddress, \r\n SendPassword, TOAddress, CCAddress, Subject, IsBodyHtml, \r\n Body, Retry, Status, Message, MUSER, MUSERName, CreatTIME, ModifyTIME)\r\n VALUES(NEWID(),'{0}','{1}','{2}','{3}',\r\n '{4}','{5}','{6}','{7}','{8}',\r\n '{9}','0','{10}','{11}','','',GETDATE(),GETDATE())";
text = string.Format(text, new object[]
{
SendHost,
SendPort,
SendDisplayName,
SendAddress,
SendPassword,
TOAddress,
CCAddress,
Subject,
IsBodyHtml,
Body.Replace("'", "''"),
Status,
Message
});
ExecuteDate(ConnectionString, text);
}
catch (Exception ex)
{
throw ex;
}
}
catch (Exception ex)
{
throw ex;
}
}
private static void ExecuteDate(string conStr, string sql)
{
try
{
using (SqlConnection sqlConnection = new SqlConnection(conStr))
{
sqlConnection.Open();
try
{
using (SqlTransaction sqlTransaction = sqlConnection.BeginTransaction())
{
using (SqlCommand sqlCommand = new SqlCommand())
{
sqlCommand.Connection = sqlConnection;
sqlCommand.Transaction = sqlTransaction;
sqlCommand.CommandText = sql;
try
{
int num = sqlCommand.ExecuteNonQuery();
sqlTransaction.Commit();
}
catch (Exception ex)
{
sqlTransaction.Rollback();
throw ex;
}
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (sqlConnection.State == ConnectionState.Open)
{
sqlConnection.Close();
}
sqlConnection.Dispose();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}