在Windows服务中以特定时间间隔发送电子邮件

时间:2012-06-15 18:32:06

标签: c# visual-studio-2010 windows-services window

我使用的是Visual Studio 2010和Database Sql Server 2010。

我创建了一个窗口服务,当时间间隔发生时必须发送电子邮件(EMAIL_SENT_TIME)

我的数据库包含此列

    Tracker_Id bigint 
,EMAIL_SENT_TIME bigint --this contains interval in which the email must be fired 1800,3600,7200 etc, 
    Last_check datetime --the previous check datetime,
    next_check datetime --the next check datetime

假设表包含此日期 tracker_Id | EMAIL_SENT_TIME | Last_check | next_check
 100097 1800 datetime nextdatetime
 100098 3600 datetime nextdatetime
 100099 7200 datetime nextdatetime
 100100 1800 datetime nextdatetime

1800表示30分钟, 3600表示1小时等。

我希望在30分钟后发送电子邮件,EMAIL_SENT_TIME为1800,依此类推。

如何解决这个问题....

1 个答案:

答案 0 :(得分:0)

每条记录的EMAIL_SENT_TIME列都可以更改,还是常量?在间隔上执行某些操作的最简单方法之一是System.Threading.Timer类。第一个参数是回调函数(在这种情况下,每次间隔过去时发送一封电子邮件),然后是状态数据(这可能包含您要插入每封电子邮件的信息,偏移时间,最后一个是间隔。如果数据库EMAIL_SENT_TIME值发生变化,class还允许您在Timer运行时更改间隔。

Timer timer = new Timer((x) => SendEmail(), null, 0, EMAIL_SENT_TIME * 1000).

在SendEmail()函数中,您可以在Last_Check时间列中记录当前的DateTime.Now。你真的不需要Next_Check,因为你总是可以添加最后一次检查加上间隔来获得该值。

System.Threading.Timer