C# 每天自动发送E-mail告警程序

//啥也不说了,看看就明白了

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data.SqlClient;
using System.Net.Mail;
//C# 发送E-mail告警程序
//用Windows计划话任务让它每天晚上11:30执行一次就带到了每天自动告警的功能
namespace EmailService
{
    class Program
    {
        static void Main(string[] args)
        {
           string Connstr= getConnStr();
           if (Connstr == "")
           {
               F_MailSend("[email protected]", "Connstr is NULL!");//出现异常用E-mail告警
               return;
           }
           try
           {
               string EmailListA = getEmailListA(Connstr);
               string EmailListB = getEmailListB(Connstr);
               string HtmlBodyA = getBodyHtmlA(Connstr);
               string HtmlBodyB = getBodyHtmlB(Connstr);
               F_MailSend(EmailListA, HtmlBodyA);

               F_MailSend(EmailListB, HtmlBodyB);
           }
           catch (Exception listException)
           {
               F_MailSend("[email protected]", "listException:
"+listException.Message+":
"+listException.StackTrace);//出现异常用E-mail告警
               return;
           }

         
        }
        static String getConnStr()//取得连接字符串
        {
            //XmlReader reader = XmlReader.Create("web.config");
            XmlDocument MyXMLDoc = new XmlDocument();
            try
            {
                MyXMLDoc.Load("web.config");
            }
            catch (Exception fileLoadException)
            {
                Console.WriteLine("web.config找不到!");
                F_MailSend("[email protected]", "web.config找不到!");
                //Console.Read();
            }
            XmlNodeList MyNodes = MyXMLDoc.GetElementsByTagName("add");
            //MyNodes= MyNodes[0].ChildNodes
           // MyNodes=MyXMLDoc.SelectNodes("add");
           // MyNodes=MyNodes.
            for (int i = 0; i < MyNodes.Count; i++)
            {
                try
                {
                    if (MyNodes[i].Attributes["name"].Value == "GongChengGuanLiConnectionString")
                    {
                       
                        return MyNodes[i].Attributes["connectionString"].Value.ToString();//获取数据库连接字符串
                    }
                }
                catch (Exception)
                {

                }
            }
             return "";
                //if (MyNodes.Count > 0)
                //    return MyNodes[0].InnerText;
                XmlDocument xmldoc=new XmlDocument(
                //else return "";
           
        }
        static string getEmailListA(string Connstr)//取得邮件列表A
        {
            StringBuilder EmailListA = new StringBuilder();
            SqlConnection ZDConnection = new SqlConnection(Connstr);
            ZDConnection.Open();
            SqlCommand cmdSelect = new SqlCommand("Select XMZ from dbo.SCD_YJSZ where BS=1 and XMLB='新建專案產出財務'", ZDConnection);
            SqlDataReader SDR = cmdSelect.ExecuteReader();
            while (SDR.Read())
            {
                EmailListA.Append(""+SDR["XMZ"]+";");
            }
            SDR.Close();
            ZDConnection.Close();
            return EmailListA.ToString();
        }
        static string getEmailListB(string Connstr)//取得邮件列表B
        {
            StringBuilder EmailListB= new StringBuilder();
            SqlConnection ZDConnection = new SqlConnection(Connstr);
            ZDConnection.Open();
            SqlCommand cmdSelect = new SqlCommand("Select XMZ from dbo.SCD_YJSZ where BS=1 and XMLB='新建專案產出倉庫'", ZDConnection);
            SqlDataReader SDR = cmdSelect.ExecuteReader();
            while (SDR.Read())
            {
                EmailListB.Append("" + SDR["XMZ"] + ";");
            }
            SDR.Close();
            ZDConnection.Close();
            return EmailListB.ToString();
        }
        static string getBodyHtmlA(string Connstr)//取得A表输出内容
        {
            StringBuilder htmlBodyA = new StringBuilder(8000);
            SqlConnection ZDConnection = new SqlConnection(Connstr);
            ZDConnection.Open();
            SqlCommand cmdSelect = new SqlCommand(@"Select SG.GCMC,SG.ZAH from dbo.SCD_GXDY SG inner join dbo.SCD_GXDY_XGJL SGL
on SG.BH=SGL.BH and SGL.XGLX='INSERT'
and Convert(Bigint,Convert(DateTime,Convert(varchar(11),getDate(),111),111))-Convert(Bigint,Convert(DateTime,Convert(varchar(11),XGSJ,111),111))=0 order by SG.ZAH ASC", ZDConnection);
            htmlBodyA.Append("

");
            SqlDataReader SDR = cmdSelect.ExecuteReader();
            bool hasRecord = false;
            string BgColor = "";
            while (SDR.Read())//
            {
                htmlBodyA.Append("");
                if (BgColor != "")
                {
                    BgColor = "";
                }
                else
                {
                    BgColor = "#AAAAAA";
                }
                hasRecord = true;
            }
            htmlBodyA.Append("
" + DateTime.Now.ToString("yyyyMMdd") + "新建專案
專案號碼工程名稱
" + SDR["ZAH"] + "" + SDR["GCMC"] + "
");
            SDR.Close();
            ZDConnection.Close();
            if (!hasRecord)
            {
                return "";
            }
            else
            {
                return htmlBodyA.ToString();
            }
        }
        static string getBodyHtmlB(string Connstr)//取得B表输出内容
        {
             StringBuilder htmlBodyB = new StringBuilder(8000);
            SqlConnection ZDConnection = new SqlConnection(Connstr);
            ZDConnection.Open();
            SqlCommand cmdSelect = new SqlCommand(@"Select SG.GCMC,SG.ZAH,SJ.CPLRZX,SJ.KW,SG.KWH from dbo.SCD_GXDY SG inner join dbo.SCD_GXDY_XGJL SGL
on SG.BH=SGL.BH and SGL.XGLX='INSERT' and SG.BS=1
and Convert(Bigint,Convert(DateTime,Convert(varchar(11),getDate(),111),111))-Convert(Bigint,Convert(DateTime,Convert(varchar(11),XGSJ,111),111))=0
left join dbo.SCD_JCSZ SJ on SG.CMDPFX=SJ.CPDL
order by SG.ZAH ASC", ZDConnection);

            htmlBodyB.Append("

");
            SqlDataReader SDR = cmdSelect.ExecuteReader();
            bool hasRecord = false;
            string BgColor = "";
            while (SDR.Read())//
            {
              
                htmlBodyB.Append("");
                if (BgColor != "")
                {
                    BgColor = "";
                }
                else
                {
                    BgColor = "#AAAAAA";
                }
                hasRecord = true;
            }
            htmlBodyB.Append("
"+DateTime.Now.ToString("yyyyMMdd")+"新建專案
專案號碼工程名稱倉庫庫位號碼產品處利潤中心
" + SDR["ZAH"] + "" + SDR["GCMC"] + "" + SDR["KW"] + "" + SDR["KWH"] + "" + SDR["CPLRZX"] + "
");
            SDR.Close();
            ZDConnection.Close();
            if (!hasRecord)
            {
                return "";
            }
            else
            {
                return htmlBodyB.ToString();
            }
        }
        static bool F_MailSend(String toAdress,String HtmlBody)//发送邮件
        {
            if (toAdress == "" || HtmlBody == "")
            {
                return false;
            }
            if (toAdress.Substring(toAdress.Length - 1, 1) == ";")
            {
                toAdress = toAdress.Substring(0, toAdress.Length-1);
            }
            try
            {
                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.delta.com.cn");
                client.UseDefaultCredentials = false;
                // client.Credentials = new System.Net.NetworkCredential("[email protected]", "582191"); //要发送邮件的帐号信息,如果要发到外网这一行就不能注释
                client.DeliveryMethod = SmtpDeliveryMethod.Network;

                System.Net.Mail.MailMessage message = new MailMessage("[email protected]", toAdress, DateTime.Now.ToString("yyyyMMdd新建專案"), HtmlBody);
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = true;
                client.Send(message);
                //MessageBox.Show("发送成功!");
                return true;
            }
            catch (Exception e_sendmail)
            {
                //MessageBox.Show(e_sendmail.Message, "Error!");
                F_MailSend("[email protected]", "To:" + toAdress + "
" + e_sendmail.Message + ":
" + e_sendmail.StackTrace + "
" + HtmlBody);//出现异常用E-mail告警

                return false;
            }

        }
    }
}

你可能感兴趣的:(database)