Create Job Schedule On Web using ASP.net


Create Job Schedule On Web using ASP.net

Step 1

Create Global.asax file in your application.

Step 2

Copy below code and paste in Global.asax file .

Code 
<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        System.Timers.Timer timScheduledTask = new System.Timers.Timer();
        // Timer interval is set in miliseconds,
        // In this case, we'll run a task every minute
        timScheduledTask.Interval = 60 * 1000;
        timScheduledTask.Enabled = true;
        // Add handler for Elapsed event
        timScheduledTask.Elapsed +=
        new System.Timers.ElapsedEventHandler(timScheduledTask_Elapsed);
        timScheduledTask.Stop();
        timScheduledTask.Start();
       
        
        
        

    }

    void timScheduledTask_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        // Execute some task
        //GoalReminder gr = new GoalReminder();
        int howOften = 1;
        while (howOften < 5)
        {
            //do some stuff here that i can't paste
            howOften++;
            SendMail(howOften.ToString() + " Global Mail");
        }
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }
    void SendMail(string subject)
    {
        string username, pwd;
       
        username = "xyz@example.com;
        pwd = "abc@1234";
        System.Net.Mail.MailMessage greetings = new System.Net.Mail.MailMessage();
        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
        try
        {
            greetings.From = new System.Net.Mail.MailAddress(username, "Sender Display Name");
            greetings.To.Add("abc@gmail.com");

            greetings.IsBodyHtml = true;
            greetings.Priority = System.Net.Mail.MailPriority.High;
            greetings.Body = "Body Text";
            greetings.Subject = subject;
            smtp.Host = "localhost";
            smtp.Port = 25;
            smtp.EnableSsl = false;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential(username, pwd);
            smtp.Send(greetings);
           
        }
        catch (Exception ex)
        {
          
        }
    } 
</script>



If you want to change your action you can change the code and call inside the  timScheduledTask_Elapsed event .

If you have any problem please feel free to comment .

If it helpful for you please share it.


Comments

Post a Comment

Popular posts from this blog

Convert HtmlToPDF in ASP.net & iTextshrap

Create Pivot Table In C#

How to Display Page Wise Total Amount & Grand Total Amount in Last page SSRS & RDLC Report