C# Program to Alert IP Change of Broadband Router
SummaryC# Program that monitors router IP address and sends email alert if IP changes.
OverviewRecently, my O2 broadband was sold to Sky Media. After the switchover, the IP address of the broadband router keeps changing every couple of days. Sky broadband does not supply static IP address.
I have my email server running on my own server together with a few web sites. Whenvever the IP changes, I need to update the DNS records, otherwise, these sites cannot work. While I am looking for a broadband provider that offers static IP address, I need to find a way to notify me if the IP changes.
To do this, I wrote a C# program that can:
1. Check every 5 minutes if my IP address has changed
2. If the IP address has changed, send an email alert to my gmail account
3. The email will contain the new IP address
3. Once I got the notification, I log onto the DNS server and ammend DNS records
Checking the IP Address of the Broadband Router
using System.Net; string ipAddress = new WebClient().DownloadString("http://icanhazip.com");
Sending an E-mail
using System.Net.mail; static void SendEmail(string ipaddress) { MailMessage objecto_mail = new MailMessage(); SmtpClient client = new SmtpClient(); client.Port = 25; client.Host = smtp_address; client.Timeout = 10000; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential(smtp_user, smtp_pass); objecto_mail.From = new MailAddress(email_from); objecto_mail.To.Add(new MailAddress(email_to)); objecto_mail.Subject = "IP Change !!!"; objecto_mail.Body = "New IP: " + ipaddress; client.Send(objecto_mail); }
Download
Program source code: Program.cs
Program executable(zip): ChckIP.zip
Program Input
- MTP server address - Address of your ISP provider's email gateway, for example, smtp.google.com
- SMTP account name - Your SMTP account name. Your ISP can provide this and the above.
- SMTP account password - Your SMTP account password.
- From e-mail address - This is your email address with your ISP provider or any e-mail address.
- To e-mail address - This is the email address to receive email alert, for exammple, your gmail. This should not be an address hosted by your local server.