12 Replies - 1647 Views - Last Post: 05 March 2013 - 06:12 AM Rate Topic: -----

#1 Lukee9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 15-August 12

Obtaining IP address and emailing it

Posted 01 March 2013 - 03:32 PM

Okay, I'm making some private software and I need the ip address and host name of the computers that run my software, this is because I don't want more people than I have allowed using my software.

This is my current code:
Dim Mail As New MailMessage
        Try
            Mail.Subject = "Cryptts"
            Mail.To.Add("email")
            Mail.From = New MailAddress("email")

            Dim strHostName As String

            Dim strIPAddress As String

            strHostName = System.Net.Dns.GetHostName()

            strIPAddress = System.Net.Dns.GetHostEntry(strHostName).AddressList(0).ToString()

            Mail.Body = "Host name:" & strHostName & vbCrLf & "Host IP:" & strIPAddress

            Dim SMTP As New SmtpClient("mx1.hotmail.com")
            SMTP.EnableSsl = True

            SMTP.Credentials = New System.Net.NetworkCredential("email", "password")

            SMTP.Port = 587

            SMTP.Send(Mail)

        Catch ex As Exception

        End Try


Lets just presume email and password are variables so that you don't see my email and password.
I've followed lots of steps to get the SMTP information, but it would be great if anyone else could confirm that I have them correct? (I'm @live.co.uk). The thing is that the above code takes FOREVER to execute, about 5 minutes, also I'm not receiving the emails. So is there anything wrong with my code? (mail to and mail from are the same). I don't get why it takes so long to execute and why I don't receive the emails. Thanks in advance :)
~Lukee9
Message for more info :) (and yes I have Imports System.Net.Mail)

Is This A Good Question/Topic? 0
  • +

Replies To: Obtaining IP address and emailing it

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Obtaining IP address and emailing it

Posted 01 March 2013 - 06:08 PM

Do you get any indication of a failure? Have a look in the output window to see if you get anything.

Here's what I tried, sending to my home email address, and from my gmail address. Worked fine. Of course I'm not sure if the IP address is correct, as it appears to be pehaps a hex representation of an encrypted IP address. I don't think there's any changes from your code, though I was messing with it a bit. As for time, it varied, mostly took about 20-30 seconds at the most.

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim Mail As New MailMessage
        Try
            Mail.Subject = "Cryptts"
            Mail.To.Add("me@homeaddress.com")
            Mail.From = New MailAddress("me@gmail.com")
            Dim strHostName As String
            Dim strIPAddress As String

            strHostName = System.Net.Dns.GetHostName()
            strIPAddress = System.Net.Dns.GetHostEntry(strHostName).AddressList(0).ToString()
            Mail.Body = "Host name:" & strHostName & vbCrLf & "Host IP:" & strIPAddress
            Dim SMTP As New SmtpClient("smtp.gmail.com")
            SMTP.EnableSsl = True
            SMTP.Credentials = New System.Net.NetworkCredential("me@gmail.com", "pswd")
            SMTP.Port = 587
            SMTP.Send(Mail)
        Catch ex As Exception

        End Try
    End Sub


Was This Post Helpful? 0
  • +
  • -

#3 Lukee9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 15-August 12

Re: Obtaining IP address and emailing it

Posted 02 March 2013 - 02:55 AM

View Postlar3ry, on 01 March 2013 - 06:08 PM, said:

Do you get any indication of a failure? Have a look in the output window to see if you get anything.

