我正在开发一个 ASP.NET(带有 sql server)应用程序。我的要求是以特定的时间间隔从主机网络服务器(在我的情况下是来自 Godaddy 的 Windows 共享主机)发送电子邮件 - 这些可能是每天、每周或每月。无法使用 Cron 选项卡,因为它是在 Linux 主机上运行的 Linux 命令。Godaddy 的共享主机没有任何任务计划程序工具。我尝试了很多次,但都没有成功。我已经使用了这三个代码。
第一次尝试:
<%@ 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>
我如何才能在这些时间间隔内收到这些电子邮件?