ASP.NET 定时通知
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Security; using System.Web.SessionState; using System.Timers; using System.Net; using System.IO; using System.Text; using System.Threading;
using System.Data; namespace ECN.Net
{
public class Global : System.Web.HttpApplication
{ public static int times = * * * ; protected void Application_Start(object sender, EventArgs e)
{ //定义定时器 System.Timers.Timer myTimer = new System.Timers.Timer(times); myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed); myTimer.Enabled = true; myTimer.AutoReset = true; myTimer.Start(); } void myTimer_Elapsed(object source, ElapsedEventArgs e)
{ try
{ //Log.SaveNote(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":AutoTask is Working!"); YourTask(); } catch (Exception ee)
{ //Log.SaveException(ee); } } void YourTask()
{ //在这里写你需要执行的任务
string find_str = "SELECT * FROM EcnInfo ,EcnConfirm ";
find_str += " WHERE EcnInfo.EcnNo=EcnConfirm.EcnNo AND EcnConfirm.IfConfirm='False'";
DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.LocalSqlServer, find_str); for (int i = ; i < ds.Tables[].Rows.Count; i++)
{ string body = "<div><font face=\"Segoe UI\" size=\"2\" color=\"black\"><span style=\"font-size:10.5pt;\"><div><font face=\"方正舒体\" size=\"5\"><span style=\"font-size:29px;\">Hi:</span></font></div><div> <font face=\"方正舒体\" size=\"5\"><span style=\"font-size:29px;\"> </span></font><font face=\"方正舒体\" size=\"5\" color=\"red\"><span style=\"font-size:29px;\">您的ENC流还未处理,请及时处理!. ECN:" + ds.Tables[].Rows[i]["EcnNo"] + "</span></font><font face=\"方正舒体\" size=\"5\"><span style=\"font-size:29px;\"> </span></font><a href=\"http://10.68.100.250/ecn/\" target=\"_blank\"><font face=\"方正舒体\" size=\"5\"><span style=\"font-size:29px;\">http://10.68.100.250/ecn/</span></font></a><font face=\"方正舒体\" size=\"5\"><span style=\"font-size:29px;\"> </span></font></div><div><font face=\"Symbol\"> </font><font face=\"Symbol\"> </font><font face=\"Symbol\"> </font><font face=\"Symbol\"> </font><font face=\"Symbol\"> </font><font face=\"Symbol\"> </font><font face=\"Symbol\"> </font><font face=\"Symbol\"> </font></div><div><br></div><div> </div><div><font face=\"方正舒体\" size=\"5\"><span style=\"font-size:29px;\"> </span></font><font face=\"方正舒体\" size=\"5\"><span style=\"font-size:29px;\"> </span></font><font face=\"方正舒体\" size=\"5\"><span style=\"font-size:29px;\"> 请及时处理。</span></font></div><hr width=\"210\" size=\"1\" align=\"left\" color=\"#B5C4DF\" style=\"width:210px;\"><div><div style=\"margin:10px;\"><font size=\"5\"><span style=\"font-size:29px;\"><div><font face=\"方正舒体\">谢谢!</font></div>"; Public_CS.sendECNMail_No_Confirm(ds.Tables[].Rows[i]["EcnNo"].ToString(), ds.Tables[].Rows[i]["EcnName"].ToString(), body); } } protected void Application_End(object sender, EventArgs e)
{
// Log.SaveNote(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":Application End!"); //下面的代码是关键,可解决IIS应用程序池自动回收的问题 Thread.Sleep(); //这里设置你的web地址,可以随便指向你的任意一个aspx页面甚至不存在的页面,目的是要激发Application_Start string url = "default.aspx"; HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流
} protected void Session_Start(object sender, EventArgs e)
{ } protected void Application_BeginRequest(object sender, EventArgs e)
{ } protected void Application_AuthenticateRequest(object sender, EventArgs e)
{ } protected void Application_Error(object sender, EventArgs e)
{ } protected void Session_End(object sender, EventArgs e)
{ } }
}
using System;
using System.Data;
using System.Configuration;
//using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.Net.Mail;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Net; /// <summary>
///Public_CS 的摘要说明
/// </summary>
public class Public_CS
{
public Public_CS()
{
//
//TODO: 在此处添加构造函数逻辑
//
} public static string md5(string str, int code) //code 16 或 32
{
if (code == ) //16位MD5加密(取32位加密的9~25字符)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(, );
} if (code == ) //32位加密
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
} return "";
} #region /// <summary>
/// 发送邮件
/// </summary>
/// <param name="mailTo">要发送的邮箱</param>
/// <param name="mailSubject">邮箱主题</param>
/// <param name="mailContent">邮箱内容</param>
/// <returns>返回发送邮箱的结果</returns>
public static bool SendEmail(string mailTo, string mailSubject, string mailContent)
{
//简单邮件传输协议类
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = "mailtc.tc.gj.com";//邮件服务器
client.Port = ;//smtp主机上的端口号,默认是25.
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;//邮件发送方式:通过网络发送到SMTP服务器
client.Credentials = new System.Net.NetworkCredential("tc-ecn", "tc-ecn");//凭证,发件人登录邮箱的用户名和密码 //电子邮件信息类
System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress("xxx.com", "xxx");//发件人Email,在邮箱是这样显示的,[发件人:小明<[email protected]>;]
System.Net.Mail.MailAddress toAddress = new System.Net.Mail.MailAddress(mailTo, "xxx");//收件人Email,在邮箱是这样显示的, [收件人:小红<[email protected]>;]
System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(fromAddress, toAddress);//创建一个电子邮件类
mailMessage.Subject = mailSubject;
//string filePath = Server.MapPath("/index.html");//邮件的内容可以是一个html文本.
//System.IO.StreamReader read = new System.IO.StreamReader(filePath, System.Text.Encoding.GetEncoding("GB2312"));
//string mailBody = read.ReadToEnd();
//read.Close();
mailMessage.Body = mailContent;//可为html格式文本
//mailMessage.Body = "邮件的内容";//可为html格式文本
mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;//邮件主题编码
mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件内容编码
mailMessage.IsBodyHtml = true;//邮件内容是否为html格式
mailMessage.Priority = System.Net.Mail.MailPriority.High;//邮件的优先级,有三个值:高(在邮件主题前有一个红色感叹号,表示紧急),低(在邮件主题前有一个蓝色向下箭头,表示缓慢),正常(无显示).
try
{
client.Send(mailMessage);//发送邮件
//client.SendAsync(mailMessage, "ojb");异步方法发送邮件,不会阻塞线程.
}
catch (Exception)
{
return false;
} return true;
} public static void sendECNMail(string zubie, string head, string body)
{
string find_str = "SELECT * FROM UserInfo WHERE EcnGroup='" + zubie + "'"; DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.LocalSqlServer, find_str); int i = ds.Tables[].Rows.Count; if ( <= i)
{ for (int j = ; j < i; j++)
{
SendEmail(ds.Tables[].Rows[j]["Email"].ToString(), head, body);
} }
} public static void sendECNMail_No_Confirm(string ecn_no, string head, string body)
{
string find_str = "SELECT * FROM EcnConfirm WHERE EcnNo='" + ecn_no + "'"; DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.LocalSqlServer, find_str); int i = ds.Tables[].Rows.Count; if ( <= i)
{ for (int j = ; j < i; j++)
{
if (ds.Tables[].Rows[j]["PE1"].ToString() == "N")
{
Public_CS.sendECNMail("PE1", head, body);
}
if (ds.Tables[].Rows[j]["PE2"].ToString() == "N")
{
sendECNMail("PE2", head, body);
}
if (ds.Tables[].Rows[j]["PE3"].ToString() == "N")
{
sendECNMail("PE3", head, body);
}
if (ds.Tables[].Rows[j]["TE1"].ToString() == "N")
{
sendECNMail("TE1", head, body);
}
if (ds.Tables[].Rows[j]["TE2"].ToString() == "N")
{
sendECNMail("TE2", head, body);
}
if (ds.Tables[].Rows[j]["GZ1"].ToString() == "N")
{
sendECNMail("GZ1", head, body);
}
if (ds.Tables[].Rows[j]["GZ2"].ToString() == "N")
{
sendECNMail("GZ2", head, body);
}
if (ds.Tables[].Rows[j]["BC"].ToString() == "N")
{
sendECNMail("BC", head, body);
}
if (ds.Tables[].Rows[j]["ME"].ToString() == "N")
{
sendECNMail("ME", head, body);
}
} }
} public static void Change_Confirm_State(string ecn_no)
{
string find_str = "SELECT * FROM EcnConfirm WHERE EcnNo='" + ecn_no + "'"; DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.LocalSqlServer, find_str); int i = ds.Tables[].Rows.Count; if ( <= i)
{ for (int j = ; j < i; j++)
{
if (ds.Tables[].Rows[j]["PE1"].ToString() == "N")
{
return;
}
if (ds.Tables[].Rows[j]["PE2"].ToString() == "N")
{
return;
}
if (ds.Tables[].Rows[j]["PE3"].ToString() == "N")
{
return;
}
if (ds.Tables[].Rows[j]["TE1"].ToString() == "N")
{
return;
}
if (ds.Tables[].Rows[j]["TE2"].ToString() == "N")
{
return;
}
if (ds.Tables[].Rows[j]["GZ1"].ToString() == "N")
{
return;
}
if (ds.Tables[].Rows[j]["GZ2"].ToString() == "N")
{
return;
}
if (ds.Tables[].Rows[j]["BC"].ToString() == "N")
{
return;
}
if (ds.Tables[].Rows[j]["ME"].ToString() == "N")
{
return;
}
} } string sql_str_EcnConfirm = "UPDATE EcnConfirm SET IfConfirm= 'True' ,DateTime = '" + DateTime.Now.ToString("yyyy-MM-dd") + "' WHERE EcnNo='" + ecn_no + "'"; int row = SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, sql_str_EcnConfirm); } public static void Change_PoInfo_State(string ecn_no, string so)
{
string find_str = "UPDATE PoInfo SET EcnInfo='" + ecn_no + "' WHERE SO='" + so + "'";
DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.LocalSqlServer, find_str);
} public static bool ValidateServerCertificate(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
} public static bool SendEmail(string fromWhere, string Displayname, string toWhere, string subject, string body, string attach, string sPwd)
{
bool flag;
MailMessage message = new MailMessage();
message.From = new MailAddress(fromWhere, Displayname);
message.To.Add(new MailAddress(toWhere));
message.Subject = subject;
message.IsBodyHtml = true;
message.BodyEncoding = Encoding.UTF8;
message.Body = body;
message.Priority = MailPriority.High;
if (!string.IsNullOrEmpty(attach))
{
message.Attachments.Add(new Attachment(attach));
} ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate); SmtpClient client = new SmtpClient("xxx.xxx.com", );
client.Credentials = new NetworkCredential("xxx", sPwd, "xxx.com"); client.EnableSsl = true;
object obj2 = message;
try
{
client.Send(message);
flag = true;
}
catch (Exception exception)
{
throw exception;
}
return flag;
} #endregion }