//啥也不说了,看看就明白了
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("
" + DateTime.Now.ToString("yyyyMMdd") + "新建專案 | |
---|---|
專案號碼 | 工程名稱 |
" + SDR["ZAH"] + " | " + SDR["GCMC"] + " |
htmlBodyB.Append("
"+DateTime.Now.ToString("yyyyMMdd")+"新建專案 | ||||
---|---|---|---|---|
專案號碼 | 工程名稱 | 倉庫 | 庫位號碼 | 產品處利潤中心 |
" + SDR["ZAH"] + " | " + SDR["GCMC"] + " | " + SDR["KW"] + " | " + SDR["KWH"] + " | " + SDR["CPLRZX"] + " |
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;
}
}
}
}