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("cheng.cheng@Delta.com.cn", "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("cheng.cheng@Delta.com.cn", "listException:<br>"+listException.Message+":<br>"+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("cheng.cheng@Delta.com.cn", "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("<Table Border=1><th colspan=2 bgcolor=#AAAAAA>" + DateTime.Now.ToString("yyyyMMdd") + "新建專案</th><tr bgcolor=#BBBB00><td>專案號碼</td><td>工程名稱</td></tr>");
SqlDataReader SDR = cmdSelect.ExecuteReader();
bool hasRecord = false;
string BgColor = "";
while (SDR.Read())//
{
htmlBodyA.Append("<tr><td>" + SDR["ZAH"] + "</td><td>" + SDR["GCMC"] + "</td></tr>");
if (BgColor != "")
{
BgColor = "";
}
else
{
BgColor = "#AAAAAA";
}
hasRecord = true;
}
htmlBodyA.Append("</table>");
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("<Table Border=1><th colspan=5 bgcolor=#AAAAAA>"+DateTime.Now.ToString("yyyyMMdd")+"新建專案</th><tr bgcolor=#BBBB00><td>專案號碼</td><td>工程名稱</td><td>倉庫</td><td>庫位號碼</td><td>產品處利潤中心</td></tr>");
SqlDataReader SDR = cmdSelect.ExecuteReader();
bool hasRecord = false;
string BgColor = "";
while (SDR.Read())//
{
htmlBodyB.Append("<tr><td>" + SDR["ZAH"] + "</td><td>" + SDR["GCMC"] + "</td><td>" + SDR["KW"] + "</td><td>" + SDR["KWH"] + "</td><td>" + SDR["CPLRZX"] + "</td></tr>");
if (BgColor != "")
{
BgColor = "";
}
else
{
BgColor = "#AAAAAA";
}
hasRecord = true;
}
htmlBodyB.Append("</table>");
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("cheng.cheng@delta.com.cn", "582191"); //要发送邮件的帐号信息,如果要发到外网这一行就不能注释
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new MailMessage("softadmin@delta.com.cn", 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("cheng.cheng@delta.com.cn", "To:" + toAdress + "<br>" + e_sendmail.Message + ":<br>" + e_sendmail.StackTrace + "<br>" + HtmlBody);//出现异常用E-mail告警
return false;
}
}
}
}