2011-08-03 33 views
2

我的問題是,我必須在特定的時間每天發送郵件,有沒有辦法在asp.net中做到這一點? 給我適當的建議。 注意:我不想運行Windows應用程序或Windows調度程序。我在Global.asax的如何在asp.net中爲郵件創建調度程序?

private static CacheItemRemovedCallback OnCacheRemove = null; 
    void Application_Start(object sender, EventArgs e) 
    { 
     // Code that runs on application startup 
     AddTask("Remoder", 5); 
    } 

    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. 

    } 
    private void AddTask(string name, int seconds) 
    { 
     OnCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved); 
     HttpRuntime.Cache.Insert(name, seconds, null, 
      DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration, 
      CacheItemPriority.NotRemovable, OnCacheRemove); 
    } 

    public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r) 
    { 
     // do stuff here if it matches our taskname, like WebRequest 
     // re-add our task so it recurs 
     AddTask(k, Convert.ToInt32(v)); 
    } 
    public void Remoder() 
    { 
     HttpContext.Current.Response.Write("Hello Start"); 
    } 

回答

1

我覺得從傑夫自己這個技術是你所需要的: http://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/

+0

但我想調用特定的功能,它有可能嗎? –

+0

是的,看到註釋/ /如果它與我們的任務名稱相匹配,就像WebRequest一樣 - 用函數調用替換它。例如'if(k ==「callFunction」){callFunction();}' – Widor

+0

這裏是代碼,我使用 –

1

我試圖破解...將您的應用程序global.asax轉換爲虛擬調度程序。這是我的選擇,因爲我的服務器管理部門拒絕(因爲他們不明白)使用Windows或數據庫作業。

如果我有我的druthers雖然,我會使用數據庫「工作」,因爲sql-server可以直接發送郵件。不知道你使用的是哪個數據庫,但如果你有必要的訪問權限,我建議尋找這樣的解決方案,而不是試圖欺騙你的asp.net應用程序。

+0

上面的解決方案工作正常 –

+0

因爲這樣託管公司不允許我們使用Windows作業調度程序或SQL作業調度程序 –

相關問題