asp.Net利用Global.asaxタイミング送信メール


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 = "[email protected]";//    
                string AddRecipientCC = "";//    
                string table = "    ,    ";
                table += '
'; table += " !"; sendEmail("xx", "[email protected]", "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>