這是爲我工作。測試它首先要知道定時器不會被GC服務器上收集:
的Web.config
<configuration>
<system.net>
<mailSettings>
<smtp>
<network defaultCredentials="false" host="mail.domain.com" password="psw" userName="user"/>
</smtp>
</mailSettings>
</system.net>
的Global.asax.cs
public class Global : System.Web.HttpApplication {
Timer cron;
protected void Application_Start(object sender, EventArgs e) {
cron = new Timer(1000 * 60 * 5);
cron.AutoReset = true;
cron.Start();
cron.Elapsed += (s, a) => {
var address = "[email protected]";
var mail = new MailMessage("Test <[email protected]>", address);
mail.Subject = "Test";
mail.Body = "Timer test at " + DateTime.Now.ToLongTimeString();
mail.IsBodyHtml = true;
var smtp = new SmtpClient();
smtp.Send(mail);
};
}
protected void Application_End(object sender, EventArgs e) {
cron.Stop();
}
}
請記住,這個解決方案以及其他的(thread loop,cache expiration)是hacky。 Windows任務計劃程序旨在執行此類工作。
你解決了你的問題嗎? – BrOSs 2012-10-31 19:03:54