香港那边公司的市场部开会时要求我们这边实现一个定时发送邮件的功能,即在每天下午5点左右定时把今天已通过三审的订单信息发给市场部的经理,告诉他哪些订单已经通过了终审。平时只知道如何用.net发送邮件,但不知如何定时发送邮件。于是百度了一下,总结起来有以下那么三种:

  (1)做一个winform 来定时发邮件。然后通过windows计划任务,设置为指定时间,每次自动运行,运行完毕后自动关闭。

  (2)用sqlserver 数据库实现发邮件,用sqlserver实现发邮件的存储过程,然后制定一个作业,制定时间运行。

  (3)在 Global.asax 文件里编程。事件:Application_Start。利用Time类编程。比如服务器1秒钟执行一次判断。

  香港那边的公司的ERP糸统是BS模式,由于对方公司服务器环境条件的限制,我和我师傅决定用第三种方法。在编程之前,先介绍一下Global.asax文件里的几个方法。

 protected void Application_Start(Object sender, EventArgs e) 
  { 
  //Application_start方法:请求 ASP.NET 应用程序中第一个资源(如页)时调用。在应用程序的生命周期期间仅调用一次 
  } 
  protected void Application_End(Object sender, EventArgs e) 
  { 
  //Application_end方法:在卸载应用程序之前对每个应用程序生命周期调用一次。 
  }

  下面是具体的做法:

  代码

  protected void Application_Start(Object sender, EventArgs e) 
  { 
  Timer t = new Timer(60000);//设计时间间隔,如果一个小时执行一次就改为3600000 ,这里一分钟调用一次 
  t.Elapsed += new ElapsedEventHandler(t_Elapsed); 
  t.AutoReset = true; 
  t.Enabled = true; 
  } 
  private void t_Elapsed(object sender, ElapsedEventArgs e) 
  { 
  if (GetEmailContent.GetMailContent().Length == 0) 
  { 
  return;//如果没有通过三审的订单要发送,则返回不发送邮件 
  } 
  int sendTime_Hour = Convert.ToInt32(ConfigurationManager.AppSettings["SendTime"].ToString());//假如是下午17:00分发送 
  int now_Hour = Convert.ToInt32(DateTime.Now.Hour.ToString()); 
  int now_Minute = Convert.ToInt32(DateTime.Now.Minute.ToString()); 
  int absolute = 1;//差距值,单位为分钟 
  if (((now_Hour == sendTime_Hour - 1) && (now_Minute >= 60 - absolute)) || ((now_Hour == sendTime_Hour) && (now_Minute <= absolute))) //即在如果时间判断是落在16:59分至17:01分之间,那么就会调用下面的邮件发送方法 
  { 
  string subject = string.Format("CO Approve Report({0})", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); 
  string host = ConfigurationManager.AppSettings["MailHost"]; 
  string from = ConfigurationManager.AppSettings["MailFrom"]; 
  string to = ConfigurationManager.AppSettings["MailTo"]; 
  string user = ConfigurationManager.AppSettings["MailUser"]; 
  string password = ConfigurationManager.AppSettings["MailPassword"]; 
  string content = GetEmailContent.GetMailContent(); 
  try 
  { 
  OrderMail.Send(host, user, password, to, from, subject, content, null);//发送邮件的方法,改为你自己的邮件发送方法 
  } 
  catch (Exception ex) 
  { 
  throw new Exception(ex.Message); 
  } 
  } 
  }

