添加一个全局应用程序类Global.asax代码会在访问网站时运行Global.asax代码: 1 void Application_Start(object sender, EventArgs e) 2 { 3 // 在应用程序启动时运行的代码 4 Sy ...
添加一个全局应用程序类Global.asax
代码会在访问网站时运行
Global.asax代码:
1 void Application_Start(object sender, EventArgs e) 2 { 3 // 在应用程序启动时运行的代码 4 System.Timers.Timer timer = new System.Timers.Timer(3600000);//设计时间间隔,如果一个小时执行一次就改为3600000 5 timer.Elapsed += new System.Timers.ElapsedEventHandler(Send); 6 timer.AutoReset = true; 7 timer.Enabled = true; 8 } 9 10 void Application_End(object sender, EventArgs e)11 {12 // 在应用程序关闭时运行的代码13 System.Threading.Thread.Sleep(5000);14 get='_blank'>string strUrl = "服务器地址";15 System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);16 System.Net.HttpWebResponse _HttpWebResponse = (System.Net.HttpWebResponse)_HttpWebRequest.GetResponse();17 System.IO.Stream _Stream = _HttpWebResponse.GetResponseStream();//得到回写的字节流18 _HttpWebResponse.Close();19 }20 21 void Application_Error(object sender, EventArgs e)22 {23 // 在出现未处理的错误时运行的代码24 25 }26 27 void Session_Start(object sender, EventArgs e)28 {29 // 在新会话启动时运行的代码30 31 }32 33 void Session_End(object sender, EventArgs e)34 {35 // 在会话结束时运行的代码。 36 // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为37 // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer38 // 或 SQLServer,则不引发该事件。39 40 }41 42 private void Send(object sender, System.Timers.ElapsedEventArgs e)43 {44 switch (DateTime.Now.Hour)45 {46 case 0:47 case 24:48 SendEMail();49 break;50 //default:51 // SendEMail();52 // break;53 }54 }55 private void SendEMail()56 {57 string mailFrom = System.Configuration.ConfigurationManager.AppSettings["MailFrom"].ToString();58 string mailUser = System.Configuration.ConfigurationManager.AppSettings["MailUser"].ToString();59 string mailPassword = System.Configuration.ConfigurationManager.AppSettings["MailPassword"].ToString();60 string hostIP = System.Configuration.ConfigurationManager.AppSettings["MailHost"].ToString();61 62 List<string> mailAddress = new List<string>();63 string mailSubjct = "邮件主题";
原标题:.NET定时发送邮件
关键词:.NET
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。