Click here to Skip to main content
15,886,813 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Below code is sending mail every 3 minutes. But i want to send the Mail only once in every day at 8pm. How to set this in the below code.

C#
public partial class Scheduler : ServiceBase
   {
       System.Timers.Timer createOrderTimer;
       private Timer timer1 = null;
       public Scheduler()
       {
           InitializeComponent();
       }

       protected override void OnStart(string[] args)
       {
           ////timer1 = new Timer();
           ////this.timer1.Interval = 30000;
           ////this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
           ////timer1.Enabled = true;
           ////Library.WriteErrorLog("Test Window Service started");
           createOrderTimer = new System.Timers.Timer();
           createOrderTimer.Elapsed += new System.Timers.ElapsedEventHandler(GetMail);
           createOrderTimer.Interval = 180000;
           createOrderTimer.Enabled = true;
           createOrderTimer.AutoReset = true;
           createOrderTimer.Start();
       }

       private void timer1_Tick(object sender, ElapsedEventArgs e)
       {
           Library.WriteErrorLog("Timer Ticked and some job has been done successfully");
       }

       protected override void OnStop()
       {
           timer1.Enabled = false;
           Library.WriteErrorLog("Test Window Service stopped");
       }

       public void GetMail(object sender, System.Timers.ElapsedEventArgs args)
       {
           NetworkCredential cred = new NetworkCredential("abc@gmail.com", "abc");
           MailMessage msg = new MailMessage();
           msg.To.Add("to@abc.com");
           msg.Subject = "Welcome Venkat";

           msg.Body = "You Have Successfully Entered to venkat World!!!";
           msg.From = new MailAddress("from@gmail.com"); // Your Email Id
           SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
          // SmtpClient client1 = new SmtpClient("smtp.mail.yahoo.com", 465);
           client.Credentials = cred;
           client.EnableSsl = true;
           client.Send(msg);
       }
   }
Posted
Comments
Venkat Kumar chigulla 31-Oct-14 7:22am    
without cron jobs can't we do this...?

Apart from what other people have suggested, I would not use a Windows Service to act a "Scheduled Task" to send emails.

Instead, have a console application (or even PowerShell Script) scheduled to run at 8PM everyday (or whatever) using Windows Task Scheduler[^].

A Scheduled Task is more appropriate for your scenario. I don't think it make a lot of sense building a scheduling mechanism on a windows service when OS already provides scheduling infrastructure. A Windows service is more appropriate for processes that have to respond to events at any moment and not at specific and fix periods.
 
Share this answer
 
The timer Interval is in ms so 180000 = 3*60*1000 = 3 min, you should use cron jobs instead : https://github.com/kevincolyar/CronNET[^]
 
Share this answer
 
You can create a CRON job windows service
with cron expression 0 0 20 1/1 * ? * (runEveryday at 8pm)

how-to-create-cron-job-windows-service[^]
 
Share this answer
 
use TopShelf service base & a Quartz.Net cron job - much easier !
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900