zoukankan      html  css  js  c++  java
  • 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>

    上面 是一个定时发邮件的小例子,一举两得,还给大家了一个发邮件的例子 ,呵呵
    建议最好是把需要启动的事项 做成 web Service 这样 应用范围会更广~

  • 相关阅读:
    前端 时间个性化 插件 jquery.timeago.js
    IP釋放、清除、以及刷新DNS
    Visual Studio 2019 16.0 正式版下载
    handlebars.js 用 <br>替换掉 内容的换行符
    c#-SimHash匹配相似-算法
    索尼Sony ATI显卡驱动 Win7 Win8 Win8.1 视频黑屏 解决方法
    c#-冒泡排序-算法
    c#-二分查找-算法
    c#-快速排序-算法
    win8.1中安装sql2014 0x800F0906 【 Error while enabling Windows feature : NetFx3, Error Code : -2146498298 】
  • 原文地址:https://www.cnblogs.com/ucetgg/p/1388481.html
Copyright © 2011-2022 走看看