华恒Mes鼎捷代码
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.

163 lines
7.6 KiB

5 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Net.Mail;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using ICSSoft.Base.Config.AppConfig;
  9. using ICSSoft.Base.Config.DBHelper;
  10. namespace ICSSoft.Frame.AutoSendMail
  11. {
  12. class AutoSendMail
  13. {
  14. static void Main(string[] args)
  15. {
  16. try
  17. {
  18. string connstring = System.Configuration.ConfigurationManager.ConnectionStrings["SysConnectionString"].ToString();//数据库链接
  19. string days = "";
  20. for (int i = 0; i < 2; i++)
  21. {
  22. string sql = @" select distinct
  23. d.WHCode ,
  24. e.StorageName ,
  25. d.BinCode ,
  26. f.StackName ,
  27. c.LotNO ,
  28. a.INVCODE ,
  29. b.INVNAME ,
  30. c.PRODUCTDATE ,
  31. a.EATTRIBUTE1 as ,
  32. case when isnull(c.PRODUCTDATE,0)=0 then '' when isnull(a.EATTRIBUTE1,0)=0 then c.PRODUCTDATE ELSE DATEADD(day,cast(a.EATTRIBUTE1 as int),c.PRODUCTDATE)
  33. end as ,
  34. a.MUSERName ,
  35. d.LotQty ,
  36. b.INVUOM ,
  37. a.MTIME
  38. FROM dbo.ICSWareHouseLotInfo d
  39. inner join ICSITEMLot c on c.LotNO=d.LotNO
  40. inner join dbo.ICSINVInfo a on a.INVCODE=d.INVCODE
  41. inner join ICSINVENTORY b on a.INVCODE=b.INVCODE
  42. LEFT JOIN dbo.ICSStorage e ON d.WHCode=e.StorageCode
  43. left JOIN dbo.ICSStack f ON d.BinCode=f.StackCode
  44. where 1=1 and a.INVCODE<>'' and c.LotNO<>'' and d.LotQty>0 and datediff(day,DATEADD(day,cast(a.EATTRIBUTE1 as int),c.PRODUCTDATE),getdate()) BETWEEN 0 and {0}";
  45. //7天和3天发送邮件
  46. if (i == 0)
  47. {
  48. sql += " and ISNULL(d.IsMail1,0)=0";
  49. days = System.Configuration.ConfigurationManager.AppSettings["MailDays1"].ToString();//天数
  50. }
  51. else
  52. {
  53. sql += " and ISNULL(d.IsMail1,0)=1 and ISNULL(d.IsMail2,0)=0";
  54. days = System.Configuration.ConfigurationManager.AppSettings["MailDays2"].ToString();//天数
  55. }
  56. sql = string.Format(sql, days);
  57. DataTable dt = DBHelper.ExecuteDataset(connstring, CommandType.Text, sql).Tables[0];
  58. if (dt == null || dt.Rows.Count == 0) continue;
  59. string html = @"
  60. <table border= '1' cellspacing='0' cellpadding='0'><tbody>";
  61. html += "您好,库存中<b>" + dt.Rows.Count + "</b>条物料条码的保质期已不足" + days + "天,请注意及时处理。<br /><br />";
  62. html += @"
  63. <tr>
  64. <th></th>
  65. <th scope='col'></th>
  66. <th scope='col'></th>
  67. <th scope='col'></th>
  68. <th scope='col'></th>
  69. <th scope='col'></th>
  70. <th scope='col'></th>
  71. <th scope='col'></th>
  72. <th scope='col'></th>
  73. </tr>
  74. ";
  75. int m = 0;
  76. foreach (DataRow dr in dt.Rows)
  77. {
  78. m++;
  79. html += @"<tr><td>" + m + "</td>";
  80. html += @"<td>" + dr["仓库编码"].ToString() + "</td>";
  81. html += @"<td>" + dr["库位编码"].ToString() + "</td>";
  82. html += @"<td>" + dr["条码"].ToString() + "</td>";
  83. html += @"<td>" + dr["存货编码"].ToString() + "</td>";
  84. html += @"<td>" + dr["存货名称"].ToString() + "</td>";
  85. html += @"<td>" + dr["数量"].ToString() + "</td>";
  86. html += @"<td>" + dr["单位"].ToString() + "</td>";
  87. html += @"<td>" + dr["到期日"].ToString() + "</td>";
  88. html += "</tr>";
  89. }
  90. html += "智合诚物料条码到期预警邮件通知,如有疑问,请联系我们。";
  91. html += @"</tbody>
  92. </table>";
  93. html = html.Replace("'", "\"");
  94. string isMail = System.Configuration.ConfigurationManager.AppSettings["IsMail"].ToString();
  95. if (isMail == "0")
  96. {
  97. return;
  98. }
  99. else if (isMail == "1")
  100. {
  101. string send = System.Configuration.ConfigurationManager.AppSettings["Send"].ToString();//发送邮箱地址
  102. string sendAccount = System.Configuration.ConfigurationManager.AppSettings["SendAccount"].ToString();//发送邮箱账号
  103. string sendPassWord = System.Configuration.ConfigurationManager.AppSettings["SendPassWord"].ToString();//发送邮箱账号
  104. string[] to = System.Configuration.ConfigurationManager.AppSettings["To"].ToString().Split(',');//接受邮箱地址
  105. string host = System.Configuration.ConfigurationManager.AppSettings["Host"].ToString();//邮件服务器
  106. string port = System.Configuration.ConfigurationManager.AppSettings["Port"].ToString();//端口 默认25
  107. MailMessage message = new MailMessage();
  108. message.From = new MailAddress(send);
  109. foreach (string s in to)
  110. {
  111. message.To.Add(s);
  112. }
  113. message.Subject = "物料条码保质期预警-" + DateTime.Now.ToString("yyyy-MM-dd");
  114. message.Body = html;
  115. message.SubjectEncoding = System.Text.Encoding.UTF8; //邮件标题编码
  116. message.BodyEncoding = System.Text.Encoding.UTF8; //邮件内容编码
  117. message.IsBodyHtml = true; //是否是HTML邮件
  118. message.Priority = MailPriority.Normal; //邮件优先级
  119. SmtpClient client = new SmtpClient();
  120. client.Credentials = new System.Net.NetworkCredential(sendAccount, sendPassWord);
  121. client.Port = int.Parse(port);
  122. client.Host = host;
  123. client.Send(message);
  124. foreach (DataRow dr in dt.Rows)
  125. {
  126. if (i == 0)
  127. sql = @"update ICSWareHouseLotInfo set IsMail1=1 where LotNO='{0}' and WHCode='{1}' and BinCode='{2}'";
  128. else
  129. sql = @"update ICSWareHouseLotInfo set IsMail2=1 where LotNO='{0}' and WHCode='{1}' and BinCode='{2}'";
  130. sql = string.Format(sql, dr["条码"].ToString(), dr["仓库编码"].ToString(), dr["库位编码"].ToString());
  131. DBHelper.ExecuteDataset(connstring, CommandType.Text, sql);
  132. }
  133. }
  134. else
  135. {
  136. return;
  137. }
  138. }
  139. }
  140. catch (Exception ex)
  141. {
  142. throw ex;
  143. }
  144. }
  145. }
  146. }