A domain name monitoring gadget domain (IP) Watcher Implementation Code _c# tutorial

Source: Internet
Author: User
Tags mail code object model mailmessage smtpclient
Monitor the given domain name, once the domain name for 30 consecutive seconds (this is the default value, can be modified in the source code) could not ping, immediately send mail to the designated mailbox, and SMS to the Webmaster

principle:

Use a background thread to loop the domain name every 5 seconds, and if you connect 5 times ping does not send messages to a given mailbox. Texting is done in other ways, and there are two ways to use it now:

One is the use of QQ mailbox Phone binding function to achieve the message received SMS Tips

The second is to use the Gmail and SMS services provided by www.ifttt.com to send text message prompts

This tool is used in the first type.
Main program:
Mainly is the background thread ping domain name application, the interface displays the record is a RichTextBox control, other several does not say, the code is as follows:
Form Background Program:
Copy Code code as follows:

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 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 Interface:
Copy Code code as follows:

public interface Iformworkthread
{
void Showresult (string record);
}

Workthread Code:
Copy Code code as follows:

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}" cannot be accessed!) ", DomainName),
String. Format ("{0}" cannot be accessed!) <br/><br/> "Time" {1}<br/><br/> "ping" The result of the current ping is: {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 ();
}
}

Send mail code:
Copy Code code as follows:

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;//message Header
Msg. BODY = body;//message Content
Msg. isbodyhtml = ishtml;//is an HTML message
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", "Website Detection Mail", System.Text.Encoding.UTF8);
mailmessage.subjectencoding = system.text.encoding.utf8;//message header encoding
mailmessage.bodyencoding = system.text.encoding.utf8;//message content encoding
Mailmessage.priority = mailpriority.normal;//Message priority
SmtpClient client = new SmtpClient ();
Registered Mailboxes and Passwords
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);
}
}

Note: EmailAddress and password in the email code will be replaced with your own email address and password. In addition to download the EXE file to ensure that it is available, I use the email address and password has not been removed, please do not decompile the view (this is just a coding when the test mailbox, no value, not make money).
Tool Download Address Domain (IP) Watcher

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.