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 ...
随机推荐
- Android SharedPreference最佳实践
Android提供多种方式保存应用数据,其中一种方式是SharedPreferences,使用键值对保存私有基本的数据.所有的逻辑仅基于以下三个类: SharedPreferences SharedP ...
- MZL's xor
MZL's xor Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other) Total Sub ...
- 纯代码 自己主动屏幕适配iPhone button
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2h1bmdlc2hpaHVhdGlhbg==/font/5a6L5L2T/fontsize/400/fil ...
- JavaScript之字符串引号的使用技巧
在JavaScript中可以随意使用引号,但是最好根据字符串包含的字符来选择. 1.如果字符串里面包含了单引号,那就把字符串放在双引号里面 var age = "this is 'pig'? ...
- poj2356 Find a multiple(抽屉原理|鸽巢原理)
/* 引用过来的 题意: 给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解, 随意输出一组即可.若不存在,输出 0. 题解: 首先必须声明的一点是本题是一定是有解的.原理根据 ...
- 【转载】设置event.cancelBubble,使触发子元素的onclick不同时触发父元素的onclick
由于HTML中的对象都是层次结构,比如一个Table包含了多个TR,一个TR包含了多个TD Bubble就是一个事件可以从子节点向父节点传递,比如鼠标点击了一个TD,当前的event.srcEleme ...
- 【转】 一个fork的面试题
转自:一个fork的面试题 前两天有人问了个关于Unix的fork()系统调用的面试题,这个题正好是我大约十年前找工作时某公司问我的一个题,我觉得比较有趣,写篇文章与大家分享一下.这个题是这样的: 题 ...
- java 无法割符日期字符串转yyyy-MM-dd hh:mm:ss
1.需要转换字符串(20150210125942),转化后要达到的目的:2015-02-10 12:59:42 public static void main(String[] args) { Str ...
- android--email发送邮件,文本还有附件形式的邮件
1.首先用的jar包为javaemail 下载地址: https://yunpan.cn/cB3kY8WIvcGtU (提取码:e042) 2.工具包 package com.kllayhello.u ...
- feof()和EOF的用法(转载)
查看 stdio.h 可以看到如下定义: #define EOF (-1) #define _IOEOF 0x0010 #define feof(_stream) ((_stream)-& ...