|
|
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Net.Mail; using System.Text; using System.Threading.Tasks; using ICSSoft.Base.Config.AppConfig; using ICSSoft.Base.Config.DBHelper;
namespace ICSSoft.Frame.AutoSendMail { class AutoSendMail { static void Main(string[] args) { try { string connstring = System.Configuration.ConfigurationManager.ConnectionStrings["SysConnectionString"].ToString();//数据库链接
string days = "";
for (int i = 0; i < 2; i++) { string sql = @" select distinct
d.WHCode 仓库编码, e.StorageName 仓库名称, d.BinCode 库位编码, f.StackName 库位名称, c.LotNO 条码, a.INVCODE 存货编码, b.INVNAME 存货名称, c.PRODUCTDATE 生产日期, a.EATTRIBUTE1 as 保质期, 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) end as 到期日, a.MUSERName 维护人, d.LotQty 数量, b.INVUOM 单位, a.MTIME 维护时间 FROM dbo.ICSWareHouseLotInfo d inner join ICSITEMLot c on c.LotNO=d.LotNO inner join dbo.ICSINVInfo a on a.INVCODE=d.INVCODE inner join ICSINVENTORY b on a.INVCODE=b.INVCODE LEFT JOIN dbo.ICSStorage e ON d.WHCode=e.StorageCode left JOIN dbo.ICSStack f ON d.BinCode=f.StackCode 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}";
//7天和3天发送邮件
if (i == 0) { sql += " and ISNULL(d.IsMail1,0)=0"; days = System.Configuration.ConfigurationManager.AppSettings["MailDays1"].ToString();//天数
} else { sql += " and ISNULL(d.IsMail1,0)=1 and ISNULL(d.IsMail2,0)=0"; days = System.Configuration.ConfigurationManager.AppSettings["MailDays2"].ToString();//天数
} sql = string.Format(sql, days); DataTable dt = DBHelper.ExecuteDataset(connstring, CommandType.Text, sql).Tables[0]; if (dt == null || dt.Rows.Count == 0) continue;
string html = @"
<table border= '1' cellspacing='0' cellpadding='0'><tbody>";
html += "您好,库存中<b>" + dt.Rows.Count + "</b>条物料条码的保质期已不足" + days + "天,请注意及时处理。<br /><br />"; html += @"
<tr> <th>行号</th> <th scope='col'>仓库</th> <th scope='col'>库位</th> <th scope='col'>条码</th> <th scope='col'>物料编号</th> <th scope='col'>存货名称</th> <th scope='col'>数量</th> <th scope='col'>单位</th> <th scope='col'>到期日</th> </tr> ";
int m = 0; foreach (DataRow dr in dt.Rows) { m++; html += @"<tr><td>" + m + "</td>"; html += @"<td>" + dr["仓库编码"].ToString() + "</td>"; html += @"<td>" + dr["库位编码"].ToString() + "</td>"; html += @"<td>" + dr["条码"].ToString() + "</td>"; html += @"<td>" + dr["存货编码"].ToString() + "</td>"; html += @"<td>" + dr["存货名称"].ToString() + "</td>"; html += @"<td>" + dr["数量"].ToString() + "</td>"; html += @"<td>" + dr["单位"].ToString() + "</td>"; html += @"<td>" + dr["到期日"].ToString() + "</td>"; html += "</tr>"; } html += "智合诚物料条码到期预警邮件通知,如有疑问,请联系我们。"; html += @"</tbody>
</table>";
html = html.Replace("'", "\"");
string isMail = System.Configuration.ConfigurationManager.AppSettings["IsMail"].ToString(); if (isMail == "0") { return; } else if (isMail == "1") { string send = System.Configuration.ConfigurationManager.AppSettings["Send"].ToString();//发送邮箱地址
string sendAccount = System.Configuration.ConfigurationManager.AppSettings["SendAccount"].ToString();//发送邮箱账号
string sendPassWord = System.Configuration.ConfigurationManager.AppSettings["SendPassWord"].ToString();//发送邮箱账号
string[] to = System.Configuration.ConfigurationManager.AppSettings["To"].ToString().Split(',');//接受邮箱地址
string host = System.Configuration.ConfigurationManager.AppSettings["Host"].ToString();//邮件服务器
string port = System.Configuration.ConfigurationManager.AppSettings["Port"].ToString();//端口 默认25
MailMessage message = new MailMessage();
message.From = new MailAddress(send);
foreach (string s in to) { message.To.Add(s); }
message.Subject = "物料条码保质期预警-" + DateTime.Now.ToString("yyyy-MM-dd"); message.Body = html; message.SubjectEncoding = System.Text.Encoding.UTF8; //邮件标题编码
message.BodyEncoding = System.Text.Encoding.UTF8; //邮件内容编码
message.IsBodyHtml = true; //是否是HTML邮件
message.Priority = MailPriority.Normal; //邮件优先级
SmtpClient client = new SmtpClient(); client.Credentials = new System.Net.NetworkCredential(sendAccount, sendPassWord); client.Port = int.Parse(port); client.Host = host; client.Send(message);
foreach (DataRow dr in dt.Rows) { if (i == 0) sql = @"update ICSWareHouseLotInfo set IsMail1=1 where LotNO='{0}' and WHCode='{1}' and BinCode='{2}'"; else sql = @"update ICSWareHouseLotInfo set IsMail2=1 where LotNO='{0}' and WHCode='{1}' and BinCode='{2}'";
sql = string.Format(sql, dr["条码"].ToString(), dr["仓库编码"].ToString(), dr["库位编码"].ToString()); DBHelper.ExecuteDataset(connstring, CommandType.Text, sql); } } else { return; } } } catch (Exception ex) { throw ex; } } } }
|