<%@ Application Language="C#" %>
<%@ Import Namespace="System.Timers" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Net.Mail" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// 應用程式啟動時執行的程式碼
System.Timers.Timer objTimer = new Timer();
objTimer.Interval = 1000;
objTimer.Enabled = true;
objTimer.Elapsed += new ElapsedEventHandler(objTimer_Elapsed);
}
void objTimer_Elapsed(object sender, ElapsedEventArgs e)
{
//抓取過期廠商
string cmdText = "select User_Email from Users a left join SGS_Report b on a.vender_ID=b.Vender_ID where Expiry_Date<getdate()";
DataSet ds = SqlHelper.GetDataSet(cmdText);
int intHour = e.SignalTime.Hour;
int intMinute = e.SignalTime.Minute;
int intSecond = e.SignalTime.Second;
int iHour = 16;
int iMinute = 08;
int iSecond = 20;
if (intHour == iHour && intMinute == iMinute && intSecond == iSecond)
{
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
SendEmail("請儘快上傳最近的SGS報告", "SGS報告過期警告", ds.Tables[0].Rows[i]["User_Email"].ToString(), "##");
}
}
}
}
private static void TimeEvent(object source, ElapsedEventArgs e)
{
}
void Application_End(object sender, EventArgs e)
{
// 應用程式關閉時執行的程式碼
}
void Application_Error(object sender, EventArgs e)
{
// 發生未處理錯誤時執行的程式碼
}
void Session_Start(object sender, EventArgs e)
{
// 啟動新工作階段時執行的程式碼
}
void Session_End(object sender, EventArgs e)
{
// 工作階段結束時執行的程式碼。
// 注意: 只有在 Web.config 檔將 sessionstate 模式設定為 InProc 時,
// 才會引發 Session_End 事件。如果將工作階段模式設定為 StateServer
// 或 SQLServer,就不會引發這個事件。
}
private void SendEmail(string body, string title, string address, string displayName)
{
MailAddress from = new MailAddress("###@##.com", "SGS報告過期");
MailMessage mail = new MailMessage();
//設置郵件的標題
mail.Subject = title;
//設置郵件的發件人
mail.From = from;
mail.To.Add(new MailAddress(address, displayName));
//設置郵件內容
mail.Body = body;
//設置郵件格式
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
//设置邮件的发送级别
mail.Priority = MailPriority.Normal;
SmtpClient client = new SmtpClient();
//设置用于 SMTP 事务的主机的名称,填IP地址也可以
client.Host = "192.168.120.18";
//设置用于 SMTP 事务的端口,默认的是 25
//client.Port = 25;
client.UseDefaultCredentials = false;
//邮箱登陆名和密码,
client.Credentials = new System.Net.NetworkCredential("###@##.com", "");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(mail);
}
</script>