Imports System.Net.Sockets
Imports System.Text.ASCIIEncoding
Public Function Connetti(ByVal username As String, ByVal pass As String)
'Provo a connettermi
Dim client As New TcpClient
Dim NetStr As NetworkStream
'x vedere se va a buon fine
Dim S As String = ""
client.Connect("pop.gmail.it", 110)
netstr = client.GetStream()
'\\\\Conferma connessione
Dim A As Boolean = False
Dim mystring As String = Nothing
Do
Dim byteServer(client.ReceiveBufferSize) As Byte
If Not (client.ReceiveBufferSize = 0) Then
netstr.Read(byteServer, 0, byteServer.Length)
mystring = ASCII.GetString(byteServer)
A = True
End If
Loop Until A = True
A = False
'\\\\\Parte 1 (Username)
If mystring.StartsWith("+OK") Then
Dim bytes1() As Byte = ASCII.GetBytes("USER " + username + vbCrLf)
netstr.Write(bytes1, 0, bytes1.Length)
Else
Return S
client.Close()
MsgBox("Disconnesso")
Exit Function
End If
'\\\\\\Ricevi conferma username
Do
Dim byteServer(client.ReceiveBufferSize) As Byte
If Not (client.ReceiveBufferSize = 0) Then
netstr.Read(byteServer, 0, byteServer.Length)
mystring = ASCII.GetString(byteServer)
A = True
End If
Loop Until A = True
A = False
'\\\\\\Parte 2(Pass)
If mystring.StartsWith("+OK") Then
Dim bytes2() As Byte = System.Text.Encoding.ASCII.GetBytes("PASS " + pass + vbCrLf)
netstr.Write(bytes2, 0, bytes2.Length)
Else
Return S
client.Close()
MsgBox("Disconnesso")
Exit Function
End If
'\\\Ricevi conferma pass
Do
Dim byteServer(client.ReceiveBufferSize) As Byte
If Not (client.ReceiveBufferSize = 0) Then
netstr.Read(byteServer, 0, byteServer.Length)
mystring = ASCII.GetString(byteServer)
A = True
End If
Loop Until A = True
A = False
'\\\Parte 3(Elenco Messaggi)
If mystring.StartsWith("+OK") Then
Dim bytes3() As Byte = System.Text.Encoding.ASCII.GetBytes("list " + vbCrLf)
netstr.Write(bytes3, 0, bytes3.Length)
Else
Return S
client.Close()
MsgBox("Disconnesso")
Exit Function
End If
'\\\Ricevi elenco messaggi
mystring = ""
Do
Dim byteServer(client.ReceiveBufferSize) As Byte
If Not (client.ReceiveBufferSize = 0) Then
netstr.Read(byteServer, 0, byteServer.Length)
S = ASCII.GetString(byteServer)
A = True
End If
Loop Until A = True
A = False
Dim arr() As String
mystring = S.Replace("+OK POP3 clients that break here, they violate STD53." + vbCrLf, Nothing)
arr = mystring.Split(vbCrLf)
For Each I In arr
Dim h As String() = I.Split(" ")
If h(0).Contains(".") = True Or h(0) = " " Then
Exit For
End If
Dim x As Char() = h(0).ToCharArray()
Dim b As String = ""
For Each T In x
If (T = "0" Or T = "1" Or T = "2" Or T = "3" Or T = "4" Or T = "5" Or T = "6" Or T = "7" Or T = "8" Or T = "9") Then
b += T
End If
Next
MsgBox(b)
Messaggio(b, client, NetStr)
'elimina i messaggi
If b.Contains(" ") = False Then
Dim by() As Byte =ASCII.GetBytes("dele " + b + vbCrLf)
NetStr.Write(by, 0, by.Length)
End If
A = False
Do
Dim byteServer(client.ReceiveBufferSize) As Byte
If Not (client.ReceiveBufferSize = 0) Then
NetStr.Read(byteServer, 0, byteServer.Length)
mystring = ASCII.GetString(byteServer)
A = True
End If
Loop Until A = True
Next
A = False
'\\\Parte finale(Quit)
Dim bytes() As Byte = ASCII.GetBytes("quit" + vbCrLf)
NetStr.Write(bytes, 0, bytes.Length)
client.Close()
MsgBox("Disconnesso")
Return S
End Function
Private Sub Messaggio(ByVal I As String, ByVal client As TcpClient, ByVal netstr As NetworkStream)
If I.Contains(" ") = True Then
Exit Sub
End If
Dim S As String = ""
Dim A As Boolean = False
'\\\Parte 4(leggi messaggio)
Dim bytes() As Byte = ASCII.GetBytes("retr " + I + vbCrLf)
NetStr.Write(bytes, 0, bytes.Length)
Do
Dim byteServer(client.ReceiveBufferSize) As Byte
If Not (client.ReceiveBufferSize = 0) Then
NetStr.Read(byteServer, 0, byteServer.Length)
S = ASCII.GetString(byteServer)
A = True
End If
Loop Until A = True
A = False
Do
Dim byteServer(client.ReceiveBufferSize) As Byte
If Not (client.ReceiveBufferSize = 0) Then
NetStr.Read(byteServer, 0, byteServer.Length)
S = ASCII.GetString(byteServer)
A = True
End If
Loop Until A = True
A = False
TextBox1.Text = S
End Sub