ASP.NET 定时通知

ASP.NET 定时通知

 

  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

}

 

 

 

posted @ 2015-01-07 15:25  NathanJoy  阅读(474)  评论(0编辑  收藏  举报