Create Task Scheduler In C# And.NET

Hi everyone!
 
Today, we will create a Windows application in Visual Studio for sending emails and will create its EXE file. Also, we will set task scheduler for autorun EXE file.

Create Windows application

Start Visual Studio >> create a new project >> select Windows application in C#.

In this project, in the program.cs file, we need to create the following method for sending the emails and stuff.
  1. public static void SendMail() {  
  2.     try {  
  3.         Console.WriteLine("Method call at" + DateTime.Now);  
  4.         MailMessage mail = new MailMessage();  
  5.         SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");  
  6.         mail.From = new MailAddress("Sender mail address");  
  7.         mail.To.Add("Receiver mail address");  
  8.         mail.Subject = "Test Mail";  
  9.         mail.Body = "This is for testing of cron-job";  
  10.         SmtpServer.Port = 587;  
  11.         SmtpServer.Credentials = new System.Net.NetworkCredential("Sender mail address""Password of sender mail address");  
  12.         SmtpServer.EnableSsl = true;  
  13.         SmtpServer.Send(mail);  
  14.         Console.WriteLine("Mail sent at" + DateTime.Now);  
  15.         Console.ReadLine();  
  16.     } catch (Exception ex) {}  
  17. }  
Let's now call this method into the main method.
  1. static void Main(string[] args)  
  2. {  
  3.    SendMail();  
  4. }  
Publish & create EXE

Once the above code is completed and executed, the following 3 files will be created.

C# 

Create Task Scheduler

For performing autorun operation, we need to set one task scheduler.

Open task scheduler 

C# 

Click Create Task which is present on the right side of the screen, as shown in the image.

The following window will open.

C#

Here, we can give a meaningful name to the task scheduler and in another tab, set trigger.

C# 

Choose the frequency as - one time, daily, weekly, or monthly as per your requirement. Set an appropriate time check Enabled check box. In the next tab, we will set action, i.e., start EXE file (or set appropriate action as per your requirement).

C#

Choose action and select path of the exe file.

Your task scheduler is ready to use.

Thank You.