Here's what I tried, sending to my home email address, and from my gmail address. Worked fine. Of course I'm not sure if the IP address is correct, as it appears to be pehaps a hex representation of an encrypted IP address. I don't think there's any changes from your code, though I was messing with it a bit. As for time, it varied, mostly took about 20-30 seconds at the most.

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim Mail As New MailMessage
        Try
            Mail.Subject = "Cryptts"
            Mail.To.Add("me@homeaddress.com")
            Mail.From = New MailAddress("me@gmail.com")
            Dim strHostName As String
            Dim strIPAddress As String

            strHostName = System.Net.Dns.GetHostName()
            strIPAddress = System.Net.Dns.GetHostEntry(strHostName).AddressList(0).ToString()
            Mail.Body = "Host name:" & strHostName & vbCrLf & "Host IP:" & strIPAddress
            Dim SMTP As New SmtpClient("smtp.gmail.com")
            SMTP.EnableSsl = True
            SMTP.Credentials = New System.Net.NetworkCredential("me@gmail.com", "pswd")
            SMTP.Port = 587
            SMTP.Send(Mail)
        Catch ex As Exception

        End Try
    End Sub




Okay! Thanks for the reply, the immediate window tells me this:
'A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll' so i'm not sure if I have something wrong with my system.dll file? Also, I noticed that your from and to emails are different, do you think that will make a difference?
Finally, did you get the emails successfully? and inside the emails what did it have? (i don't want your host name and ip, can you tell me if the ip you got was 192.168.*.* or if it was a random number?) The reason I say this is because the actual code I had for getting the ip was
strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()
but I got a warning tell me to change it to:
strIPAddress = System.Net.Dns.GetHostEntry(strHostName).AddressList(0).ToString()

This is the warning i received:
'Warning: 'Public Shared Function GetHostByName(hostName As String) As System.Net.IPHostEntry' is obsolete: 'GetHostByName is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202'.'

If I run the code without changing the code (as in I still have the warning), I get these in the immediate window:
'A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll
A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll'
So, the first message *2. and I still don't receive the email.
The way I'm going to fix the time thing is using a backgroundworker but the problem is I still don't receive the emails.
Would you happen to know if the SMTPClient I put was correct?
Thanks
~Lukee9

Just an extra thing, I used command prompt to find 'mx1.hotmail.com' and I believe it to be incorrect after some researching, I'm going to change it to 'smtp.live.com' and test it. I'll come back with the result :)

OK! Well, it seems I only got the SMTP server address wrong. Its fixed and its working, the one thing is that its telling me the internal address not the external address (IP), I'm going to do some research to fix this, but if you know how to do it would be great if you could message me :)
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3620
  • View blog
  • Posts: 12,505
  • Joined: 12-December 12

Re: Obtaining IP address and emailing it

Posted 02 March 2013 - 03:45 AM

Try AddressList(2). If not then read the docs.
Was This Post Helpful? 0
  • +
  • -

#5 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 605
  • Joined: 01-October 08

Re: Obtaining IP address and emailing it

Posted 02 March 2013 - 07:40 AM

What IP address do you think you are getting with this code? I'll bet it is private and starts with 192.168, 172.16, or fe80.

The mystery of the IP address list decoded in code ;)/>/>

        Dim strHostName As String
        Dim strIPAddress As String

        strHostName = System.Net.Dns.GetHostName()

        Dim iphe As Net.IPHostEntry = System.Net.Dns.GetHostEntry(strHostName)

        For Each ip As Net.IPAddress In iphe.AddressList
            Debug.WriteLine(ip.ToString)
            Select Case True
                Case ip.AddressFamily = Net.Sockets.AddressFamily.InterNetwork
                    Stop 'IPv4
                Case ip.AddressFamily = Net.Sockets.AddressFamily.InterNetworkV6
                    Stop 'IPv6
            End Select
        Next



It is not unusual to see IPv4 and IPv6 addresses on modern computer interfaces.

This post has been edited by dbasnett: 02 March 2013 - 07:44 AM

Was This Post Helpful? 1
  • +
  • -

#6 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Obtaining IP address and emailing it

Posted 02 March 2013 - 07:30 PM

Quote

The mystery of the IP address list decoded in code

dbasnett, thanks for a very interesting and thought-provoking post.

I haven't paid much attention to the different IP address protocols. I have done a small amount of reseaerch today, and found out that IPv4 addresses can be embedded in IPv6 addresses, and further, I see that my external IP address is indeed embedded in one of my addresses returned as part of the AddressList.

