需要窗口服务间隔的建议

时间:2013-10-16 08:58:00

标签: c# vb.net email send

我有窗口服务应用程序每隔10秒发送一次电子邮件,但我想在特定日期发送它。我想只在那一天发送一次/一封电子邮件。我需要将10秒改为1天吗?或者还有其他任何方式。

代码:

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.Web;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace TestWS
{
    public partial class MyNewService : ServiceBase
    {
        private string from_email = "dcaquino@trends.com.ph";
        private string to_email   = "dcaquino@trends.com.ph";
        //private string cc_email   = "dcaquino@trends.com.ph";
        System.Timers.Timer timer = new System.Timers.Timer();
        public MyNewService()
        {
            InitializeComponent();
            this.CanStop = true;
            this.CanPauseAndContinue = true;
        }
        protected override void OnStart(string[] args)
        {
            timer.Stop();
            timer.Elapsed += new System.Timers.ElapsedEventHandler(sendmail);
            timer.Interval = 10000; // 15 min
            timer.Enabled = true;
            timer.AutoReset = true;
            timer.Start();
            //timer.Enabled = true;
            //timer.Interval = 10000;
            //timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        }
        protected override void OnStop()
        {
        }

        //--------TIMER----//
        protected void timer_Elapsed(object source, System.Timers.ElapsedEventArgs aa)
        {
            check_contract();
        }

        //---------SEND EMAIL AND CHECK FOR THE CONTRACT END DATE-------//
        private void check_contract()
        {
            string constring = "server=localhost;user=root;database=scms;port=3306;password=;";
            MySqlConnection conn = new MySqlConnection(constring);
            conn.Open();
            string query = "select a.contract_end,a.contract_id,b.title from contracts a join clients b on a.client_id = b.id";
            MySqlCommand cmd1 = new MySqlCommand(query, conn);
            MySqlDataAdapter ad1 = new MySqlDataAdapter(cmd1);
            DataTable dt1 = new DataTable();
            ad1.Fill(dt1);

            string querycount = "select count(contract_end) from contracts";
            MySqlCommand cmd2 = new MySqlCommand(querycount, conn);
            MySqlDataAdapter ad2 = new MySqlDataAdapter(cmd2);
            DataTable dt2 = new DataTable();
            ad2.Fill(dt2);
            int count_dates = Convert.ToInt16(dt2.Rows[0][0].ToString());

            for (int i = 0; i < count_dates; i++)
            {
                DateTime c_end = Convert.ToDateTime(dt1.Rows[i][0]);
                DateTime c_end_30 = c_end.AddDays(-30);
                DateTime c_end_15 = c_end.AddDays(-15);
                DateTime c_end_10 = c_end.AddDays(-10);
                string c_id = Convert.ToString(dt1.Rows[i][1]);
                string client_name = Convert.ToString(dt1.Rows[i][2]);
                string format = "MMMM dd, yyyy";
                string date_expired = Convert.ToDateTime(dt1.Rows[i][0]).ToString(format);
                if (c_end_30 == DateTime.Today)
                {
                    sendemail(c_id, client_name, date_expired,"30 Days");
                }
                else if(c_end_15 == DateTime.Today)
                {
                    sendemail(c_id, client_name, date_expired, "15 Days");
                }
                else if (c_end_10 == DateTime.Today)
                {
                    sendemail(c_id, client_name, date_expired, "10 Days");
                }
            }
            conn.Close();
        }
        private void sendemail(string c_id, string client_name, string date_expired,string days_remain)
        {
            SmtpClient smtpClient = new SmtpClient();

            using (MailMessage message = new MailMessage())
            {
                MailAddress fromAddress = new MailAddress(from_email);
                MailAddress toAddress = new MailAddress(to_email);
                //MailAddress ccAddress = new MailAddress(ccAddress);

                message.From = fromAddress;
                message.To.Add(toAddress);
                //message.CC.Add(ccAddress);
                message.Subject = "Contract Expiration -SCMS";
                message.IsBodyHtml = true;
                message.Body = "Hello Account Manager, <br /> <br /> <br />" +
                               "Contract will expired on " + date_expired +
                               "<br /><br />Days Remaining: " + days_remain +
                               "<br /><br />Contract Number: " + c_id +
                               "<br /><br />Client Name: " + client_name +
                               "<br /><br /><br /><br />Please prepare for necessary steps to update the client for renewal of contract. <br/>" +
                               "<br /><br /><br />Message from SCMS";

                smtpClient.Host = "10.10.20.20";
                smtpClient.Port = 25;
                smtpClient.Credentials = new System.Net.NetworkCredential("dcaquino@trends.com.ph", "@dca12345");
                smtpClient.Send(message);
                smtpClient.ServicePoint.CloseConnectionGroup(smtpClient.ServicePoint.ConnectionName);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

你可能需要的是一个cron作业类型的代码,我建议这篇文章

http://blog.bobcravens.com/2009/10/an-event-based-cron-scheduled-job-in-c/

在那里,您将找到我在Java中使用的Quartz.NET链接,它非常可靠。

答案 1 :(得分:0)

只需将上次发送邮件的日期和时间存储在注册表(或配置文件)中。

在定时检查中启动服务时读取该值,以查看自上次发送邮件以来的时间:

'At Startup
Dim lastSend as DateTime = ReadFromRegistry

'In a Timer
If (DateTime.Now - lastSend).TotalDays >= 1 Then
    'send mail   
    lastSend = DateTime.Now
    'store lastSend in registry
End If