title: Scheduling jobs in ASP.NET (Automatically sending email)
body: <p>I m developing an ASP.NET (with sql server) application. My requirement is to send emails from the host web server (in my case Windows Shared Hosting from Godaddy) at specific intervals - these may be daily or weekly or monthly. Cron tab can't be used because it's Linux command which runs on Linux hosting. Godaddy's Shared hosting doesn't have any Task Scheduler tool. I've tried many times but couldn't get success. I've already used these three codes.</p>
  
  <p>First attempt:</p>
  
  <pre><code>&lt;%@ Application Language="C#" %&gt;
  &lt;%@ Import Namespace="System.IO" %&gt;
  &lt;%@ Import Namespace="System.Threading" %&gt;
  &lt;%@ Import Namespace="System.Data" %&gt;
  &lt;%@ Import Namespace="System.Data.SqlClient" %&gt;
  &lt;%@ Import Namespace="System.Timers" %&gt;
  &lt;script runat="server"&gt;    
  
      void Application_Start(object sender, EventArgs e) 
      {
          // Code that runs on application startup
          Thread timerThread = new Thread(TimerForNotification);
          timerThread.IsBackground = true;
          timerThread.Priority = ThreadPriority.Highest;
          timerThread.Start();    
      }
  
      void TimerForNotification()
      {
          //Code that runs on application startup
          System.Timers.Timer timScheduledTask = new System.Timers.Timer();
          timScheduledTask.Interval = 1000 * 60 * 60;   //TimeSpan.FromMinutes(30).Minutes * 1000 * 60;
          timScheduledTask.Enabled = true;
          timScheduledTask.Elapsed += new System.Timers.ElapsedEventHandler(timScheduledTas_Elapsed);       
      }
  
      void timScheduledTas_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
      {
          ConnectionStringSettings conn = ConfigurationManager.ConnectionStrings["myshop_con"];
          SqlConnection con = new SqlConnection(conn.ConnectionString);
          SqlCommand cmd = con.CreateCommand();
          cmd.CommandType = CommandType.Text;
          cmd.CommandText = "insert into dbo.tblUserDetail(UName)VALUES('" + DateTime.Now.ToString() + "')";
          con.Open();
          cmd.ExecuteNonQuery();
          con.Close();  
      }    
  
  &lt;/script&gt;
  </code></pre>
  
  <p>Second attempt:</p>
  
  <pre><code>&lt;%@ Application Language="C#" %&gt;
  &lt;%@ Import Namespace="System.IO" %&gt;
  &lt;%@ Import Namespace="System.Threading" %&gt;
  &lt;%@ Import Namespace="System.Data" %&gt;
  &lt;%@ Import Namespace="System.Data.SqlClient" %&gt;
  
  
  &lt;script runat="server"&gt;    
  
      public class TimerStarter
      {
          private static System.Threading.Timer threadingTimer;
          public static void StartTimer()
          {
              if (null == threadingTimer)
              {
                  threadingTimer = new System.Threading.Timer(new TimerCallback(DoActions), HttpContext.Current, 0, 3600000);
              }
          }
          private static void DoActions(object sender)
          {
              ConnectionStringSettings conn = ConfigurationManager.ConnectionStrings["myshop_con"];
              SqlConnection con = new SqlConnection(conn.ConnectionString);
              SqlCommand cmd = con.CreateCommand();
              cmd.CommandType = CommandType.Text;
              cmd.CommandText = "insert into dbo.tblUserDetail(UName)VALUES('" + DateTime.Now.ToString() + "')";
              con.Open();
              cmd.ExecuteNonQuery();
              con.Close();  
          }
  
      }
  
  
      void Application_Start(object sender, EventArgs e) 
      {
          TimerStarter.StartTimer();
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>Third attempt:</p>
  
  <pre><code>&lt;%@ Application Language="C#" %&gt;
  &lt;%@ Import Namespace="System.IO" %&gt;
  &lt;%@ Import Namespace="System.Threading" %&gt;
  &lt;%@ Import Namespace="System.Data" %&gt;
  &lt;%@ Import Namespace="System.Data.SqlClient" %&gt;
  
  
  &lt;script runat="server"&gt;    
      private void NightlyProcess(object o)
      {
          ConnectionStringSettings conn = ConfigurationManager.ConnectionStrings["myshop_con"];
          SqlConnection con = new SqlConnection(conn.ConnectionString);
          SqlCommand cmd = con.CreateCommand();
          cmd.CommandType = CommandType.Text;
          cmd.CommandText = "insert into dbo.tblUserDetail(UName)VALUES('" + DateTime.Now.ToString() + "')";
          con.Open();
          cmd.ExecuteNonQuery();
          con.Close();  
      } 
  
  
      void Application_Start(object sender, EventArgs e) 
      {
          System.Threading.TimerCallback tcb = new System.Threading.TimerCallback(NightlyProcess);
          System.Threading.Timer theTimer = new System.Threading.Timer(tcb, null, GetTimerInitialDelay(20, 40), GetTimerRepeatDelay(24));          
      }
  
      private long GetTimerInitialDelay(int hours, int minutes)
      {
          long startMS, repeatMS, currentMS;
          startMS = (1000 * 60 * 60 * hours) + (1000 * 60 * minutes);
          repeatMS = GetTimerRepeatDelay(24);
  
          DateTime now = DateTime.Now;
          long currentHours = 1000 * 60 * 60 * now.Hour;
          long currentMinutes = 1000 * 60 * now.Minute;
          long currentSeconds = 1000 * now.Second;
          long currentMilliSeconds = now.Millisecond;
          currentMS = currentHours + currentMinutes + currentSeconds + currentMilliSeconds;
          long delay = startMS - currentMS;
          if (delay &lt; 0)
          {
              return repeatMS + delay;
          }
          else
          {
              return delay;
          }
      }
  
      private long GetTimerRepeatDelay(int hours)
      {
          long repeatMS;
          repeatMS = 1000 * 60 * 60 * hours;
          return repeatMS;
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>How might I get these emails sent at these intervals?</p>