asp.net(C#)定时自动发送邮件

1

2 protected override void OnStart(string[] args)

3         {

4             MyTimer();

5         }

6

7         //实例化System.Timers.Timer

8         private void MyTimer()

9         {

10             //设置时间间隔

11             System.Timers.Timer MT = new System.Timers.Timer(int.Parse(ConfigResource.Interval)*60*1000);

12             MT.Elapsed += new System.Timers.ElapsedEventHandler(MTimedEvent);

13             MT.Enabled = true;

14

15         }

16         //构造System.Timers.Timer实例   间隔时间事件  (定时执行事件)

17         private void MTimedEvent(object source, System.Timers.ElapsedEventArgs e)

18         {

19             //开始工作

20             StartWork();

21         }

22

23         public void StartWork()

24         {

25              //从数据库DB查询表A中的时间   代码省略。。。

26                //时间比较

27                if()   //时间大于当前系统时间

28              {

29                   //发送邮件

30                     int iStatus = SendMail("你指定的收件人Email地址","标题","内容");

31                   if( iStatus > 0)

32                   {

33                        using (StreamWriter sw = new StreamWriter(filePath + "log.txt", System.Text.Encoding.GetEncoding("utf-8")))

34                          {

35                              sw.Wirte(System.DateTime.Now.ToString() + " 发送邮件成功!")

36                          }

37                   }

38                   else{//失败}

39              }

40         }

41

42 /// <summary>

43         /// 发送EMAIL

44         /// </summary>

45         /// <param name="sRecipientEmail">收件人地址</param>

46         /// <param name="sSubject">主题</param>

47         /// <param name="sMessage">内容</param>

48         /// <returns>发送是否成功</returns>

49         public bool SendMail(string sRecipientEmail, string sSubject, string sMessage)

50         {

51

52             //邮件对象

53             MailMessage emailMessage;

54

55             //smtp客户端对象

56

57             SmtpClient client;

58

59             // 初始化邮件对象

60

61             String sSenderEmail = "你的邮箱";

62

63             emailMessage = new MailMessage(sSenderEmail, sRecipientEmail, sSubject, sMessage);

64             emailMessage.IsBodyHtml = true;

65             emailMessage.SubjectEncoding = System.Text.Encoding.Default;

66             emailMessage.BodyEncoding = System.Text.Encoding.Default;

67             //加入

68             emailMessage.Headers.Add("X-Priority", "3");

69             emailMessage.Headers.Add("X-MSMail-Priority", "Normal");

70             emailMessage.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");

71             emailMessage.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869");

72

73             //邮件发送客户端

74             client = new SmtpClient();

75

76             //邮件服务器及帐户信息

77             client.Host = "邮件服务器";

78             //client.Host = "smtp.163.com";

79

80             //client.Port = 465;

81             //client.EnableSsl = true;

82             System.Net.NetworkCredential Credential = new System.Net.NetworkCredential();

83

84             Credential.UserName = "你的邮箱帐号"   //可以在资源文件中配置

85             Credential.Password = "密码"

86

87             client.Credentials = Credential;

88

89             try

90             {

91                 client.Send(emailMessage);

92             }

93             catch (Exception e)

94             {

95                 return false;

96             }

97             return true;

98

99         }

100

101

102

asp.net程序一般是当用户请求一个Page,或者请求一个WebService的时候,才会执行一段代码,如果我们希望让程序定时自动执行代码,但是又不增加新的应用程序,应该怎么做呢?

首先,给你的web应用程序,添加一个“Global.asax”文件,这个类里面默认有一个“Application_Start”,我们就在这个方法里面添加定时程序的逻辑代码。这样,只要有一个人访问了这个web应用,就会启动这个定时程序。

为了方便我们对定时程序的管理,我们单独编写一个类,专门用于控制定时程序。这个类中用的核心对象是System.Timers.Timer。下面说一下这个类设计的基本思路:ExecuteTask是一个公共的事件对象,以后调用者可以利用它来添加回调函数。_task是这个类型的唯一静态实例,调用者可以用Instance()方法读取它。_timer就是实现定时运行的对象,Interval是运行的间隔时间。

public class Time_Task
{
    public event System.Timers.ElapsedEventHandler ExecuteTask;

private static readonly Time_Task _task = null;
    private System.Timers.Timer _timer = null;
    private int _interval = 1000;

public int Interval
    {
        set
        {
            _interval = ;
        }
        get
        {
            return _interval;
        }
    }
    
    static Time_Task()
    {
        _task = new Time_Task();
    }
    
    public static Time_Task Instance()
    {
        return _task;
    }
    
    public void Start()
    {
        if(_timer == null)
        {
            _timer = new System.Timers.Timer(_interval);
            _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
            _timer.Enabled = true;
            _timer.Start();
        }
    }
    
    protected void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        if(null != ExecuteTask)
        {
            ExecuteTask(sender, e);
        }
    }
    
    public void Stop()
    {
        if(_timer != null)
        {
            _timer.Stop();
            _timer.Dispose();
            _timer = null;
        }
    }
}

有了这个类型,我们可以在Application_Start方法中轻松的实现定时了。

protected void Application_Start(object sender, EventArgs e)
{
    Time_Task.Instance().ExecuteTask += new System.Timers.ElapsedEventHandler(Global_ExecuteTask);
    Time_Task.Instance().Interval = 1000 * 60;//表示间隔1分钟
    Time_Task.Instance().Start();
}

void Global_ExecuteTask(object sender, System.Timers.ElapsedEventArgs e)
{
    //在这里编写需要定时执行的逻辑代码
}

需要注意的是,当你重新部署web应用的时候,比如更新了DLL文件,或者修改了web.config文件,就会中止这个定时程序,需要有人再次请求Page,才能把它启动起来。

定时执行。。。。在webform中实现真的有点别扭,会因各种因素而停止执行,比如

1:web服务停止了一段时间内没人访问;
2:莫名其妙的不执行;
3:……

所以,95%有经验的人会选择使用webservice+客户端的方式来实现。
而我这里介绍的偏偏是使用纯Global.asax实现。

前提,我要实现的是定时处理数据库里的过期信息,比如业务员跟踪的业务到期释放、一个月未登录的用户自动锁定等等。
备注:要是需要实现定时对外发信之类的功能就不合适了,为什么呢?因为所谓定时收信的时间参照是外界的,即便你的站点"死"了,时间依然在走,那么此时一旦到了时间而你的站点处理“死”的状态,必然就收不到信了。而处理系统内的业务数据就不一样了,假设你的站点是“死”的,又何来业务?有业务的前提是:站点是正常的。说到这你明白了一些吗?

扯远了,下面就说说具体步骤吧:

第一步,打开Web.config,在appSettings下新建
这个value自己随时可以改,主要是控制任务执行的权限

第二步,新建Global.asax,打开Global.asax.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Timers;

namespace CGWCS.WebFile
{
    public class Global : System.Web.HttpApplication
    {
        public static HttpContext myContext = HttpContext.Current;

protected void Application_Start(object sender, EventArgs e)
        {
            System.Timers.Timer aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new ElapsedEventHandler(TimeEvent);
            aTimer.Interval = 1000;// 设置引发时间的时间间隔 此处设置为1秒
            aTimer.Enabled = true;
        }
        private void TimeEvent(object source, ElapsedEventArgs e)
        {
            // 得到 hour minute second 如果等于某个值就开始执行某个程序。
            int intHour = e.SignalTime.Hour;
            int intMinute = e.SignalTime.Minute;
            int intSecond = e.SignalTime.Second;
            // 设置 每天的7:30:00开始执行程序
            //假设web服务异常或正常的重启,则强行执行一次任务(目的是为防止遗漏)
            if ((intHour == 7 && intMinute == 30 && intSecond == 0) || myContext.Application["AutoTask_1"] == null)
            {
                string _url = ServerUrl() + "/autotask.aspx?oper=Release1&password=" + System.Configuration.ConfigurationManager.AppSettings["AutoTask:Password"];
                System.IO.StreamWriter sw = new System.IO.StreamWriter(myContext.Request.PhysicalApplicationPath + "\\log.txt", true, System.Text.Encoding.UTF8);
                sw.WriteLine(e.SignalTime.ToString());
                sw.WriteLine(GetHttpPage(_url));
                sw.Close();
                sw.Dispose();
                myContext.Application.Lock();
                myContext.Application["AutoTask_1"] = "true";
                myContext.Application.UnLock();
            }
            // 设置 每天的7:45:00开始执行程序
            //假设web服务异常或正常的重启,则强行执行一次任务(目的是为防止遗漏)
            if ((intHour == 7 && intMinute == 45 && intSecond == 0) || myContext.Application["AutoTask_2"] == null)
            {
                string _url = ServerUrl() + "/autotask.aspx?oper=Release2&password=" + System.Configuration.ConfigurationManager.AppSettings["AutoTask:Password"];
                System.IO.StreamWriter sw = new System.IO.StreamWriter(myContext.Request.PhysicalApplicationPath + "\\log.txt", true, System.Text.Encoding.UTF8);
                sw.WriteLine(e.SignalTime.ToString());
                sw.WriteLine(GetHttpPage(_url));
                sw.Close();
                sw.Dispose();
                myContext.Application.Lock();
                myContext.Application["AutoTask_2"] = "true";
                myContext.Application.UnLock();
            }
        }
        private string ServerUrl()
        {
            if (myContext.Request.Url.Port == 80)
                return "http://" + myContext.Request.Url.Host;
            else
                return "http://" + myContext.Request.Url.Host + ":" + myContext.Request.Url.Port;
        }
        private string AppPath()
        {
            string _ApplicationPath = myContext.Request.ApplicationPath;
            if (_ApplicationPath != "/")
                _ApplicationPath += "/";
            return _ApplicationPath;
        }
        private string GetHttpPage(string url)
        {
            string strResult = string.Empty;
            try
            {
                System.Net.WebClient MyWebClient = new System.Net.WebClient();
                MyWebClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
                MyWebClient.Encoding = System.Text.Encoding.UTF8;
                strResult = MyWebClient.DownloadString(url);
            }
            catch (Exception)
            {
                strResult = "页面获取失败";
            }
            return strResult;
        }
        protected void Session_Start(object sender, EventArgs e)
        {

}

protected void Application_BeginRequest(object sender, EventArgs e)
        {

}

protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

}

protected void Application_Error(object sender, EventArgs e)
        {

}

protected void Session_End(object sender, EventArgs e)
        {

}

protected void Application_End(object sender, EventArgs e)
        {

}
    }
}

