VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > C/C++语言编程 >
  • 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时的测试邮箱,没什么价值,赚不到钱的)。
相关教程