2013-10-11 61 views
0

我正在開發一個ASP.NET(使用sql server)應用程序。我的要求是以特定的時間間隔從主機Web服務器發送電子郵件(在我的情況下是來自Godaddy的Windows共享主機) - 這些可能是每天或每週或每月。 Cron選項卡不能使用,因爲它是在Linux主機上運行的Linux命令。 Godaddy的共享主機沒有任何任務計劃程序工具。我已經嘗試了很多次,但無法取得成功。我已經使用了這三個代碼。在ASP.NET中調度作業(自動發送電子郵件)

第一次嘗試:

<%@ Application Language="C#" %> 
<%@ Import Namespace="System.IO" %> 
<%@ Import Namespace="System.Threading" %> 
<%@ Import Namespace="System.Data" %> 
<%@ Import Namespace="System.Data.SqlClient" %> 
<%@ Import Namespace="System.Timers" %> 
<script runat="server">  

    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(); 
    }  

</script> 

第二次嘗試:

<%@ Application Language="C#" %> 
<%@ Import Namespace="System.IO" %> 
<%@ Import Namespace="System.Threading" %> 
<%@ Import Namespace="System.Data" %> 
<%@ Import Namespace="System.Data.SqlClient" %> 


<script runat="server">  

    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(); 
    } 
</script> 

第三次嘗試:

<%@ Application Language="C#" %> 
<%@ Import Namespace="System.IO" %> 
<%@ Import Namespace="System.Threading" %> 
<%@ Import Namespace="System.Data" %> 
<%@ Import Namespace="System.Data.SqlClient" %> 


<script runat="server">  
    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 < 0) 
     { 
      return repeatMS + delay; 
     } 
     else 
     { 
      return delay; 
     } 
    } 

    private long GetTimerRepeatDelay(int hours) 
    { 
     long repeatMS; 
     repeatMS = 1000 * 60 * 60 * hours; 
     return repeatMS; 
    } 
</script> 

我怎麼可能會得到這些電子郵件在這些間隔發送?

回答

0

如果沒有某種任務計劃程序或Windows服務在後臺運行,您試圖實現的目標是不可能的。

你可以嘗試做的是創建一個網頁,當被請求時會發送一定數量的電子郵件,然後使用一些第三方ping服務或從本地PC上運行一些非常簡單的腳本來調用此頁面。

請注意,這只是一種解決方法,並且一個可靠的解決方案需要在具有VPS或專用服務器託管的服務器上進行更多訪問。

你可以嘗試的另一件事是某種第三方服務,它會爲你發送電子郵件。你可以嘗試郵件黑猩猩 - 他們有免費版本,每月有限的電子郵件數量,還有一個你可以使用的API。

0

很古老的問題,但可能會幫助一些新的讀者。 在ASP.NET中,我們可以模擬Windows服務來運行預定作業。

這非常奇怪,但緩存項目對此非常有用。

邏輯非常簡單,不同。

  1. 創建一個緩存對象

    private const string DummyCacheItemKey = "GagaGuguGigi"; 
    
    protected void Application_Start(Object sender, EventArgs e) 
    { 
        RegisterCacheEntry(); 
    } 
    
    private bool RegisterCacheEntry() 
    { 
        if(null != HttpContext.Current.Cache[ DummyCacheItemKey ]) 
         return false; 
    
        HttpContext.Current.Cache.Add(DummyCacheItemKey, "Test", null, 
        DateTime.MaxValue, TimeSpan.FromMinutes(1), 
        CacheItemPriority.Normal, 
        new CacheItemRemovedCallback(CacheItemRemovedCallback)); 
    
        return true; 
    } 
    
  2. 當緩存對象已被刪除,養一個回調函數來運行計劃的作業。

    public void CacheItemRemovedCallback(string key, 
               object value, CacheItemRemovedReason reason) 
    { 
        Debug.WriteLine("Cache item callback: " + DateTime.Now.ToString()); 
        HitPage() 
    // Do the service works 
    
    DoWork(); 
    } 
    
  3. 關於回調函數,您必須重新設置緩存。爲此,創建一個虛擬頁面並通過webclient訪問。

    private const string DummyPageUrl = 
          "http://localhost/TestCacheTimeout/WebForm1.aspx"; 
    
    private void HitPage() 
    { 
        WebClient client = new WebClient(); 
        client.DownloadData(DummyPageUrl); 
    } 
    
  4. 在Application_BeginRequest上檢查它是否爲虛擬頁面。

    protected void Application_BeginRequest(Object sender, EventArgs e) 
    { 
        // If the dummy page is hit, then it means we want to add another item 
    
        // in cache 
    
        if(HttpContext.Current.Request.Url.ToString() == DummyPageUrl) 
        { 
         // Add the item in cache and when succesful, do the work. 
    
         RegisterCacheEntry(); 
        } 
    } 
    

Here is the details how you can schedule your activities with pure ASP.NET