第三步:新建一个autotask.aspx,打开autotask.aspx.cs

using System;
using System.Data;
using System.Web;
namespace CGWCS.WebFile
{
    public partial class _autotask : CGWCS.UI.BasicPage
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            this._operType = q("oper");
            switch (this._operType)
            {
                case "Release1":
                    Release1();
                    break;
                case "Release2":
                    Release2();
                    break;
                default:
                    DefaultResponse();
                    break;
            }
            Response.Write(this._response);
        }
        private void DefaultResponse()
        {
            this._response = "未知操作";
        }
        private void Release1()
        {
            string _password = q("password");
            if (_password != System.Configuration.ConfigurationManager.AppSettings["AutoTask:Password"])
            {
                this._response = "密码错误";
                return;
            }
            ....这是释放代码

int _doCount = 释放的个数;
            if (_doCount > 0)
            {
                this._response = "成功释放" + _doCount + "个锁定的企业";
            }
            else
                this._response = "没有可以释放的锁定的企业";
        }
        private void Release2()
        {
            string _password = q("password");
            if (_password != System.Configuration.ConfigurationManager.AppSettings["AutoTask:Password"])
            {
                this._response = "密码错误";
                return;
            }
            ....这是锁定代码

int _doCount = 锁定的用户数;
            if (_doCount > 0)
            {
                this._response = "成功锁定" + _doCount + "个用户";
            }
            else
                this._response = "没有可以锁定的用户";
        }
    }
}

