My career in technology

ET Phone Home!

Do you have a system that you access remotely, but doesn’t have a static IP address?   Or maybe you need to remotely access a family member’s computer to help them troubleshoot a problem.  How can you make sure you have the current IP address? 

Here is a bit of C# code that can help. You can add the executable to your Startup or set a scheduled execution with Task Scheduler, or you could name the app “Get Help” and your family member can click it to get you the information without you having to walk them through the process of figuring out their IP address.

The code creates a console app that allows you to have your system discover the IP address that the world sees by calling to and then uses Gmail to send an e-mail containing the address to your selected recipient address.


using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Mail;

static void Main(string[] args)

        // used to build entire input
        StringBuilder sb = new StringBuilder();

        // used on each read operation
        byte[] buf = new byte[8192];

        // prepare the web page we will be asking for
        HttpWebRequest request = (HttpWebRequest)

        // execute the request
        HttpWebResponse response = (HttpWebResponse)

        // we will read data via the response stream
        Stream resStream = response.GetResponseStream();
        string tempString = null;
        int count = 0;
            // fill the buffer with data
            count = resStream.Read(buf, 0, buf.Length);

            // make sure we read some data
            if (count != 0)
                // translate from bytes to ASCII text
                tempString = Encoding.ASCII.GetString(buf, 0, count);

                // continue building the string
        while (count > 0); // any more data to read?

        //Send to e-mail

private static void SendToEmail(string emailstring)
            MailMessage mail = new MailMessage();

           //change the italicized text to the correct values
            mail.From = new MailAddress("<em>YourGmailAccount</em>");
            mail.Subject = "IP address"; //or your own choice of subject text
            mail.Body = emailstring; //this is the value passed from Main
            mail.IsBodyHtml = false;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("<em>YourGmailAccount</em>", "<em>YourPassword</em>");
            smtp.EnableSsl = true;
        catch(Exception ex)

This was put together using code from two sources:   .Net SMTPClient Send Through Gmail on my buddy Tom Resing’s SharePoint Blog and How To: Fetching Web Pages with HTTP by Joe Mayo at C# Station (Thanks, guys!).  I broke out the MailMessage and smtp parameters to make the code easier to fiddle with, so it’s a little less elegant-looking than Tom’s code.


(Want to add code to your WordPress blog?  Check here!)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: