创建Global.asax
<%@ Application Language="C#" %> <%@ Import Namespace="System.Threading" %> <%@ Import Namespace="System.Timers" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Net" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="Model" %> <%@ Import Namespace="BLL" %> <%@ Import Namespace="DBUtility" %> <script runat="server"> void Application_Start(object sender, EventArgs e) { //在应用程序启动时运行的代码 //定义定时器 System.Timers.Timer myTimer = new System.Timers.Timer(60000);//1分钟 myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed); myTimer.AutoReset = true;//设置是执行一次(false)还是一直执行(true); myTimer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件; } void myTimer_Elapsed(object source, ElapsedEventArgs e) { try { DateTime dt1 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")+" 23:17:59"); DateTime dt2 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:18:59"); DateTime dt3 = DateTime.Now; //if (DateTime.Compare(dt1, dt2) >= 0) if (dt3 > dt1 && dt3 < dt2) { //在这里写你需要执行的任务 string name=System.DateTime.Now.ToString("yyyyMMdd"); string pFilePath = @"C:\web\SaveFile\" + name + ".xlsx";//附件,分号隔开 string receiver = "xxxx@163.com";//分号隔开 string AddRecipientCC = "";//分号隔开 string table = "定时任务,定时邮件"; table += '\n'; table += "看到此信息代表发送成功!"; sendEmail("xx", "xxxx@163.com", "1111123334", receiver, AddRecipientCC, "定时邮件", table, "smtp.163.com", pFilePath); } } catch (Exception ex) { throw new Exception(ex.Message, ex); } } //发送邮件方法 public bool sendEmail(string senderuser/*发件人显示发送时的名称*/, string euser/*发件人邮箱地址*/, string epwd/*发件人邮箱密码*/ , string receiver/*收件人邮箱地址*/, string AddRecipientCC/*cc抄送邮箱地址*/, string subject/*邮件标题*/, string body/*邮件内容*/ , string eserver/*邮件服务器smtp.harmony.com.cn*/, string attachment/*邮件附件;隔开,附件为空可传null*/)//9个参数 { jmail.MessageClass jmMessage = new jmail.MessageClass(); jmMessage.Charset = "GB2312"; jmMessage.ISOEncodeHeaders = false; jmMessage.From = euser; jmMessage.FromName = senderuser; jmMessage.Subject = subject; jmMessage.MailServerUserName = euser; jmMessage.MailServerPassWord = epwd; if (!string.IsNullOrEmpty(receiver)) { string[] recip = receiver.Replace(';', ';').Split(';'); foreach (string item in recip) { jmMessage.AddRecipient(item.Trim(), "", ""); } } if (!string.IsNullOrEmpty(AddRecipientCC)) { string[] cc = AddRecipientCC.Replace(';', ';').Split(';'); foreach (string item in cc) { jmMessage.AddRecipientCC(item.Trim(), "", ""); } } jmMessage.Body = body; #region 附件 if (!string.IsNullOrEmpty(attachment)) { string[] attach = attachment.Split(';'); foreach (string item in attach) { string ContentType = ""; switch (item.Trim().Substring(item.LastIndexOf(".") + 1).ToLower()) { case "pdf": ContentType = "application/pdf"; break; case "jpg": case "jpeg": ContentType = "image/jpeg"; break; case "gif": ContentType = "image/gif"; break; case "png": ContentType = "image/png"; break; case "bmp": ContentType = "image/bmp"; break; case "zip": ContentType = "application/zip"; break; case "txt": ContentType = "text/plain"; break; case "htm": case "html": ContentType = "text/html"; break; case "doc": ContentType = "application/msword"; break; case "xls": ContentType = "application/vnd.ms-excel"; break; case "ppt": ContentType = "application/vnd.ms-powerpoint"; break; default: ContentType = "application/octet-stream"; break; } jmMessage.AddAttachment(item, true, ContentType); } } #endregion bool result = jmMessage.Send(eserver, false); jmMessage = null; return result; } void Application_End(object sender, EventArgs e) { //在应用程序关闭时运行的代码 //下面的代码是关键,可解决IIS应用程序池自动回收的问题 Thread.Sleep(1000); //这里设置你的web地址,可以随便指向你的任意一个aspx页面甚至不存在的页面,目的是要激发Application_Start string url = "http://192.168.0.1/web/"; HttpWebRequest myHttpWebRequest = (HttpWebRequest)System.Net.WebRequest.Create(url); HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流 } 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,则不会引发该事件。 } </script>