我正在開發一個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>
我怎麼可能會得到這些電子郵件在這些間隔發送?