添加一個全局應用程序類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 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 事件。如果會話模式設置為 StateServer
38 // 或 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 = "郵件主題";
string mailBody = "郵件內容:";
mailAddress.Add("郵件地址");string strReturn = sendMail(mailSubjct, mailBody, mailFrom, mailAddress, hostIP, mailUser, mailPassword, false);
64 }
sendMail方法
1 public static string sendMail(string mailSubjct, string mailBody, string mailFrom, List<string> mailAddress, string hostIP, string username, string password, bool ssl)
2 {
3 string str = "";
4 try
5 {
6 MailMessage message = new MailMessage
7 {
8 IsBodyHtml = true,
9 Subject = mailSubjct,
10 Body = mailBody,
11
12 From = new MailAddress(mailFrom)
13 };
14 for (int i = 0; i < mailAddress.Count; i++)
15 {
16 message.To.Add(mailAddress[i]);
17 }
18 SmtpClient client = new SmtpClient
19 {
20 EnableSsl = ssl,
21 UseDefaultCredentials = false
22 };
23 NetworkCredential credential = new NetworkCredential(username, password);
24 client.Credentials = credential;
25 client.DeliveryMethod = SmtpDeliveryMethod.Network;
26 client.Host = hostIP;
27 client.Port = 0x19;
28 client.Send(message);
29 }
30 catch (Exception exception)
31 {
32 str = exception.Message;
33 }
34 return str;
35 }