简体   繁体   English

在Windows Service中按特定时间间隔发送电子邮件

[英]Sending a email on a specfic time interval in windows service

I am using Visual Studio 2010 and Database Sql Server 2010. 我正在使用Visual Studio 2010和数据库Sql Server 2010。

I have Created a Window Service which must sent email's when time interval occurs (EMAIL_SENT_TIME) 我创建了一个窗口服务,该窗口服务必须在发生时间间隔(EMAIL_SENT_TIME)时发送电子邮件

My Database Contains this columns 我的数据库包含此列

    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

Assume table contains this date tracker_Id | 假设表包含此日期tracker_Id | EMAIL_SENT_TIME | EMAIL_SENT_TIME | Last_check | 最后检查| next_check next_check
100097 1800 datetime nextdatetime 100097 1800 datetime nextdatetime
100098 3600 datetime nextdatetime 100098 3600 datetime nextdatetime
100099 7200 datetime nextdatetime 100099 7200日期时间nextdatetime
100100 1800 datetime nextdatetime 100100 1800 datetime nextdatetime

1800 means 30 mins, 3600 means 1 hours etc. 1800表示30分钟,3600表示1小时,依此类推。

I want to send email after 30 mins those EMAIL_SENT_TIME are 1800 and so on. 我想在30分钟后EMAIL_SENT_TIME为1800等发送电子邮件。

how to solve this problem.... 如何解决这个问题呢....

Can the EMAIL_SENT_TIME column change for each record or is it a constant? EMAIL_SENT_TIME列是否可以为每条记录更改,或者它是常量? One of the easiest ways to do something on an interval is the System.Threading.Timer class. System.Threading.Timer类是在某个时间间隔上做某事的最简单方法之一。 The first parameter is the callback function (in this case to send an email every time the interval elapses), then state data (this could possibly contain information you want to insert into each email, offset time, and the last is the interval. This class also allows you to change the interval while the Timer is running if your database EMAIL_SENT_TIME value changes. 第一个参数是回调函数(在这种情况下,每隔一段时间间隔发送一次电子邮件),然后是状态数据(可能包含要插入每封电子邮件中的信息,偏移时间,最后一个是间隔)。如果您的数据库EMAIL_SENT_TIME值更改,该类还允许您更改计时器运行时的间隔。

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

In the SendEmail() function you can record the current DateTime.Now in the Last_Check time column. 在SendEmail()函数中,您可以在Last_Check时间列中记录当前的DateTime.Now。 You don't really need Next_Check because you can always add last check plus interval to get that value. 您实际上并不需要Next_Check,因为您总是可以添加上次检查加上间隔来获取该值。

System.Threading.Timer System.Threading.Timer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM