Web定時任務
想在Web中定時 處理某些任務吧?
可以在Global.asax 中定義,直接上例子:
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// 應用程式啟動時
System.Timers.Timer timer = new System.Timers.Timer();
DateTime sTime = DateTime.Now;
int sHour = sTime.Hour;
int sMin = sTime.Minute;
int sSecond = sTime.Second;
int sHourInterval = 15 - sHour;
int sSecondInterval = sHourInterval * 60 * 60 * 1000;
//timer.Interval = 1000*5;//5秒傳送一次
timer.Interval = sSecondInterval;
//timer.AutoReset;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
string s_from = "fengsheng@xxx.com";
string s_to = "fengsheng@xxx.com";
string pwd = "1234567";
string s_body = "test";
string s_smtp = "mail.xxx.com";
int i = s_from.IndexOf("@");
string s_subjetc = "test";
string username = s_from.Substring(0, i);
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(s_from);
System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress(s_to);
System.Net.Mail.MailMessage mailobj = new System.Net.Mail.MailMessage(from, to);
mailobj.Subject = s_subjetc;
mailobj.Body = s_body;
mailobj.IsBodyHtml = true;
mailobj.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
mailobj.Priority = System.Net.Mail.MailPriority.High;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.Host = s_smtp;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new System.Net.NetworkCredential(username, pwd);
smtp.Send(mailobj);
}
script>
void Application_Start(object sender, EventArgs e)
{
// 應用程式啟動時
System.Timers.Timer timer = new System.Timers.Timer();
DateTime sTime = DateTime.Now;
int sHour = sTime.Hour;
int sMin = sTime.Minute;
int sSecond = sTime.Second;
int sHourInterval = 15 - sHour;
int sSecondInterval = sHourInterval * 60 * 60 * 1000;
//timer.Interval = 1000*5;//5秒傳送一次
timer.Interval = sSecondInterval;
//timer.AutoReset;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
string s_from = "fengsheng@xxx.com";
string s_to = "fengsheng@xxx.com";
string pwd = "1234567";
string s_body = "test";
string s_smtp = "mail.xxx.com";
int i = s_from.IndexOf("@");
string s_subjetc = "test";
string username = s_from.Substring(0, i);
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(s_from);
System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress(s_to);
System.Net.Mail.MailMessage mailobj = new System.Net.Mail.MailMessage(from, to);
mailobj.Subject = s_subjetc;
mailobj.Body = s_body;
mailobj.IsBodyHtml = true;
mailobj.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
mailobj.Priority = System.Net.Mail.MailPriority.High;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.Host = s_smtp;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new System.Net.NetworkCredential(username, pwd);
smtp.Send(mailobj);
}
script>
上面 是一個定時發郵件的小例子,一舉兩得,還給大家了一個發郵件的例子 ,呵呵
建議最好是把需要啟動的事項 做成 web Service 這樣 應用範圍會更廣~
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-548885/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- java web定時任務JavaWeb
- 普通web整合quartz跑定時任務Webquartz
- java web每天定時執行任務JavaWeb
- 定時任務
- 定時任務scheduler
- At 、Crontabl定時任務
- crontab定時任務
- 定時任務管理
- ubuntu定時任務Ubuntu
- schedule 定時任務
- Oracle定時任務Oracle
- laravel定時任務Laravel
- Navicat定時任務
- Java 定時任務Java
- @Scheduled 定時任務
- Js定時任務JS
- mysql 定時任務MySql
- 定時任務操作
- 定時任務crond服務
- quartz定時任務時間設定quartz
- Golang——Cron 定時任務Golang
- Linux | 定時任務Linux
- Linux 定時任務Linux
- 石英定時任務-quartzquartz
- Spring 定時任務Spring
- Linux at 定時任務Linux
- CentOS Crontab(定時任務)CentOS
- Linux定時任務Linux
- 記mysql定時任務MySql
- 定時任務總覽
- 定時任務技術
- SpringTask定時任務Spring
- Java & Go 定時任務JavaGo
- 通過定時器、時間分片、Web Worker優化長任務定時器Web優化
- Linux系統中延時任務及定時任務Linux
- laravel框架任務排程(定時執行任務)Laravel框架
- SpringBoot與非同步任務、定時任務、郵件任務Spring Boot非同步
- 定時任務管理系統