Managing Dialup connection






2.84/5 (17 votes)
Mange your Internet connection .
Introduction
I am going to tell you in this artical about how you can access your PC from outside, using simple mobile Internet connection which is frequently disconnected. Normally our mobile GPRS connection gets Dynamic IP(which is frequently changed).
Now i want connect to my FTP floder located at my home PC. So that i created a application, this application automatically connected to the dial-up connection if it's disconnected, and send a mail to your mail id with the IP address.
Background
I taken some code from the code project for the network quality check.
I used "rasdial.exe" for the dial a dial up connection. located at ("c:\windows\system32\rasdial.exe")
Using the code
Before using this code user have set some modification in their connections, code.
ISP Entry Name: Please take care about the ISP entry name, This name don't put spaces and other special characters.
And also remove the redial option for the dialup connection properties....
SMTP mail : Change the mail address/password in the code. for sender mail ID. this mail id must be enable with the POP3,SMTP Out .
#Region "Imports" Imports System Imports System.IO Imports System.Net Imports System.Net.Mail #End Region Public Class Manage #Region "Declarations" ''' <summary> ''' Process Delaration , used for the run external command (rasdial.exe) ''' </summary> Public MyProcess As New System.Diagnostics.Process ''' <summary> ''' Holds the Current IP address ''' </summary> Public PresentIPAddress As String = "" ''' <summary> ''' Quality of the connection (String Type) ''' </summary> Public Conn_Qulity As String = "Bad" ''' <summary> ''' windows API function for Internet Connection type and connection quality. ''' </summary> Private Declare Function InternetGetConnectedState Lib _ "wininet.dll" (ByRef lpSFlags As Int32, _ ByVal dwReserved As Int32) As Boolean ''' <summary> ''' enum used for the internet connection type. ''' </summary> Public Enum InetConnState modem = &H1 lan = &H2 proxy = &H4 ras = &H10 offline = &H20 configured = &H40 End Enum #End Region #Region "Network Handlers/Methods" ''' <summary> ''' Network Connection Handler. ''' </summary> Private Sub Handle_NetworkAvailabilityChanged() AddHandler My.Computer.Network.NetworkAvailabilityChanged, _ AddressOf MyComputerNetwork_NetworkAvailabilityChanged DisplayAvailability(My.Computer.Network.IsAvailable) End Sub ''' <summary> ''' Network Availability changing Handler, ''' </summary> ''' <remarks> ''' *** Problems witn when two network connctions are present. ''' </remarks> Private Sub MyComputerNetwork_NetworkAvailabilityChanged( _ ByVal sender As Object, _ ByVal e As Devices.NetworkAvailableEventArgs) DisplayAvailability(e.IsNetworkAvailable) End Sub ''' <summary> ''' Display availability methods ''' </summary> ''' <param name="available"> ''' True/False ''' </param> Private Sub DisplayAvailability(ByVal available As Boolean) Application.DoEvents() If available = True Then '' Verifiying for Genuine Connection Try Application.DoEvents() MyProcess.StartInfo.FileName = "c:\myip.bat" MyProcess.StartInfo.Arguments = "" MyProcess.StartInfo.UseShellExecute = False MyProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal 'Process.GetCurrentProcess() Application.DoEvents() MyProcess.Start() MyProcess.WaitForExit() MyProcess.Close() 'System.Diagnostics.Process.Start("c:\myip.bat", "") Application.DoEvents() Delay(5000) Dim line As String = "" Using sr As StreamReader = New StreamReader("c:\MYIP.txt") line = sr.ReadToEnd() End Using If InStr(UCase(line), UCase(txtISPEntry.Text)) <= 0 Then lblstatus.Text = "Connecting..." AppendStatus("Connecting...") lblstatus.Refresh() Delay(5000) Application.DoEvents() Call DisplayAvailability(False) End If Catch ex As Exception MsgBox(ex.Message) End Try lblstatus.Text = "Reading IP Address(s)." AppendStatus("Reading IP Address(s).") Dim hostName As String = System.Net.Dns.GetHostName() Delay(2000) lblstatus.Text = System.Net.Dns.GetHostByName(hostName).AddressList(0).ToString() AppendStatus("IP Address : " & System.Net.Dns.GetHostByName(hostName).AddressList(0).ToString()) PresentIPAddress = System.Net.Dns.GetHostByName(hostName).AddressList(0).ToString() Beep() Beep() Beep() lblstatus.Text = "Connected." AppendStatus("Connected.") lblstatus.Refresh() Check_Net_Conn_Type() Call Send_email() Else lblstatus.Text = "Disconnected." AppendStatus("Disconnected.") lblstatus.Refresh() lblstatus.Text = "Waiting For Connection..." AppendStatus("Waiting For Connection...") lblstatus.Refresh() Application.DoEvents() lblstatus.Text = "Connecting..." AppendStatus("Connecting...") lblstatus.Refresh() Try MyProcess.StartInfo.FileName = "rasdial.exe" If Trim(txtusername.Text) = "" Or Trim(txtpassword.Text) = "" Then MyProcess.StartInfo.Arguments = txtISPEntry.Text Else MyProcess.StartInfo.Arguments = txtISPEntry.Text & " " & txtusername.Text & " " & txtpassword.Text End If MyProcess.StartInfo.UseShellExecute = False MyProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal 'Process.GetCurrentProcess() MyProcess.Start() MyProcess.WaitForExit() MyProcess.Close() 'System.Diagnostics.Process.Start("rasdial.exe", TextBox5.Text) Catch ex As Exception MsgBox(ex.Message) End Try Try Application.DoEvents() MyProcess.StartInfo.FileName = "c:\myip.bat" MyProcess.StartInfo.Arguments = "" MyProcess.StartInfo.UseShellExecute = False MyProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal 'Process.GetCurrentProcess() Application.DoEvents() MyProcess.Start() MyProcess.WaitForExit() MyProcess.Close() 'System.Diagnostics.Process.Start("c:\myip.bat", "") Application.DoEvents() Delay(5000) Dim line As String = "" Using sr As StreamReader = New StreamReader("c:\MYIP.txt") line = sr.ReadToEnd() End Using If InStr(UCase(line), UCase(txtISPEntry.Text)) <= 0 Then lblstatus.Text = "Re-Connecting..." AppendStatus("Re-Connecting...") lblstatus.Refresh() Delay(5000) Application.DoEvents() Call DisplayAvailability(False) End If Catch ex As Exception MsgBox(ex.Message) End Try Beep() Beep() Beep() Application.DoEvents() End If End Sub ''' <summary> ''' Force disconnect/re-connect by the User. ''' </summary> Public Sub Re_Connect() Try MyProcess.StartInfo.FileName = "rasdial" MyProcess.StartInfo.Arguments = txtISPEntry.Text & " /disconnect" MyProcess.StartInfo.UseShellExecute = False MyProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal 'Process.GetCurrentProcess() MyProcess.Start() MyProcess.WaitForExit() MyProcess.Close() Delay(5000) Call DisplayAvailability(False) Catch ex As Exception MsgBox(ex.Message) End Try End Sub ''' <summary> ''' Creats bat file for every fresh run of the application. ''' </summary> Public Sub Create_MYIP_Batfile() Dim sr As New FileStream("c:\myip.bat", FileMode.Create) Dim wr As New StreamWriter(sr) wr.WriteLine("rasdial >c:\myip.txt") wr.Close() sr.Close() End Sub ''' <summary> ''' Chaking for Network connction type. ''' </summary> Function Check_Net_Conn_Type() As Boolean Dim NetFlags As Long If InternetGetConnectedState(NetFlags, 0) Then ' True If NetFlags And InetConnState.lan Then lblconntype.Text = "LAN." ElseIf NetFlags And InetConnState.modem Then lblconntype.Text = "Modem." ElseIf NetFlags And InetConnState.configured Then lblconntype.Text = "Configured." ElseIf NetFlags And InetConnState.proxy Then lblconntype.Text = "Proxy." ElseIf NetFlags And InetConnState.ras Then lblconntype.Text = "RAS." ElseIf NetFlags And InetConnState.offline Then lblconntype.Text = "Offline." End If Else lblconntype.Text = "Off" End If Call CheckInetConnection() End Function ''' <summary> ''' Checking for Neteork connction quality. ''' </summary> Function CheckInetConnection() As Boolean Dim lngFlags As Long If InternetGetConnectedState(lngFlags, 0) Then ' True If lngFlags And InetConnState.lan Then Select Case Conn_Qulity Case "Good" lblqlty.Text = " Good" Conn_Qulity = "Good" Case "Intermittent" lblqlty.Text = " Good" Conn_Qulity = "Good" Case "Bad" lblqlty.Text = " Intermittent" Conn_Qulity = "Intermittent" End Select Me.Refresh() ElseIf lngFlags And InetConnState.modem Then Select Case Conn_Qulity Case "Good" lblqlty.Text = " Good" Conn_Qulity = "Good" Case "Intermittent" lblqlty.Text = " Good" Conn_Qulity = "Good" Case "Bad" lblqlty.Text = " Intermittent" Conn_Qulity = "Intermittent" End Select ElseIf lngFlags And InetConnState.configured Then Select Case Conn_Qulity Case "Good" lblqlty.Text = " Good" Conn_Qulity = "Good" Case "Intermittent" lblqlty.Text = " Good" Conn_Qulity = "Good" Case "Bad" lblqlty.Text = " Intermittent" Conn_Qulity = "Intermittent" End Select ElseIf lngFlags And InetConnState.proxy Then Select Case Conn_Qulity Case "Good" lblqlty.Text = " Good" Conn_Qulity = "Good" Case "Intermittent" lblqlty.Text = " Good" Conn_Qulity = "Good" Case "Bad" lblqlty.Text = " Intermittent" Conn_Qulity = "Intermittent" End Select ElseIf lngFlags And InetConnState.ras Then Select Case Conn_Qulity Case "Good" lblqlty.Text = " Good" Conn_Qulity = "Good" Case "Intermittent" lblqlty.Text = " Good" Conn_Qulity = "Good" Case "Bad" lblqlty.Text = " Intermittent" Conn_Qulity = "Intermittent" End Select ElseIf lngFlags And InetConnState.offline Then Select Case Conn_Qulity Case "Good" lblqlty.Text = " Good" Conn_Qulity = "Good" Case "Intermittent" lblqlty.Text = " Good" Conn_Qulity = "Good" Case "Bad" lblqlty.Text = " Intermittent" Conn_Qulity = "Intermittent" End Select End If Else ' False Select Case Conn_Qulity Case "Good" lblqlty.Text = " Intermittent" Conn_Qulity = "Intermittent" Case "Intermittent" lblqlty.Text = " Bad" Conn_Qulity = "Bad" Case "Bad" lblqlty.Text = " Bad" Conn_Qulity = "Bad" End Select End If End Function ''' <summary> ''' Timer for the check network quality frequentkly(2000 milli seconds.) ''' </summary> Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick CheckInetConnection() Application.DoEvents() Me.Refresh() End Sub #End Region #Region "Buttons Code" ''' <summary> ''' Close Application. ''' </summary> Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Application.Exit() End Sub ''' <summary> ''' Starts Operations. ''' </summary> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Call Create_MYIP_Batfile() Call Handle_NetworkAvailabilityChanged() End Sub ''' <summary> ''' Force Reconnction Button. ''' </summary> Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Call AppendStatus("Force Disconnction By User.") Call Re_Connect() End Sub #End Region #Region "Functions" ''' <summary> ''' Delay Methods .., in this code i used Stopwatch for exact Delaytime. ''' </summary> Public Sub Delay(ByVal Delaytime As Object) Dim mystopwatch As New Stopwatch mystopwatch.Start() Dim mytime As Integer = mystopwatch.ElapsedMilliseconds Do If mystopwatch.ElapsedMilliseconds > (mytime + Delaytime) Then mystopwatch.Stop() Exit Do End If Loop End Sub ''' <summary> ''' Mail Sending ..... ''' </summary> Public Sub Send_email() If Not chkdntmail.Checked Then Dim innlop As Integer For innlop = 1 To CInt(txtmailretry.Text) Try Dim oMsg As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage() oMsg.From = New System.Net.Mail.MailAddress("rajeshcragy@yahoo.co.in", "Manage Dialup.") oMsg.To.Add(New MailAddress(txtemailaddrs.Text, "Manage Dialup.")) oMsg.Subject = "Your Internet Connection Info." oMsg.Body = " *** IP Address: " & PresentIPAddress & " [At: " & Now.ToString & " ]" & vbCrLf _ & " *** Connection Type: " & lblconntype.Text & vbCrLf _ & " *** Connection Quality: " & lblqlty.Text & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf _ & " Regards..," & vbCrLf _ & " Rajesh B.., -- A Poor Workman Blames His Tools. --" & vbCrLf _ & " e-mail: rajeshcragy@gmail.com, rajeshcragy@yahoo.co.in." & vbCrLf _ & " Ph: +919397099540" & vbCrLf _ & " Hyderabad, Andra Pradesh, India." & vbCrLf Dim oSmtp As New SmtpClient("smtp.mail.yahoo.com") oSmtp.DeliveryMethod = SmtpDeliveryMethod.Network Dim oCredential As New NetworkCredential("yourmailid", "yourpassword") oSmtp.UseDefaultCredentials = False oSmtp.Credentials = oCredential oSmtp.Send(oMsg) AppendStatus("Message Sent Successfully!") Exit For Catch ex As Exception AppendStatus(ex.Message) AppendStatus("Retrinig to Send Mail....") End Try Next End If End Sub ''' <summary> ''' Records all Status for further Refrrence. ''' </summary> Public Sub AppendStatus(ByVal MessageText As String) txtstatus.Items.Add(MessageText & " [At: " & Now.ToString & " ]") txtstatus.SelectedIndex = txtstatus.Items.Count - 1 End Sub #End Region End Class
In this code i covered.., Sending mail from vb.NET, checking the Internet connection quality.