ASP.NET 定時通知

ASP.NET 定時通知html

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.Web.Security;
  6 using System.Web.SessionState;
  7 using System.Web.Security;
  8 
  9 using System.Web.SessionState;
 10 
 11 using System.Timers;
 12 
 13 using System.Net;
 14 
 15 using System.IO;
 16 
 17 using System.Text;
 18 
 19 using System.Threading;
 20 using System.Data;  
 21 
 22 
 23 
 24 
 25 namespace ECN.Net
 26 {
 27     public class Global : System.Web.HttpApplication
 28     {
 29 
 30 
 31         public static int times = 1000 * 60 * 60 * 24;
 32 
 33         protected void Application_Start(object sender, EventArgs e)
 34         {
 35 
 36 
 37             //定義定時器  
 38 
 39             System.Timers.Timer myTimer = new System.Timers.Timer(times);
 40 
 41             myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
 42 
 43             myTimer.Enabled = true;
 44 
 45             myTimer.AutoReset = true;
 46 
 47             myTimer.Start();
 48 
 49 
 50         }
 51 
 52 
 53         void myTimer_Elapsed(object source, ElapsedEventArgs e)
 54         {
 55 
 56             try
 57             {
 58 
 59                 //Log.SaveNote(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":AutoTask is Working!");
 60 
 61                 YourTask();
 62 
 63             }
 64 
 65             catch (Exception ee)
 66             {
 67 
 68                 //Log.SaveException(ee);
 69 
 70             }
 71 
 72         }
 73 
 74         void YourTask()
 75         {
 76 
 77             //在這裏寫你須要執行的任務  
 78             string find_str = "SELECT * FROM EcnInfo ,EcnConfirm ";
 79             find_str += " WHERE EcnInfo.EcnNo=EcnConfirm.EcnNo AND EcnConfirm.IfConfirm='False'";
 80             DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.LocalSqlServer, find_str);
 81 
 82             for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
 83             {
 84 
 85                 string body = "<div><font face=\"Segoe UI\" size=\"2\" color=\"black\"><span style=\"font-size:10.5pt;\"><div><font face=\"方正舒體\" size=\"5\"><span style=\"font-size:29px;\">Hi:</span></font></div><div>&nbsp; &nbsp;<font face=\"方正舒體\" size=\"5\"><span style=\"font-size:29px;\"> </span></font><font face=\"方正舒體\" size=\"5\" color=\"red\"><span style=\"font-size:29px;\">您的ENC流還未處理,請及時處理!. ECN:" + ds.Tables[0].Rows[i]["EcnNo"] + "</span></font><font face=\"方正舒體\" size=\"5\"><span style=\"font-size:29px;\"> &nbsp;</span></font><a href=\"http://10.68.100.250/ecn/\" target=\"_blank\"><font face=\"方正舒體\" size=\"5\"><span style=\"font-size:29px;\">http://10.68.100.250/ecn/</span></font></a><font face=\"方正舒體\" size=\"5\"><span style=\"font-size:29px;\">&nbsp;</span></font></div><div><font face=\"Symbol\">&nbsp; &nbsp;&nbsp;</font><font face=\"Symbol\">&nbsp; &nbsp;&nbsp;</font><font face=\"Symbol\">&nbsp; &nbsp;&nbsp;</font><font face=\"Symbol\">&nbsp; &nbsp;&nbsp;</font><font face=\"Symbol\">&nbsp; &nbsp;&nbsp;</font><font face=\"Symbol\">&nbsp; &nbsp;&nbsp;</font><font face=\"Symbol\">&nbsp; &nbsp;&nbsp;</font><font face=\"Symbol\">&nbsp; &nbsp;&nbsp;</font></div><div><br></div><div>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</div><div><font face=\"方正舒體\" size=\"5\"><span style=\"font-size:29px;\">&nbsp; &nbsp;&nbsp;</span></font><font face=\"方正舒體\" size=\"5\"><span style=\"font-size:29px;\">&nbsp; &nbsp;&nbsp;</span></font><font face=\"方正舒體\" size=\"5\"><span style=\"font-size:29px;\">&nbsp; &nbsp; 請及時處理。</span></font></div><hr width=\"210\" size=\"1\" align=\"left\" color=\"#B5C4DF\" style=\"width:210px;\"><div><div style=\"margin:10px;\"><font size=\"5\"><span style=\"font-size:29px;\"><div><font face=\"方正舒體\">謝謝!</font></div>";
 86 
 87 
 88                 Public_CS.sendECNMail_No_Confirm(ds.Tables[0].Rows[i]["EcnNo"].ToString(), ds.Tables[0].Rows[i]["EcnName"].ToString(), body);
 89 
 90 
 91             }
 92 
 93 
 94         }
 95 
 96         protected void Application_End(object sender, EventArgs e)
 97         {
 98             // Log.SaveNote(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":Application End!");  
 99 
100             //下面的代碼是關鍵,可解決IIS應用程序池自動回收的問題  
101 
102             Thread.Sleep(1000);
103 
104             //這裏設置你的web地址,能夠隨便指向你的任意一個aspx頁面甚至不存在的頁面,目的是要激發Application_Start  
105 
106             string url = "default.aspx";
107 
108             HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
109 
110             HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
111 
112             Stream receiveStream = myHttpWebResponse.GetResponseStream();//獲得回寫的字節流  
113         }
114 
115 
116 
117 
118         protected void Session_Start(object sender, EventArgs e)
119         {
120 
121         }
122 
123         protected void Application_BeginRequest(object sender, EventArgs e)
124         {
125 
126         }
127 
128         protected void Application_AuthenticateRequest(object sender, EventArgs e)
129         {
130 
131         }
132 
133         protected void Application_Error(object sender, EventArgs e)
134         {
135 
136         }
137 
138         protected void Session_End(object sender, EventArgs e)
139         {
140 
141         }
142 
143     }
144 }

 

