打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c#web定时任务

转自:http://www.programbbs.com/doc/3003.htm 另外可参考http://developer.51cto.com/art/200909/149829.htm

想在Web中定时 处理某些任务吧?ITPUB个人空间T?b}N9W8_ e]w;`8B
可以在Global.asax 中定义,直接上例子:

<script runat="server">
    
void Application_Start(object sender, EventArgs e) 
    {
YN~MB DdY.w `#D0       
       
// 应用程序启动时
       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";
ww"l9BF ?$^0        
string s_body = "test";
        
string s_smtp = "mail.xxx.com";ITPUB个人空间!V @-K*DR xM Jr
        
int i = s_from.IndexOf("@");ITPUB个人空间XI@A L"?|
        
string s_subjetc = "test";
        
string username = s_from.Substring(0, i);
        System.Net.Mail.MailAddress from 
= new System.Net.Mail.MailAddress(s_from);ITPUB个人空间 dvT*IPZDs+I
        System.Net.Mail.MailAddress to 
= new System.Net.Mail.MailAddress(s_to);
        System.Net.Mail.MailMessage mailobj 
= new System.Net.Mail.MailMessage(from, to);
QE.~ gN0W[0        mailobj.Subject 
= s_subjetc;
        mailobj.Body 
= s_body;ITPUB个人空间Z FoC `'z LV

        mailobj.IsBodyHtml 
= true;
        mailobj.BodyEncoding 
= System.Text.Encoding.GetEncoding("GB2312");
        mailobj.Priority 
= System.Net.Mail.MailPriority.High;
u8KJ#M a0T ^0        System.Net.Mail.SmtpClient smtp 
= new System.Net.Mail.SmtpClient();
        smtp.Host 
= s_smtp;
        smtp.UseDefaultCredentials 
= true;ITPUB个人空间*SL m K Q f^}#G
        smtp.Credentials 
= new System.Net.NetworkCredential(username, pwd);
        smtp.Send(mailobj);
    }ITPUB个人空间 sP P+Is/E
</script>
ui?g"z4"5o9kAzmo!p0

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
System.Net.Mail
邮件发送
.net类库中发送电子邮件的方法总结
Unity发送邮件功能
JavaMail发送邮件和附件(源码)_2
收发Mail
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服