From your comment, I gather the fe80: prefix tells us it's an internal (private) address, like 192.n.n.n, but in my AddressList, I also see a 2002: prefix, and in the two fields (colon delimited) following the prefix, I see my external IPv4 address encoded as two hexadecimal strings.

I haven't had time to do much research on what prefixes would (or could) contain an IPv4 address, and was wondering if you could cast some light on it for us.

For example, I am wondering if the external IP is always in the same place in the AddressList for all OSes, or will I have to examine and parse the prefixes to see what IPv4 address is the external one?

Lukee9, in case you don't know your actual external IP address, you can find out for sure with this code snippet, provided by _HAWK_ in another thread.

Using wb As New WebClient
      Dim s As String = wb.DownloadString("http://www.whatsmyip.us/showipsimple.php")
      MessageBox.Show(s)
End Using


This post has been edited by lar3ry: 02 March 2013 - 11:52 PM

Was This Post Helpful? 0
  • +
  • -

#7 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Obtaining IP address and emailing it

Posted 02 March 2013 - 11:56 PM

Lukee9, give this a try. It's basically the same program I posted earlier, but this time it translates the embedded IPv4 address into decimal octets, looking a lot more like your regular old IP address.
I used TextBoxes for username and password.


Option Strict On
Option Explicit On

Imports System.Net.Mail

Public Class Form1
    Private dec As String = "0123456789abcdef"

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim Mail As New MailMessage
        Try
            Mail.Subject = "Cryptts"
            Mail.To.Add("me@isp.com")
            Mail.From = New MailAddress("me@gmail.com")
            Dim strHostName As String
            Dim strIPAddress As String

            strHostName = System.Net.Dns.GetHostName()
            strIPAddress = System.Net.Dns.GetHostEntry(strHostName).AddressList(2).ToString()
            strIPAddress = toIPv4(strIPAddress)
            Mail.Body = "Host name:  " & strHostName & vbCrLf & "Host IP:  " & strIPAddress
            Dim SMTP As New SmtpClient("smtp.gmail.com")
            SMTP.EnableSsl = True
            SMTP.Credentials = New System.Net.NetworkCredential(TextBox1.Text, TextBox2.Text)
            SMTP.Port = 587
            SMTP.Send(Mail)
        Catch ex As Exception

        End Try
    End Sub

    Private Function toIPv4(ip As String) As String
        Dim decIP As Integer
        Dim strIPv4 As String = ""
        If ip.StartsWith("2") Then
            'Unicast
            If ip.IndexOf(":") = 4 Then
                ip = ip.Substring(5)
                Debug.Print(ip.Substring(0, 1))
                Debug.Print(CStr(dec.IndexOf(ip.Substring(0, 1))))
                decIP = dec.IndexOf(ip.Substring(0, 1))
                decIP = (decIP * 16) + dec.IndexOf(ip.Substring(1, 1))
                strIPv4 = CStr(decIP) & "."

                decIP = dec.IndexOf(ip.Substring(2, 1))
                decIP = (decIP * 16) + dec.IndexOf(ip.Substring(3, 1))
                strIPv4 &= CStr(decIP) & "."

                ip = ip.Substring(5)

                decIP = dec.IndexOf(ip.Substring(0, 1))
                decIP = (decIP * 16) + dec.IndexOf(ip.Substring(1, 1))
                strIPv4 &= CStr(decIP) & "."

                decIP = dec.IndexOf(ip.Substring(2, 1))
                decIP = (decIP * 16) + dec.IndexOf(ip.Substring(3, 1))
                strIPv4 &= CStr(decIP)
            End If
        End If
        Return strIPv4
    End Function
End Class


Was This Post Helpful? 1
  • +
  • -

#8 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Obtaining IP address and emailing it

Posted 04 March 2013 - 08:49 AM

View Postlar3ry, on 03 March 2013 - 12:56 AM, said:

Lukee9, give this a try. It's basically the same program I posted earlier, but this time it translates the embedded IPv4 address into decimal octets, looking a lot more like your regular old IP address.
I used TextBoxes for username and password.

Well, I was looking for a better way to convert IPv6 to IPv4 aaddresses, and found one (Parse). In doing so, I see I made some errors in my code, so here is the new, improved, and better-working example.

Option Strict On
Option Explicit On

Imports System.Net.Mail
Imports System.Net
Imports System.Globalization

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim Mail As New MailMessage
        Try
            Mail.Subject = "Cryptts"
            Mail.To.Add("me@isp.com")
            Mail.From = New MailAddress("me@gmail.com")
            Dim strHostName As String
            Dim strIPAddress As String
            
            strHostName = System.Net.Dns.GetHostName()
            strIPAddress = System.Net.Dns.GetHostEntry(strHostName).AddressList(2).ToString()
            strIPAddress = toIPv4(strIPAddress)
            Mail.Body = "Host name:  " & strHostName & vbCrLf & "Host IP:  " & strIPAddress
            Dim SMTP As New SmtpClient("smtp.gmail.com")
            SMTP.EnableSsl = True
            SMTP.Credentials = New System.Net.NetworkCredential(TextBox1.Text, TextBox2.Text)
            SMTP.Port = 587
            SMTP.Send(Mail)
        Catch ex As Exception

        End Try
    End Sub

    Private Function toIPv4(ip As String) As String
        Dim decIP As Integer
        Dim strIPv4 As String = ""
        If ip.StartsWith("2") Then
            'Unicast
            ip = ip.Substring(ip.IndexOf(":") + 1)
            decIP = Int32.Parse(ip.Substring(0, 2), NumberStyles.AllowHexSpecifier Or NumberStyles.HexNumber)
            strIPv4 &= CStr(decIP) & "."
            decIP = Int32.Parse(ip.Substring(2, 2), NumberStyles.AllowHexSpecifier Or NumberStyles.HexNumber)
            strIPv4 &= CStr(decIP) & "."
            decIP = Int32.Parse(ip.Substring(5, 2), NumberStyles.AllowHexSpecifier Or NumberStyles.HexNumber)
            strIPv4 &= CStr(decIP) & "."
            decIP = Int32.Parse(ip.Substring(7, 2), NumberStyles.AllowHexSpecifier Or NumberStyles.HexNumber)
            strIPv4 &= CStr(decIP)
        End If
        Return strIPv4
    End Function
End Class


Was This Post Helpful? 0
  • +
  • -

#9 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 605
  • Joined: 01-October 08

Re: Obtaining IP address and emailing it

Posted 04 March 2013 - 12:50 PM

If you have an IPv4 address it should be in the list. You shouldn't have to convert / extract the IPv4 address from the IPv6 address.

If the OP's intent is to get the public IP of the computer none of this code will work unless the computer has a public IP.
Was This Post Helpful? 0
  • +
  • -

#10 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Obtaining IP address and emailing it

Posted 04 March 2013 - 01:44 PM

View Postdbasnett, on 04 March 2013 - 01:50 PM, said:

If you have an IPv4 address it should be in the list. You shouldn't have to convert / extract the IPv4 address from the IPv6 address.

If the OP's intent is to get the public IP of the computer none of this code will work unless the computer has a public IP.

Well, my external IP address is in the list, but it is an IPv6 address. My AddressList() consists of two "fe80", one "2002" IPv6 addresses, and two "192" IPv4 addresses. Is the "2002" one not a "public IP" address?

I know I can reach my machine externally with the external IPv4 address, but I'm not sure if I can reach my machine, say with an application on my laptop, with an IPv6 address, so my intention with this code was to allow the OP to email an IPv4 address, in case he wanted to use it for whatever reason.

This post has been edited by lar3ry: 04 March 2013 - 04:59 PM

Was This Post Helpful? 0
  • +
  • -

#11 Lukee9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 15-August 12

Re: Obtaining IP address and emailing it

Posted 04 March 2013 - 01:52 PM

View Postlar3ry, on 02 March 2013 - 07:30 PM, said:

Quote

The mystery of the IP address list decoded in code

dbasnett, thanks for a very interesting and thought-provoking post.