using System;
using System.Data;
using System.Configuration;
//using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.Net.Mail;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Net;

/// <summary>
///Public_CS 的摘要說明
/// </summary>
public class Public_CS
{
    public Public_CS()
    {
        //
        //TODO: 在此處添加構造函數邏輯
        //
    }


    public static string md5(string str, int code)  //code 16 或 32  
    {
        if (code == 16) //16位MD5加密(取32位加密的9~25字符)  
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8, 16);
        }

        if (code == 32) //32位加密  
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
        }

        return "00000000000000000000000000000000";
    }


    #region

   
    /// <summary>
    /// 發送郵件
    /// </summary>
    /// <param name="mailTo">要發送的郵箱</param>
    /// <param name="mailSubject">郵箱主題</param>
    /// <param name="mailContent">郵箱內容</param>
    /// <returns>返回發送郵箱的結果</returns>
    public static bool SendEmail(string mailTo, string mailSubject, string mailContent)
    {
        //簡單郵件傳輸協議類
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
        client.Host = "mailtc.tc.gj.com";//郵件服務器
        client.Port = 25;//smtp主機上的端口號,默認是25.
        client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;//郵件發送方式:經過網絡發送到SMTP服務器
        client.Credentials = new System.Net.NetworkCredential("tc-ecn", "tc-ecn");//憑證,發件人登陸郵箱的用戶名和密碼

        //電子郵件信息類
        System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress("xxx.com", "xxx");//發件人Email,在郵箱是這樣顯示的,[發件人:小明<panthervic@163.com>;]
        System.Net.Mail.MailAddress toAddress = new System.Net.Mail.MailAddress(mailTo, "xxx");//收件人Email,在郵箱是這樣顯示的, [收件人:小紅<43327681@163.com>;]
        System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(fromAddress, toAddress);//建立一個電子郵件類
        mailMessage.Subject = mailSubject;
        //string filePath = Server.MapPath("/index.html");//郵件的內容能夠是一個html文本.
        //System.IO.StreamReader read = new System.IO.StreamReader(filePath, System.Text.Encoding.GetEncoding("GB2312"));
        //string mailBody = read.ReadToEnd();
        //read.Close();
        mailMessage.Body = mailContent;//可爲html格式文本
        //mailMessage.Body = "郵件的內容";//可爲html格式文本
        mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;//郵件主題編碼
        mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//郵件內容編碼
        mailMessage.IsBodyHtml = true;//郵件內容是否爲html格式
        mailMessage.Priority = System.Net.Mail.MailPriority.High;//郵件的優先級,有三個值:高(在郵件主題前有一個紅色感嘆號,表示緊急),低(在郵件主題前有一個藍色向下箭頭,表示緩慢),正常(無顯示).
        try
        {
            client.Send(mailMessage);//發送郵件
            //client.SendAsync(mailMessage, "ojb");異步方法發送郵件,不會阻塞線程.
        }
        catch (Exception)
        {
            return false;
        }

        return true;
    }




    public static void sendECNMail(string zubie, string head, string body)
    {
            string find_str = "SELECT * FROM UserInfo WHERE EcnGroup='" + zubie + "'";


            DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.LocalSqlServer, find_str);

            int i = ds.Tables[0].Rows.Count;

            if (1 <= i)
            {

                for (int j = 0; j < i; j++)
                {
                    SendEmail(ds.Tables[0].Rows[j]["Email"].ToString(), head, body);
                }

            }
    }


    public static void sendECNMail_No_Confirm(string ecn_no, string head, string body)
    {
        string find_str = "SELECT * FROM EcnConfirm WHERE EcnNo='" + ecn_no + "'";

        DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.LocalSqlServer, find_str);

        int i = ds.Tables[0].Rows.Count;

        if (1 <= i)
        {

            for (int j = 0; j < i; j++)
            {
                if (ds.Tables[0].Rows[j]["PE1"].ToString() == "N")
                {
                    Public_CS.sendECNMail("PE1", head, body);
                }
                if (ds.Tables[0].Rows[j]["PE2"].ToString() == "N")
                {
                    sendECNMail("PE2", head, body);
                }
                 if (ds.Tables[0].Rows[j]["PE3"].ToString() == "N")
                {
                    sendECNMail("PE3", head, body);
                }
                 if (ds.Tables[0].Rows[j]["TE1"].ToString() == "N")
                {
                    sendECNMail("TE1", head, body);
                }
                 if (ds.Tables[0].Rows[j]["TE2"].ToString() == "N")
                {
                    sendECNMail("TE2", head, body);
                }
                 if (ds.Tables[0].Rows[j]["GZ1"].ToString() == "N")
                {
                    sendECNMail("GZ1", head, body);
                }
                 if (ds.Tables[0].Rows[j]["GZ2"].ToString() == "N")
                {
                    sendECNMail("GZ2", head, body);
                }
                 if (ds.Tables[0].Rows[j]["BC"].ToString() == "N")
                {
                    sendECNMail("BC", head, body);
                }
                 if (ds.Tables[0].Rows[j]["ME"].ToString() == "N")
                {
                    sendECNMail("ME", head, body);
                }
            }

        }
    }

    public static void Change_Confirm_State(string ecn_no)
    {
        string find_str = "SELECT * FROM EcnConfirm WHERE EcnNo='" + ecn_no + "'";

        DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.LocalSqlServer, find_str);

        int i = ds.Tables[0].Rows.Count;

        if (1 <= i)
        {

            for (int j = 0; j < i; j++)
            {
                if (ds.Tables[0].Rows[j]["PE1"].ToString() == "N")
                {
                    return;
                }
                if (ds.Tables[0].Rows[j]["PE2"].ToString() == "N")
                {
                    return;
                }
                if (ds.Tables[0].Rows[j]["PE3"].ToString() == "N")
                {
                    return;
                }
                if (ds.Tables[0].Rows[j]["TE1"].ToString() == "N")
                {
                    return;
                }
                if (ds.Tables[0].Rows[j]["TE2"].ToString() == "N")
                {
                    return;
                }
                if (ds.Tables[0].Rows[j]["GZ1"].ToString() == "N")
                {
                    return;
                }
                if (ds.Tables[0].Rows[j]["GZ2"].ToString() == "N")
                {
                    return;
                }
                if (ds.Tables[0].Rows[j]["BC"].ToString() == "N")
                {
                    return;
                }
                if (ds.Tables[0].Rows[j]["ME"].ToString() == "N")
                {
                    return;
                }
            }

        }

        string sql_str_EcnConfirm = "UPDATE  EcnConfirm SET IfConfirm= 'True' ,DateTime = '" + DateTime.Now.ToString("yyyy-MM-dd") + "' WHERE  EcnNo='" + ecn_no + "'";

        int row = SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, sql_str_EcnConfirm);

    }



    public static void Change_PoInfo_State(string ecn_no, string so)
    {
        string find_str = "UPDATE  PoInfo SET EcnInfo='" + ecn_no + "' WHERE SO='" + so + "'";
        DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.LocalSqlServer, find_str);
    }


    public static bool ValidateServerCertificate(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        return true;
    }

    public static bool SendEmail(string fromWhere, string Displayname, string toWhere, string subject, string body, string attach, string sPwd)
    {
        bool flag;
        MailMessage message = new MailMessage();
        message.From = new MailAddress(fromWhere, Displayname);
        message.To.Add(new MailAddress(toWhere));
        message.Subject = subject;
        message.IsBodyHtml = true;
        message.BodyEncoding = Encoding.UTF8;
        message.Body = body;
        message.Priority = MailPriority.High;
        if (!string.IsNullOrEmpty(attach))
        {
            message.Attachments.Add(new Attachment(attach));
        }

        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);

        SmtpClient client = new SmtpClient("xxx.xxx.com", 25);
        client.Credentials = new NetworkCredential("xxx", sPwd, "xxx.com");

        client.EnableSsl = true;
        object obj2 = message;
        try
        {
            client.Send(message);
            flag = true;
        }
        catch (Exception exception)
        {
            throw exception;
        }
        return flag;
    }



    #endregion

}
相關文章
相關標籤/搜索