最后说明一下,虽然编译通过,但是在Global.asax.cs里有些代码是不生效的,比如:

1、不能直接使用HttpContext.Current,需要先定义一个静态值。
2、Request.ServerVariables["Server_Port"].ToString()  和 Request.Url.Port 同时表示当前web服务端口号,但是只能用后者而不能用前者.

C# asp.net的定时发送相关推荐

  1. 全网最全的Windows下Anaconda2 / Anaconda3里Python语言实现定时发送微信消息给好友或群里(图文详解)...

    不多说,直接上干货! 缘由: (1)最近看到情侣零点送祝福,感觉还是很浪漫的事情,相信有很多人熬夜为了给爱的人送上零点祝福,但是有时等着等着就睡着了或者时间并不是卡的那么准就有点强迫症了,这是也许程序 ...

  2. pwd python 安装 模块_Python自动化办公之邮件定时发送

    Python自动化之邮件的定时发送,我们这里使用网易163邮箱. Python邮件操作相关模块的安装 yagmail,The goal here is to make it as simple and ...

  3. android notification 定时显示,Android编程使用Service实现Notification定时发送功能示例...

    本文实例讲述了android编程使用service实现notification定时发送功能.分享给大家供大家参考,具体如下: /** * 通过启动或停止服务来管理通知功能 * * @descripti ...

  4. EJB3.0定时发送jms(发布/定阅)方式

    EJB3.0定时发送jms(发布/定阅)方式 介绍: 定时器分为两种:single-action Timer(单动定时器)和interval  Timer(间隔定时器). 为了使用定时服务,1.ent ...

  5. 安卓能帮你检查密码泄露了!还有Messages定时发送等多项功能优化

    子豪 发自 凹非寺 量子位 报道 | 公众号 QbitAI 密码泄露,现在能在登陆之前收到提醒了! 再也不用熬夜给异国好友发消息了-- 这样的变化,都在谷歌最新公布的Android新功能中. 还有谷歌 ...

  6. 定时发送服务器运行数据并设置阀值警报方法

    案例分析:笔者架设了公司整体的服务器系统监控系统,实现运行数据收集,筛选,报警的机制,采用了windows系统自带的性能监视器,但是在实施的过程中,遇到了一个问题:性能监视器无法对sql server ...

  7. 在 Lotus Notes 中设置邮件定时发送的方法及代理功能介绍

    孙 茂伟, 测试工程师, IBM 孙茂伟在北京科技大学获得工学学士.工学硕士学位,现于 IBM CDL ECM FileNet EForms TEAM 从事自动化测试相关工作. 简介: 本文将向广大 ...

  8. android udp定时发送,Android Socket基于UDP协议通信

    首先我们要知道UDP通信协议是Socket通信的一种实现方式,Socket通信一般有两种通信方式:基于TCP协议.基于UDP协议.这两者的差别和优缺点就不说了,这里主要讲一下基于UDP协议的实现. 基 ...

  9. 网易邮箱大师如何定时发送 定时发送邮件方法步骤详解

    网易邮箱大师是我们日常使用邮箱的最佳软件,不仅能批量登录邮件,还能定时发送邮件,可谓功能齐全,很多小伙伴不知道如何定时发送邮件,那么接下来小编说的这篇文章肯定会对你有帮助. 操作步骤如下: 1.打开网 ...

最新文章

  1. 谷歌为什么把几十亿行代码放在一个库?
  2. MySql轻松入门系列——第一站 从源码角度轻松认识mysql整体框架图
  3. 设计模式 C++装饰模式
  4. 【数据库系统设计】关系数据理论(函数依赖、码、范式、模式分解)
  5. java private list_Java基础知识回顾之四 ----- 集合List、Map和Set
  6. 量产软件测试培训,U盘量产及在虚拟机中测试
  7. 免费获取ps密钥_ps密钥
  8. 新数据时代,浪潮存储如何革故鼎“新”
  9. 什么是jquery?简单的jquery代码
  10. Your Freedom — 跨平台的代理软件
  11. 美国计算机专业修什么课程,美国大学计算机专业课程有4大特点
  12. 屏幕后期特效——Blood(角色死亡闪血)
  13. Stay Hungry, Stay Foolish — 求知若饥,虚心若愚!
  14. 115家电子科技企业待遇一览
  15. 如何自己进行论文投稿
  16. ERP软件费用包括哪些?总共多少钱
  17. 如何采用FM进行召回
  18. RGB颜色空间和CIELab颜色空间互换(matlab代码)
  19. 增强现实(AR:Augmented Reality ) 之介绍及应用
  20. Ancient Go

热门文章

  1. ROS机器人DIY教程:超声波数据获取(HC-SR04/US-100)
  2. 2010QG优秀学员总结——李世宏
  3. 医学图像处理:读取mhd文件
  4. 带你读懂Spring的事务传播行为
  5. Python带你朗读网页
  6. layui让当前页面刷新_layui怎么刷新当前页面?
  7. 读《文艺复兴三杰,米开朗基罗》
  8. TensorFlow:误差计算
  9. python response_教你用 Python 修改微信(支付宝)运动步数,轻松升到 TOP1
  10. 系列案例解读丨“欲做非洲第一电商”,Kilimall “黑五”大促的自动化营销实践解读...