星空网 > 软件开发 > ASP.net

.NET定时发送邮件

添加一个全局应用程序类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 事件。如果会话模式设置为 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 = "邮件主题";  
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 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     }

 




原标题:.NET定时发送邮件

关键词:.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

了解全球商标的概念与作用:https://www.kjdsnews.com/a/1359133.html
了解全球商标注册费用费用收取标准一览:https://www.kjdsnews.com/a/1359134.html
了解全球商标注册流程及全球注册商标网的优势:https://www.kjdsnews.com/a/1359135.html
跨境周报|TikTok面临新禁令,亚马逊卖家竞争加剧?:https://www.kjdsnews.com/a/1359136.html
了解商标国际注册的全部内容:https://www.kjdsnews.com/a/1359137.html
了解商标国际条约及其对企业的重要性:https://www.kjdsnews.com/a/1359138.html
83万单,Halara的TikTok美国小店爆单思路拆解:https://www.goluckyvip.com/news/186217.html
携程高铁旅游攻略,更优惠的高铁票价都在哪里:https://www.vstour.cn/a/362188.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流