NOVOTS KMS 词汇表 Glossary    联系我们 Contact Us
查询 Search  
   
按类别浏览 Browse by Category
NOVOTS KMS .: 工作心得 .: Web 网站实现定期自动发送邮件

Web 网站实现定期自动发送邮件

httpmodule.cs文件

----------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Web.Hosting;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


namespace OnTimingTasks.Controllers
{
    public class HttpModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            var tasker = new CacheTimingTasker();
            tasker.RegistCacheTimmer();
        }
        public void Dispose()
        { }
    }
  
    public class CacheTimingTasker   
    {       
        const string LogPath = "~/log/log.txt";       
        const string CacheKeyName = "TimingTasker";//缓存的名字       
        const int ExpireTime = 600;//执行周期(秒) 
        const string rawUrl = "http://127.0.0.1:8060/";//缓存过期时请求的页面地址

        private static Cache WebCache = HttpRuntime.Cache;        /// <summary>      
    /// 当缓存不存在的时候则注册该缓存       
           
       public void RegistCacheTimmer()       
       {          
           if (WebCache[CacheKeyName] != null) return;          
           WebCache.Add(CacheKeyName, '1', null, DateTime.Now.AddSeconds(ExpireTime),  Cache.NoSlidingExpiration,                CacheItemPriority.High,                CacheItemOnRemoved);          
           WriteCacheLog("缓存时钟注册成功");      
       }       
        /// <summary>        /// 缓存过期的回调函数      
        /// /// </summary>      
        /// /// <param name="key">缓存的名字</param>     
        /// /// <param name="value">缓存的值</param>     
        /// /// <param name="reason">缓存原因</param>      
        protected void CacheItemOnRemoved(string key, object value, CacheItemRemovedReason reason)
        {          
            if (key == CacheKeyName)      
            {              
                ExecJob();        
                RegistCacheTimmer();     
            }    
        }
        protected void RequestWebPage()
        {
            System.Net.WebClient wc = new System.Net.WebClient();
            wc.DownloadData(rawUrl);         
        }

        protected void ExecJob()   
        {          
            //do sth in plan 
            data_m mydata = new data_m();
            WriteCacheLog("调用计划任务");
            RequestWebPage();
            int[] NotificationTime = mydata.GetNotificationTime();
            if (DateTime.Now.Day == NotificationTime[0] && DateTime.Now.Hour == NotificationTime[1] && (DateTime.Now.Minute < NotificationTime[2] + 10 && DateTime.Now.Minute >= NotificationTime[2]))
            {
                mydata.SendEmailByDate();

            }
        }     
        public static void WriteCacheLog(string logInfo)
        {         
            using (var sw = new StreamWriter(HostingEnvironment.MapPath(LogPath), true, Encoding.UTF8))       
            {              
                sw.WriteLine(String.Format("{0} {1}",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),logInfo));
            }     
        }  
    }
}

 web.config文件添加

<system.webServer>

<modules>

<add type="OnTimingTasks.Controllers.HttpModule" name="HttpModule" />

</modules>

</system.webServer>


这篇文章对你多有用?

相关文章

article 防范邮件病毒 禁止非法程序暗送邮件
   

(No rating)  5-15-2008    Views: 1462   
article 某用户修改别名后只能发送邮件,不能接收

(No rating)  6-1-2011    Views: 1801   
article Lync日常维护之三:客户端显示状态未更新的处理方法
删除如下本地缓存文件: windows...

(No rating)  5-23-2013    Views: 1102   

用户评语

添加评语
当前还没有评语.


.: .: .: .: .:
[ 登陆 ]
北京护航科技有限公司 2006

Novots Technologies Limited