码迷,mamicode.com
首页 > Web开发 > 详细

.NET定时发送邮件

时间:2015-08-05 18:15:10      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

添加一个全局应用程序类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         }

 

.NET定时发送邮件

标签:

原文地址:http://www.cnblogs.com/bishibaotou/p/4705093.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!