ASP.NET 定时通知
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,在邮箱是这样显示的,[发件人:小明<panthervic@163.com>;]
System.Net.Mail.MailAddress toAddress = new System.Net.Mail.MailAddress(mailTo, "xxx");//收件人Email,在邮箱是这样显示的, [收件人:小红<43327681@163.com>;]
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 }
ASP.NET 定时通知的更多相关文章
- Asp.net 定时写入文本记录
Asp.net 定时写入文本记录 public static string FileAddress = "c:\\TimerLog.txt"; protected void Pag ...
- android 后台服务定时通知
最近有个项目的要求是在程序退出之后,任然可以每天定时发通知,我们可以想下,其实就是后台开一个服务,然后时间到了就发下通知. 1.首先我们需要用到Service类. 先上代码在慢慢解释 package ...
- android本地定时通知
android本地通知略有不同,分为立即触发和延时触发 1.即时通知 android默认的Notification为立即触发 Intent intent = new Intent(Intent.ACT ...
- asp 自我定时删除
<% if now()>"2008-9-15" thenset myfso=server.CreateObject("scripting.filesystem ...
- ASP.NET定时调用WebService 运行后台代码
效果: 通过在网站的Global.asax的Application_Start方法中 加入定时器 定时调用WebService 该WebService的一个方法 负责在后台 向数据库的某个表加入数据 ...
- asp.net 定时间点执行任务的简易解决办法
这里的定时间点执行任务,指的是每天的某个时间执行一项任务. 比如每天凌晨七点的时候email发送一次报表. 这里首先想到的就是利用 Global.asax 文件来实现, 以下Global文件的内容. ...
- springboot使用redis的keyspace notifications 实现定时通知
简单定时任务解决方案:使用redis的keyspace notifications(键失效后通知事件) 需要注意此功能是在redis 2.8版本以后推出的,因此你服务器上的reids最少要是2.8版本 ...
- asp.net 定时执行任务代码 定时采集数据
using System; using System.Data; using System.Configuration; using System.Collections; using System. ...
- 基于ASP.NET MVC(C#)和Quartz.Net组件实现的定时执行任务调度
http://www.cnblogs.com/bobositlife/p/aspnet-mvc-csharp-quartz-net-timer-task-scheduler.html 在之前的文章&l ...
随机推荐
- 经常使用的时间同步server地址
转载出处http://www.minunix.com/2013/03/ntpserver/ 感谢原作者,一切的权利都属于原作者,假设有所不适,我会马上删除 中国大概能用的NTP时间server地址 s ...
- 学习pthreads,给线程传递多个參数
上篇博文中.boss线程给其它线程传递的仅仅有一个參数,那么假如是多个參数呢?怎么传递呢?也许你会有这种疑问,带着这个疑问,我们进入本文的世界,这里传递多个參数,採用结构体,为什么呢?由于结构体里能够 ...
- HDU1506 ( Largest Rectangle in a Histogram ) [dp]
近期情绪太不稳定了.可能是由于在找实习这个过程碰壁了吧.第一次面试就跪了,可能是我面的是一个新公司,制度不完好,我感觉整个面试过程全然不沾编程,我面试的还是软件开发-后来我同学面试的时候.说是有一道数 ...
- php 求水仙花数优化
水仙花数是指一个n位数(n>=3),它每一个位上数字的n次幂之和等于它本身,n为它的位数.(比如:1^3+5^3+3^3 = 153) 水仙花数又称阿姆斯特朗数. 三位的水仙花数有4个:153, ...
- android http同步请求
1.界面 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...
- 使用Notepad++快速有效删除复制代码中的行号
转载:http://plum.0602.blog.163.com/blog/static/1130006502011101524120757/ 试了该方法,很好用! 为什么我把用Notepad++删除 ...
- 【转】CoreData以及MagicalRecord (二)
3. 运行时类与对象 NSManagedObject Managed Object 表示数据文件中的一条记录,每一个Managed Object在内存中对应的实体(Entity)的一个数据表示.Man ...
- jQuery相关面试题
1 你在公司是怎么用jquery的? 答:在项目中是怎么用的是看看你有没有项目经验(根据自己的实际情况来回答) 你用过的选择器啊,复选框啊,表单啊,ajax啊,事件等 配置Jquery环境 下载jqu ...
- 在mac系统安装Apache Tomcat的详细步骤(转载自himi的博客,修改了错误添加了图片)
链接地址:http://blog.csdn.net/liuyuyefz/article/details/8072485 1. 2. 3. 4. 5. 对于Apache Tomcat 估计很多童鞋都会, ...
- POJ 3376 Finding Palindromes(扩展kmp+trie)
题目链接:http://poj.org/problem?id=3376 题意:给你n个字符串m1.m2.m3...mn 求S = mimj(1=<i,j<=n)是回文串的数量 思路:我们考 ...