2013-10-16 2 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

관련 문제