C#背景程式重啟IIS,發郵件通知

來源:互聯網
上載者:User

標籤:

應用情境:IIS網站掛掉,系統自動重啟IIS,通知相關連絡人;

主要代碼:

監控類

public class monitoringiis    {        EmailSend send = new EmailSend();        public void MonitoringISSAppPool(object obj)        {            string method_Start = "Start";            string ISSAppPoolName = "";            string iislist = System.Configuration.ConfigurationManager.AppSettings["iislist"];            DirectoryEntry appPool = new DirectoryEntry("IIS://LOCALHOST/W3SVC/AppPools");            while (true)            {                try                {                    foreach (DirectoryEntry item in appPool.Children)                    {                        string AppPoolCommand = item.Properties["ManagedPipelineMode"].Value.ToString();                        string ManagedRuntimeVersion = item.Properties["ManagedRuntimeVersion"].Value.ToString();//,net版本號碼                                            string AppPoolState = item.Properties["AppPoolState"].Value.ToString();//目前狀態                                            if (AppPoolState != "2" && iislist.Contains(item.Name.Trim()))                        {                            DirectoryEntry findPool = appPool.Children.Find(item.Name, "IIsApplicationPool");                            findPool.Invoke(method_Start, null);                            ISSAppPoolName = item.Name;                            appPool.CommitChanges();                            appPool.Close();                            var IIsMsg = string.Format("應用池名稱:[{0}],NET版本:{1},標識:{2},目前狀態:[{3}],時間:{4}/n", item.Name, ManagedRuntimeVersion,                                item.SchemaClassName, "已啟動", DateTime.Now.ToString());                            logclass.Info(IIsMsg);                        }                    }                    Thread.Sleep(180000);                }                catch (Exception ex)                {                    logclass.Error("MonitoringISSAppPool:" + ISSAppPoolName + string.Format("==時間:{0},啟動失敗", DateTime.Now) + "==" + ex.Source + "==" + ex.InnerException + "==" + ex.TargetSite + "==" + ex.Message);//日誌記錄錯誤資訊                                  send.SendMail(string.Format("伺服器:" + GetComputerInfo() + "==MonitoringISSAppPool:{0},時間:{1},啟動失敗", ISSAppPoolName, DateTime.Now) + "==" + ex.Message,                        ISSAppPoolName + "ISS服務發生問題,請儘快查看原因並解決");//郵件發送錯誤資訊                        Thread.Sleep(10000);                }            }        }        /// <summary>擷取當前電腦資訊   例如:電腦名稱:aaa,IP:10.10.10.10</summary>                /// <returns></returns>                public static string GetComputerInfo()        {            var computerNmae = System.Net.Dns.GetHostName();//擷取當前電腦名稱                        var computerIp = new System.Net.IPAddress(System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList[0].Address).ToString();            //擷取區域網路IP                                         return string.Format("PC_Name: {0},IP: {1}", computerNmae, computerIp);        }    }
View Code

發郵件類:

public class EmailSend    {        public void SendMail(string sendmessage,string sendtitle)        {            string senduser = System.Configuration.ConfigurationManager.AppSettings["senduser"];            string sendpwd = System.Configuration.ConfigurationManager.AppSettings["sendpwd"];            string[] receiveusers = System.Configuration.ConfigurationManager.AppSettings["receiveusers"].Split(‘|‘);            string[] fasong = senduser.Split(‘@‘);            string[] fs = fasong[1].Split(‘.‘);            //發送            SmtpClient client = new SmtpClient("smtp.163.com");   //設定郵件協議            client.UseDefaultCredentials = false;//這一句得寫前面            client.DeliveryMethod = SmtpDeliveryMethod.Network; //通過網路發送到Smtp伺服器            client.Credentials = new NetworkCredential(fasong[0].ToString(), sendpwd); //通過使用者名稱和密碼 認證            foreach (string jsuser in receiveusers)            {                MailMessage mmsg = new MailMessage(new MailAddress(senduser), new MailAddress(jsuser)); //寄件者和收件者的郵箱地址                mmsg.Subject = sendtitle;      //郵件主題                mmsg.SubjectEncoding = Encoding.UTF8;   //主題編碼                mmsg.Body = sendmessage;         //郵件內文                mmsg.BodyEncoding = Encoding.UTF8;      //本文編碼                mmsg.IsBodyHtml = true;    //設定為HTML格式                           mmsg.Priority = MailPriority.High;   //優先順序                try                {                    client.Send(mmsg);                }                catch (Exception ex)                {                    logclass.Debug("發送郵件失敗:" + ex.Message);                }            }        }    }
View Code

 

C#背景程式重啟IIS,發郵件通知

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.