纽威
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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Quartz;
  6. using System.Data;
  7. //using NFine.Data.Extensions;
  8. using System.Configuration;
  9. using ICSSoft.FromERP;
  10. //using ICSSoft.SendMail;
  11. using System.Data.SqlClient;
  12. using System.Net;
  13. using System.Net.Mail;
  14. using System.Security.Cryptography.X509Certificates;
  15. using System.Net.Security;
  16. namespace ICSSoft.FromERP
  17. {
  18. //邮件
  19. public class ICSSendEmail : IJob
  20. {
  21. private static object key = new object();
  22. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  23. public void Execute(IJobExecutionContext context)
  24. {
  25. try
  26. {
  27. lock (key)
  28. {
  29. log.Info("开始……………………………………………………………………");
  30. Execute();
  31. log.Info("结束……………………………………………………………………");
  32. }
  33. }
  34. catch (Exception ex)
  35. {
  36. log.Error(ex.ToString());
  37. }
  38. }
  39. public void Execute()
  40. {
  41. string erpName = ICSHelper.GetConfigString()["ERPDB"];
  42. string conStr = ICSHelper.GetConnectString();
  43. string Namespace = this.GetType().Namespace;
  44. //string Class = this.GetType().Name;
  45. DataTable dt = ICSHelper.GetERPDB(conStr);
  46. foreach (DataRow dr1 in dt.Rows)
  47. {
  48. string WorkPoint = dr1["WorkPointCode"].ToString();
  49. string Class = this.GetType().Name + WorkPoint;
  50. erpName = string.Format(erpName, WorkPoint);
  51. string sql = @"SELECT a.ASNID, a.STNO,a.VENDORCODE,b.cVenName AS VENDORNAME,a.Auditor,CONVERT(NVARCHAR(50),a.CREATETIME,23) as CREATETIME,
  52. a.CREATEUSER,a.ADDITION1,a.ADDITION2,CONVERT(NVARCHAR(50),
  53. a.EXPARRIVALDATE,23) as EXPARRIVALDATE,
  54. CASE WHEN c.cCode IS NULL THEN a.STSTATUS ELSE '3' END AS STATUS
  55. FROM dbo.ICSASN a
  56. LEFT JOIN dbo.ICSVendor b ON a.VENDORCODE=b.cVenCode and a.WOrkPoint=b.WorkPoint
  57. LEFT JOIN ICSPOArrive c ON a.STNO = c.STNO
  58. where datediff( dd, EXPARRIVALDATE,getdate())>=3 and a.STSTATUS='2' and c.cCode IS NULL ";
  59. DataTable dtInInfo = ICSHelper.ExecuteTable(conStr, sql);
  60. if (dtInInfo.Rows.Count > 0)
  61. {
  62. string MailOpen = ICSHelper.GetConfigString("MailOpen");
  63. if (MailOpen == "true")
  64. {
  65. string SendHost = ICSHelper.GetConfigString("SendHost");
  66. string StrSendPort = ICSHelper.GetConfigString("SendPort");
  67. int SendPort = 25;
  68. if (!string.IsNullOrEmpty(StrSendPort))
  69. SendPort = Convert.ToInt32(ICSHelper.GetConfigString("SendPort"));
  70. string SendDisplayName = ICSHelper.GetConfigString("SendDisplayName");
  71. string SendAddress = ICSHelper.GetConfigString("SendAddress");
  72. string SendPassword = ICSHelper.GetConfigString("SendPassword");
  73. foreach (DataRow dr in dtInInfo.Rows)
  74. {
  75. string cVenCode = dr["VENDORCODE"].ToString();
  76. string PersonCode = dr["Auditor"].ToString();
  77. string sqls = " SELECT F_Email FROM dbo.Sys_SRM_User WHERE F_Account= '" + cVenCode + "' or F_Account='" + PersonCode + "'";
  78. DataTable dts = ICSHelper.ExecuteTable(conStr, sqls);
  79. string Email = string.Empty;
  80. foreach (DataRow drs in dts.Rows)
  81. {
  82. Email += drs["F_Email"].ToString() + ",";
  83. }
  84. string TOAddress = Email.TrimEnd(',');
  85. string CCAddress = "";
  86. string Subject = "有来自华恒SRM平台送货订单信息";
  87. bool isBodyHtml = false;
  88. string VenName = dr["VENDORNAME"].ToString();
  89. string STNO = dr["STNO"].ToString();
  90. string NowDate = DateTime.Now.GetDateTimeFormats('D')[0].ToString();
  91. string body = VenName + ":";
  92. body += "\r\n";
  93. body += " 您好!有来自华恒SRM送货订单:" + STNO + " ,已超3天未入库,请联系相关人员查询!";
  94. body += "\r\n";
  95. body += " 顺颂商祺!";
  96. body += "\r\n";
  97. body += " 昆山华恒焊接股份有限公司";
  98. body += "\r\n";
  99. body += " " + NowDate;
  100. //string StrConn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
  101. SendEmail(conStr, SendHost, SendPort, SendDisplayName, SendAddress, SendPassword, TOAddress, CCAddress, Subject, isBodyHtml, body);
  102. //ICSHelper.ExecuteTable(conStr, null, sql);
  103. }
  104. }
  105. }
  106. }
  107. }
  108. 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)
  109. {
  110. try
  111. {
  112. SmtpClient smtpClient = new SmtpClient
  113. {
  114. //EnableSsl = false,
  115. UseDefaultCredentials = false,
  116. Host = SendHost,
  117. Port = SendPort,
  118. Credentials = new NetworkCredential(SendAddress, SendPassword)
  119. };
  120. MailMessage mailMessage = new MailMessage
  121. {
  122. Subject = Subject,
  123. SubjectEncoding = Encoding.GetEncoding("utf-8"),
  124. BodyEncoding = Encoding.GetEncoding("utf-8"),
  125. From = new MailAddress(SendAddress, SendDisplayName),
  126. IsBodyHtml = IsBodyHtml,
  127. Body = Body
  128. };
  129. string[] array = TOAddress.Split(new char[]
  130. {
  131. ','
  132. });
  133. string[] array2 = array;
  134. for (int i = 0; i < array2.Length; i++)
  135. {
  136. string text = array2[i];
  137. if (!string.IsNullOrEmpty(text))
  138. {
  139. mailMessage.To.Add(text);
  140. }
  141. }
  142. string[] array3 = CCAddress.Split(new char[]
  143. {
  144. ','
  145. });
  146. array2 = array3;
  147. for (int i = 0; i < array2.Length; i++)
  148. {
  149. string text2 = array2[i];
  150. if (!string.IsNullOrEmpty(text2))
  151. {
  152. mailMessage.CC.Add(text2);
  153. }
  154. }
  155. ServicePointManager.ServerCertificateValidationCallback = ((object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true);
  156. smtpClient.Send(mailMessage);
  157. InsertData(ConnectionString, SendHost, SendPort, SendDisplayName, SendAddress, SendPassword, TOAddress, CCAddress, Subject, IsBodyHtml, Body, "1", null);
  158. }
  159. catch (Exception ex)
  160. {
  161. InsertData(ConnectionString, SendHost, SendPort, SendDisplayName, SendAddress, SendPassword, TOAddress, CCAddress, Subject, IsBodyHtml, Body, "2", ex.Message);
  162. throw;
  163. }
  164. }
  165. 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)
  166. {
  167. try
  168. {
  169. try
  170. {
  171. 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())";
  172. text = string.Format(text, new object[]
  173. {
  174. SendHost,
  175. SendPort,
  176. SendDisplayName,
  177. SendAddress,
  178. SendPassword,
  179. TOAddress,
  180. CCAddress,
  181. Subject,
  182. IsBodyHtml,
  183. Body.Replace("'", "''"),
  184. Status,
  185. Message
  186. });
  187. ExecuteDate(ConnectionString, text);
  188. }
  189. catch (Exception ex)
  190. {
  191. throw ex;
  192. }
  193. }
  194. catch (Exception ex)
  195. {
  196. throw ex;
  197. }
  198. }
  199. private static void ExecuteDate(string conStr, string sql)
  200. {
  201. try
  202. {
  203. using (SqlConnection sqlConnection = new SqlConnection(conStr))
  204. {
  205. sqlConnection.Open();
  206. try
  207. {
  208. using (SqlTransaction sqlTransaction = sqlConnection.BeginTransaction())
  209. {
  210. using (SqlCommand sqlCommand = new SqlCommand())
  211. {
  212. sqlCommand.Connection = sqlConnection;
  213. sqlCommand.Transaction = sqlTransaction;
  214. sqlCommand.CommandText = sql;
  215. try
  216. {
  217. int num = sqlCommand.ExecuteNonQuery();
  218. sqlTransaction.Commit();
  219. }
  220. catch (Exception ex)
  221. {
  222. sqlTransaction.Rollback();
  223. throw ex;
  224. }
  225. }
  226. }
  227. }
  228. catch (Exception ex)
  229. {
  230. throw ex;
  231. }
  232. finally
  233. {
  234. if (sqlConnection.State == ConnectionState.Open)
  235. {
  236. sqlConnection.Close();
  237. }
  238. sqlConnection.Dispose();
  239. }
  240. }
  241. }
  242. catch (Exception ex)
  243. {
  244. throw ex;
  245. }
  246. }
  247. }
  248. }