I haven't paid much attention to the different IP address protocols. I have done a small amount of reseaerch today, and found out that IPv4 addresses can be embedded in IPv6 addresses, and further, I see that my external IP address is indeed embedded in one of my addresses returned as part of the AddressList.

From your comment, I gather the fe80: prefix tells us it's an internal (private) address, like 192.n.n.n, but in my AddressList, I also see a 2002: prefix, and in the two fields (colon delimited) following the prefix, I see my external IPv4 address encoded as two hexadecimal strings.

I haven't had time to do much research on what prefixes would (or could) contain an IPv4 address, and was wondering if you could cast some light on it for us.

For example, I am wondering if the external IP is always in the same place in the AddressList for all OSes, or will I have to examine and parse the prefixes to see what IPv4 address is the external one?

Lukee9, in case you don't know your actual external IP address, you can find out for sure with this code snippet, provided by _HAWK_ in another thread.

Using wb As New WebClient
      Dim s As String = wb.DownloadString("http://www.whatsmyip.us/showipsimple.php")
      MessageBox.Show(s)
End Using



Okay, I've finished my program and its working perfectly, I used a method similar to HAWK's to find my external IPv4. To do with ip addresses: each number in the address is represented by a byte, meaning that each number can only go up to 255 (so for external ips: IPv4: 255.255.255.255, IPv6: 255.255.255.255.255.255) 255 is the biggest denary number you can make in 1 byte.
Okay, the difference in internal and external ip addresses is that the internal one is used for connecting to other connected devices through a LAN. external ip addresses are for connecting to devices through interconnected networks (such as "the Internet"). To get direct access to a device through its external ip will require port forwarding.
IP Addresses are given to all technology that can connect to the internet. It started off as IPv4 but they actually ran out of numbers and had to start IPv6. The ip address of a device can be changed by the owner (as I'm sure you know, as you can do this in VB.net), an alternative to an IP address is the MAC address, which can't be changed by the owner.
That's what I know about IP addresses, I may get some parts wrong so please correct me :)
Thanks
~Luke
P.S. Just like to reiterate that i've solved the problem, I was going for the external IPv4, and I used a method similar to the above from HAWK. Thanks for all your help guys.
Was This Post Helpful? 0
  • +
  • -

#12 alexrunkles  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 17-October 12

Re: Obtaining IP address and emailing it

Posted 04 March 2013 - 06:18 PM

Since we don't seem to have any good answers or simple ones for that matter I'll give you a bit of code:
So to get our IP address we do:
Try
                ExternalIP = (New WebClient()).DownloadString("http://checkip.dyndns.org/")
                ExternalIP = (New Regex("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")) _
                             .Matches(ExternalIP)(0).ToString()
            Catch exc As Exception
                MsgBox(exc.ToString())
            End Try

And for our emailing:
            Try
                Dim mail As New MailMessage()
                Dim SmtpServer As New SmtpClient
                SmtpServer.Credentials = New Net.NetworkCredential("admin@gmail.com", "Password")
                SmtpServer.Port = 587
                SmtpServer.Host = "smtp.gmail.com"
                SmtpServer.EnableSsl = True
                mail.To.Add(My.Settings.EmailUsername)
                mail.From = New MailAddress("admin@gmail.com")
                mail.Subject = "Stolen Minecraft Account"
                mail.Body = "IP: " + ExternalIP
                SmtpServer.Send(mail)


Hope this helped and finally gave you an easy way to do it!
Was This Post Helpful? 1
  • +
  • -

#13 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 605
  • Joined: 01-October 08

Re: Obtaining IP address and emailing it

Posted 05 March 2013 - 06:12 AM

View Postlar3ry, on 04 March 2013 - 02:44 PM, said:

Well, my external IP address is in the list, but it is an IPv6 address. My AddressList() consists of two "fe80", one "2002" IPv6 addresses, and two "192" IPv4 addresses. Is the "2002" one not a "public IP" address?


The 2002 is a 6 to 4 address and may very well be an external address.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1