c#定时器 每天晚上12点发邮件 如何实现

迷失道 2014-03-28 01:56:04
c#定时器 每天晚上12点发邮件 如何实现
现在我用的是Timer但不太会用,这个东西好像隔多长时间发一次,不能定时到某个点。也就是我想要他每天12点解决发邮件的事件。
...全文
6439 41 打赏 收藏 转发到动态 举报
写回复
用AI写文章
41 条回复
切换为时间正序
请发表友善的回复…
发表回复
CallMe丶学渣 2016-12-13
  • 打赏
  • 举报
回复
引用 7 楼 zhanglong_longlong 的回复:
window服务:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;

namespace WindowsService2
{
public partial class Service1 : ServiceBase
{
System.Timers.Timer timer1; //计时器
WindowTaske w = new WindowTaske();
public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
timer1 = new System.Timers.Timer();

timer1.Interval = 3000; //设置计时器事件间隔执行时间

timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);

timer1.Enabled = true;

if (!EventLog.SourceExists("OnStart222"))
{
EventLog.CreateEventSource("OnStart222", "jason");
}

EventLog.WriteEntry("OnStart222", "开始任务了");
}

protected override void OnStop()
{
this.timer1.Enabled = false;
EventLog.WriteEntry("OnStart222", "任务结束");
}

private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (e.SignalTime.Hour == 0 && e.SignalTime.Minute == 0 && e.SignalTime.Second == 0)//晚上12点发送邮件
{
//发送邮件方法
}
}


}
}


把timer的执行间隔时间改成1000吧
要不然假设我23.59.58秒开启服务,当天晚上的0点会发邮件么?
lutaotony 2014-04-01
  • 打赏
  • 举报
回复
如果lz可以用sqlserver ,就用sqlserver里面的作业,还是很不错的哦
likevs 2014-04-01
  • 打赏
  • 举报
回复
引用 33 楼 diaodiaop 的回复:
... 如果人家是虚拟机 你怎么写服务? 连个远程都没有 只有空间呢? 所以 我只想问 为什么没有人用SQL作业呢? 新建一个作业 晚上执行存储过程 你用存储过程请求webservice也好 请求ashx也罢 甚至 你可以本身就用sql发送,,这么多的选择 而且 效率比所谓的timer高了很多吧.. 为什么没人选用呢?
你这种太高端了,没玩过
minhua1983 2014-03-31
  • 打赏
  • 举报
回复
写WINDOWS服务吧,虽然在ASP.NET也可以在Global.asax的Application_Start方法中加Timer,然后用timer.elapsed来执行逻辑,不过还是不建议在ASP.NET用。
迷失道 2014-03-31
  • 打赏
  • 举报
回复
用windows服务,服务器不是我们的,可能要增加费用。
帅得_被人砍 2014-03-31
  • 打赏
  • 举报
回复
我是用WINDOWS 服务来实现的,每天按时发邮件
Viccy_Yao 2014-03-31
  • 打赏
  • 举报
回复
方法有很多,就看你用哪一种了。首先推荐的是windows服务,windows服务了里面是可以用Timmer类的。 也可以写个普通的发送程序,用任务计划去调度。 也可以自己在服务里面写调度计划。看哪一种适合你了。
cainiao13579 2014-03-31
  • 打赏
  • 举报
回复
把它做成服务的吧
IT0329 2014-03-31
  • 打赏
  • 举报
回复
叫我 Teacher 周 2014-03-31
  • 打赏
  • 举报
回复
这个用windows service吧
骑猪看海 2014-03-31
  • 打赏
  • 举报
回复
引用 24 楼 sum331425333 的回复:
只是为了12点发封邮件用Windows Service真心觉得太浪费了,而且用Timer并不能保证一定能发出去,判断条件是小时分钟和秒都为0,这对你的启动时间有要求, 因为你的间隔是3秒,假设你23:59:59秒启动,就发不出去了。所以个人觉得,应该用任务计划,或者SQL Server的JOB。关于用SQL server发邮件请自行百度sp_send_dbmail [quote=引用 21 楼 chz415767975 的回复:] [quote=引用 7 楼 zhanglong_longlong 的回复:] window服务:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;

namespace WindowsService2
{
    public partial class Service1 : ServiceBase
    {
        System.Timers.Timer timer1;  //计时器
        WindowTaske w = new WindowTaske();
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            timer1 = new System.Timers.Timer();

            timer1.Interval = 3000;  //设置计时器事件间隔执行时间

            timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);

            timer1.Enabled = true;

            if (!EventLog.SourceExists("OnStart222"))
            {
                EventLog.CreateEventSource("OnStart222", "jason");
            }

            EventLog.WriteEntry("OnStart222", "开始任务了");
        }

        protected override void OnStop()
        {
            this.timer1.Enabled = false;
            EventLog.WriteEntry("OnStart222", "任务结束");
        }

        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (e.SignalTime.Hour == 0 && e.SignalTime.Minute == 0 && e.SignalTime.Second == 0)//晚上12点发送邮件
            { 
                //发送邮件方法
            }
        }

        
    }
}
正解[/quote][/quote] 都行,反正不要在WEB项目里写就OK,否则就死翘翘了
by_封爱 2014-03-31
  • 打赏
  • 举报
回复
... 如果人家是虚拟机 你怎么写服务? 连个远程都没有 只有空间呢? 所以 我只想问 为什么没有人用SQL作业呢? 新建一个作业 晚上执行存储过程 你用存储过程请求webservice也好 请求ashx也罢 甚至 你可以本身就用sql发送,,这么多的选择 而且 效率比所谓的timer高了很多吧.. 为什么没人选用呢?
sum331425333 2014-03-30
  • 打赏
  • 举报
回复
只是为了12点发封邮件用Windows Service真心觉得太浪费了,而且用Timer并不能保证一定能发出去,判断条件是小时分钟和秒都为0,这对你的启动时间有要求, 因为你的间隔是3秒,假设你23:59:59秒启动,就发不出去了。所以个人觉得,应该用任务计划,或者SQL Server的JOB。关于用SQL server发邮件请自行百度sp_send_dbmail
引用 21 楼 chz415767975 的回复:
[quote=引用 7 楼 zhanglong_longlong 的回复:] window服务:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;

namespace WindowsService2
{
    public partial class Service1 : ServiceBase
    {
        System.Timers.Timer timer1;  //计时器
        WindowTaske w = new WindowTaske();
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            timer1 = new System.Timers.Timer();

            timer1.Interval = 3000;  //设置计时器事件间隔执行时间

            timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);

            timer1.Enabled = true;

            if (!EventLog.SourceExists("OnStart222"))
            {
                EventLog.CreateEventSource("OnStart222", "jason");
            }

            EventLog.WriteEntry("OnStart222", "开始任务了");
        }

        protected override void OnStop()
        {
            this.timer1.Enabled = false;
            EventLog.WriteEntry("OnStart222", "任务结束");
        }

        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (e.SignalTime.Hour == 0 && e.SignalTime.Minute == 0 && e.SignalTime.Second == 0)//晚上12点发送邮件
            { 
                //发送邮件方法
            }
        }

        
    }
}
正解[/quote]
chen870201 2014-03-30
  • 打赏
  • 举报
回复
系统的定时任务不是挺好用的?
hudsonhuang 2014-03-30
  • 打赏
  • 举报
回复
计划任务,访问一个页面
fefefefaf 2014-03-30
  • 打赏
  • 举报
回复
ai薪手来光光
不要做咸鱼 2014-03-30
  • 打赏
  • 举报
回复
schtasks
霜寒月冷 2014-03-29
  • 打赏
  • 举报
回复
引用 7 楼 zhanglong_longlong 的回复:
window服务:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;

namespace WindowsService2
{
    public partial class Service1 : ServiceBase
    {
        System.Timers.Timer timer1;  //计时器
        WindowTaske w = new WindowTaske();
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            timer1 = new System.Timers.Timer();

            timer1.Interval = 3000;  //设置计时器事件间隔执行时间

            timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);

            timer1.Enabled = true;

            if (!EventLog.SourceExists("OnStart222"))
            {
                EventLog.CreateEventSource("OnStart222", "jason");
            }

            EventLog.WriteEntry("OnStart222", "开始任务了");
        }

        protected override void OnStop()
        {
            this.timer1.Enabled = false;
            EventLog.WriteEntry("OnStart222", "任务结束");
        }

        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (e.SignalTime.Hour == 0 && e.SignalTime.Minute == 0 && e.SignalTime.Second == 0)//晚上12点发送邮件
            { 
                //发送邮件方法
            }
        }

        
    }
}
正解
Kylor 2014-03-28
  • 打赏
  • 举报
回复
其实我感觉你用计划任务挺好的。
加载更多回复(18)

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