. NET regularly sends emails, and. NET sends emails
Add a Global application class Global. asax
The code will run when accessing the website
Global. asax code:
1 void Application_Start (object sender, EventArgs e) 2 {3 // code 4 System when the application starts. timers. timer timer = new System. timers. timer (3600000); // The design interval. If one hour is executed, it is changed to 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 // code 13 System when the application is closed. threading. thread. sleep (5000); 14 string strUrl = "server address"; 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 (); // get the write-back byte stream 18 _ HttpWebResponse. close (); 19} 20 21 void Application_Error (object sender, E VentArgs e) 22 {23 // code 24 25} 26 27 void Session_Start (object sender, EventArgs e) that runs when an unhandled error occurs) 28 {29 // code 30 31} 32 33 void Session_End (object sender, EventArgs e) 34 {35 // code to run when the session ends. 36 // Note: The Session_End event is triggered only when the sessionstate mode in the Web. config file is set to 37 // InProc. If the session mode is set to StateServer38 // or SQLServer, this event is not triggered. 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. deleetask[ "MailPassword"]. toString (); 60 string hostIP = System. configuration. configurationManager. appSettings ["MailHost"]. toString (); 61 62 List <string> mailAddress = new List <string> (); 63 string mailSubjct = "Email Subject ";
String mailBody = "email content :";
MailAddress. Add ("email address"); string strReturn = sendMail (mailSubjct, mailBody, mailFrom, mailAddress, hostIP, mailUser, mailPassword, false); 64}
SendMail Method
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 SmtpClient19 {20 EnableSsl = ssl,21 UseDefaultCredentials = false22 };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 }