2013-10-16 25 views
-2

我有窗口服務應用程序每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 = "[email protected]"; 
     private string to_email = "[email protected]"; 
     //private string cc_email = "[email protected]"; 
     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("[email protected]", "@dca12345"); 
       smtpClient.Send(message); 
       smtpClient.ServicePoint.CloseConnectionGroup(smtpClient.ServicePoint.ConnectionName); 
      } 
     } 
    } 
} 
+0

不清楚你問:

當服務在你的計時器開始檢查中怎麼看太久,因爲你最後一次發送的郵件閱讀價值。請刪除所有代碼沒有直接關係的問題 –

+0

已編輯謝謝.... – Denmark

回答

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 
+0

你可以編輯我的代碼或更具體我有點困惑..謝謝 – Denmark