-
C#教程之一款域名监控小工具 Domain(IP)Watcher 实现
监控给定的域名,一旦域名连续30秒(这是默认值,可以在源码中修改)无法Ping通,立刻发邮件到指定邮箱,并发短信给站长
用后台线程循环每隔5秒钟Ping结定的域名,如果连接5次Ping不通则发送邮件到给定邮箱。发短信是通过其他途径实现的,现在最常用的有两种方式:
一是利用QQ邮箱的手机绑定功能实现收到邮件短信提示
二是用www.ifttt.com提供的gmail跟sms服务实现收到邮件发短信提示
本工具用的是第一种。
主要程序:
主要是后台线程ping域名的应用,界面上显示记录的是一个richtextbox控件,其他几个就不说了,代码如下:
窗体后台程序:
public partial class FormMain : Form, IFormWorkThread
{
private delegate void ControlDelegate(string record);
private WorkThread wtObj;
ControlDelegate cdObj;
public FormMain()
{
InitializeComponent();
cdObj = SetRecord;
}
private void FormMain_Load(object sender, EventArgs e)
{
}
private void btnBegin_Click(object sender, EventArgs e)
{
if (txtDomain.Text.Trim() == "")
{
MessageBox.Show("Domain(IP) can not be empty!");
return;
}
if (txtEmail.Text.Trim() == "")
{
MessageBox.Show("Email address can not be empty!");
return;
}
Regex regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
if (!regex.IsMatch(txtEmail.Text.Trim()))
{
MessageBox.Show("Email address error!");
return;
}
wtObj = new WorkThread(this, 0, 3, txtDomain.Text.Trim(), txtEmail.Text.Trim());
wtObj.StartShowResult();
}
private void SetRecord(string record)
{
rtxtRecord.AppendText(record);
if (rtxtRecord.Lines.Length > 5 * 60 * 24 * 7)
{
rtxtRecord.Clear();
}
}
public void ShowResult(string record)
{
if (rtxtRecord.InvokeRequired)
{
Invoke(cdObj, record);
}
else
{
SetRecord(record);
}
}
private void btnEnd_Click(object sender, EventArgs e)
{
if (wtObj != null)
{
wtObj.EndShowResult();
}
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (wtObj != null)
{
wtObj.EndShowResult();
}
}
private void tslblName_Click(object sender, EventArgs e)
{
OpenMyBlog();
}
private void OpenMyBlog()
{
var Info = new System.Diagnostics.ProcessStartInfo("http://artwl.cnblogs.com");
System.Diagnostics.Process.Start(Info);
}
}
IFormWorkThread接口:
public interface IFormWorkThread
{
void ShowResult(string record);
}
WorkThread代码:
public class WorkThread
{
private IFormWorkThread _formWorkThread;
private Thread thread;
private int count = 0, maxcount = 5;
private string domain = "";
private string emailaddress = "";
public WorkThread(IFormWorkThread formWorkThread,int count,int maxcount,string domain,string email)
{
this.count = count;
this.maxcount = maxcount;
this.domain = domain;
this.emailaddress = email;
_formWorkThread = formWorkThread;
}
private string Ping(string domainname)
{
Ping ping = new Ping();
PingReply pr = ping.Send(domainname);
if (pr.Status == IPStatus.Success)
{
count = count > 0 ? count - 1 : 0;
}
else
{
count++;
if (count > maxcount)
{
EmailHelp.SendEmail(emailaddress,
string.Format("【 {0} 】不能访问啦!",domainname),
string.Format("【 {0} 】不能访问啦!<br/><br/>【时间】{1}<br/><br/>【Ping】当前ping的结果为:{2}", domainname,DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), pr.Status),
true);
count = 0;
}
}
return "【Time】:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\t【Domain】:" + domainname + "\t【Status】:" + pr.Status.ToString() + "\t【RoundtripTime】:" + pr.RoundtripTime.ToString() + "\n";
}
public void Deal()
{
while (true)
{
Thread.Sleep(5000);
string result = Ping(domain);
_formWorkThread.ShowResult(result);
}
}
public void StartShowResult()
{
thread = new Thread(new ThreadStart(Deal));
thread.Name = "TimeThread";
thread.Start();
}
public void EndShowResult()
{
thread.Abort();
}
}
发邮件代码:
public class EmailHelp
{
public static void SendEmail(string[] emailAddress, string subject, string body, bool ishtml)
{
MailMessage msg = new MailMessage();
foreach (string o in emailAddress)
{
msg.To.Add(o);
}
msg.Subject = subject;//邮件标题
msg.Body = body;//邮件内容
msg.IsBodyHtml = ishtml;//是否是HTML邮件
Thread sendemail = new Thread(SendReplyEmail);
sendemail.Start(msg);
}
public static void SendEmail(string to, string subject, string body, bool ishtml)
{
SendEmail(new string[] { to }, subject, body, ishtml);
}
private static void SendEmail(MailMessage mailMessage)
{
mailMessage.From = new MailAddress("emailaddress", "网站检测邮件", System.Text.Encoding.UTF8);
mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
mailMessage.Priority = MailPriority.Normal;//邮件优先级
SmtpClient client = new SmtpClient();
//注册的邮箱和密码
client.Credentials = new System.Net.NetworkCredential("emailaddress", "password");
client.Port = 25;
client.Host = "smtp.qq.com";
client.EnableSsl = false;
try
{
client.Send(mailMessage);
}
catch (System.Net.Mail.SmtpException ex)
{
//log.Error(ex.Message);
}
}
private static void SendReplyEmail(object model)
{
SendEmail((MailMessage)model);
}
}
注:发邮件的代码中emailaddress跟password要换成你自己的邮件地址跟密码。另外下载的exe文件为保证可用,我用的邮箱地址跟密码没有去掉,请大家也不要反编译后查看(这只是一个coding时的测试邮箱,没什么价值,赚不到钱的)。
原理:
用后台线程循环每隔5秒钟Ping结定的域名,如果连接5次Ping不通则发送邮件到给定邮箱。发短信是通过其他途径实现的,现在最常用的有两种方式:
一是利用QQ邮箱的手机绑定功能实现收到邮件短信提示
二是用www.ifttt.com提供的gmail跟sms服务实现收到邮件发短信提示
本工具用的是第一种。
主要程序:
主要是后台线程ping域名的应用,界面上显示记录的是一个richtextbox控件,其他几个就不说了,代码如下:
窗体后台程序:
复制代码 代码如下:
public partial class FormMain : Form, IFormWorkThread
{
private delegate void ControlDelegate(string record);
private WorkThread wtObj;
ControlDelegate cdObj;
public FormMain()
{
InitializeComponent();
cdObj = SetRecord;
}
private void FormMain_Load(object sender, EventArgs e)
{
}
private void btnBegin_Click(object sender, EventArgs e)
{
if (txtDomain.Text.Trim() == "")
{
MessageBox.Show("Domain(IP) can not be empty!");
return;
}
if (txtEmail.Text.Trim() == "")
{
MessageBox.Show("Email address can not be empty!");
return;
}
Regex regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
if (!regex.IsMatch(txtEmail.Text.Trim()))
{
MessageBox.Show("Email address error!");
return;
}
wtObj = new WorkThread(this, 0, 3, txtDomain.Text.Trim(), txtEmail.Text.Trim());
wtObj.StartShowResult();
}
private void SetRecord(string record)
{
rtxtRecord.AppendText(record);
if (rtxtRecord.Lines.Length > 5 * 60 * 24 * 7)
{
rtxtRecord.Clear();
}
}
public void ShowResult(string record)
{
if (rtxtRecord.InvokeRequired)
{
Invoke(cdObj, record);
}
else
{
SetRecord(record);
}
}
private void btnEnd_Click(object sender, EventArgs e)
{
if (wtObj != null)
{
wtObj.EndShowResult();
}
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (wtObj != null)
{
wtObj.EndShowResult();
}
}
private void tslblName_Click(object sender, EventArgs e)
{
OpenMyBlog();
}
private void OpenMyBlog()
{
var Info = new System.Diagnostics.ProcessStartInfo("http://artwl.cnblogs.com");
System.Diagnostics.Process.Start(Info);
}
}
IFormWorkThread接口:
复制代码 代码如下:
public interface IFormWorkThread
{
void ShowResult(string record);
}
WorkThread代码:
复制代码 代码如下:
public class WorkThread
{
private IFormWorkThread _formWorkThread;
private Thread thread;
private int count = 0, maxcount = 5;
private string domain = "";
private string emailaddress = "";
public WorkThread(IFormWorkThread formWorkThread,int count,int maxcount,string domain,string email)
{
this.count = count;
this.maxcount = maxcount;
this.domain = domain;
this.emailaddress = email;
_formWorkThread = formWorkThread;
}
private string Ping(string domainname)
{
Ping ping = new Ping();
PingReply pr = ping.Send(domainname);
if (pr.Status == IPStatus.Success)
{
count = count > 0 ? count - 1 : 0;
}
else
{
count++;
if (count > maxcount)
{
EmailHelp.SendEmail(emailaddress,
string.Format("【 {0} 】不能访问啦!",domainname),
string.Format("【 {0} 】不能访问啦!<br/><br/>【时间】{1}<br/><br/>【Ping】当前ping的结果为:{2}", domainname,DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), pr.Status),
true);
count = 0;
}
}
return "【Time】:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\t【Domain】:" + domainname + "\t【Status】:" + pr.Status.ToString() + "\t【RoundtripTime】:" + pr.RoundtripTime.ToString() + "\n";
}
public void Deal()
{
while (true)
{
Thread.Sleep(5000);
string result = Ping(domain);
_formWorkThread.ShowResult(result);
}
}
public void StartShowResult()
{
thread = new Thread(new ThreadStart(Deal));
thread.Name = "TimeThread";
thread.Start();
}
public void EndShowResult()
{
thread.Abort();
}
}
发邮件代码:
复制代码 代码如下:
public class EmailHelp
{
public static void SendEmail(string[] emailAddress, string subject, string body, bool ishtml)
{
MailMessage msg = new MailMessage();
foreach (string o in emailAddress)
{
msg.To.Add(o);
}
msg.Subject = subject;//邮件标题
msg.Body = body;//邮件内容
msg.IsBodyHtml = ishtml;//是否是HTML邮件
Thread sendemail = new Thread(SendReplyEmail);
sendemail.Start(msg);
}
public static void SendEmail(string to, string subject, string body, bool ishtml)
{
SendEmail(new string[] { to }, subject, body, ishtml);
}
private static void SendEmail(MailMessage mailMessage)
{
mailMessage.From = new MailAddress("emailaddress", "网站检测邮件", System.Text.Encoding.UTF8);
mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
mailMessage.Priority = MailPriority.Normal;//邮件优先级
SmtpClient client = new SmtpClient();
//注册的邮箱和密码
client.Credentials = new System.Net.NetworkCredential("emailaddress", "password");
client.Port = 25;
client.Host = "smtp.qq.com";
client.EnableSsl = false;
try
{
client.Send(mailMessage);
}
catch (System.Net.Mail.SmtpException ex)
{
//log.Error(ex.Message);
}
}
private static void SendReplyEmail(object model)
{
SendEmail((MailMessage)model);
}
}
注:发邮件的代码中emailaddress跟password要换成你自己的邮件地址跟密码。另外下载的exe文件为保证可用,我用的邮箱地址跟密码没有去掉,请大家也不要反编译后查看(这只是一个coding时的测试邮箱,没什么价值,赚不到钱的)。
最新更新
Objective-C语法之代码块(block)的使用
VB.NET eBook
Add-in and Automation Development In VB.NET 2003 (F
Add-in and Automation Development In VB.NET 2003 (8
Add-in and Automation Development in VB.NET 2003 (6
Add-in and Automation Development In VB.NET 2003 (5
AddIn Automation Development In VB.NET 2003 (4)
AddIn And Automation Development In VB.NET 2003 (2)
Addin and Automation Development In VB.NET 2003 (3)
AddIn And Automation Development In VB.NET 2003 (1)
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
武装你的WEBAPI-OData入门
武装你的WEBAPI-OData便捷查询
武装你的WEBAPI-OData分页查询
武装你的WEBAPI-OData资源更新Delta
5. 武装你的WEBAPI-OData使用Endpoint 05-09
武装你的WEBAPI-OData之API版本管理
武装你的WEBAPI-OData常见问题
武装你的WEBAPI-OData聚合查询
OData WebAPI实践-OData与EDM
OData WebAPI实践-Non-EDM模式