Private Sub POP3Demo(ByVal server As String, ByVal user As String, ByVal pass As String)
Dim client As Net.Sockets.TcpClient
Dim sr As IO.StreamReader
Dim sw As IO.StreamWriter
Dim ns As Net.Sockets.NetworkStream
Dim response As String
'connecting to a server
client = New Net.Sockets.TcpClient
Try
client.Connect(server, 110)
Catch e As Exception
MessageBox.Show("Failed to connect: " & e.ToString, "Error")
Return
End Try
ns = client.GetStream
sr = New IO.StreamReader(ns, Encoding.ASCII, True) ' this sends our commands to the server
sw = New IO.StreamWriter(ns) ' and this will read server's response
response = sr.ReadLine() ' reading the response
' At this point the response should contain a string +OK and some text after it.
' Also it sends CRLF at the end of each string
' We should send:
' user [username] & ControlChars.CrLf
' The server should respond:
' +OK [some text] - a user exists you may continue
' -ERR [some text] - either some error or user was not found
sw.WriteLine("USER " & user & ControlChars.CrLf)
sw.Flush()
response = sr.ReadLine()
If response.Substring(0, 3) = "-ER" Then
MessageBox.Show("Could not login", "Error")
Return
End If
' We then should send a password
' And the response is also either +OK or -ERR
' Sending password
sw.WriteLine("PASS " & pass & Chr(13) & Chr(10))
sw.Flush()
response = sr.ReadLine
If response.Substring(0, 3) = "-ER" Then
MessageBox.Show("Password is incorrect", "Error")
Return
End If
' We logged in then we should get the number of messages and their size
' We should send 'STAT'
' The response would be:
' +OK [number] [size]
Dim stats() As String
Dim totalmessages, totalsize As Integer
sw.WriteLine("STAT")
sw.Flush()
response = sr.ReadLine()
stats = response.Split(" ")
totalmessages = CInt(stats(1))
totalsize = CInt(stats(2))
' Now we should determine messages indices and the size of each message
' Send:
' LIST
' Response (example):
' +OK 2 messages 320
' 1 120
' 2 200
' The list is terminated by a new line and "." character
Dim i As Integer
Dim Messages() As String
' getting messages identifiers
sw.WriteLine("LIST")
sw.Flush()
response = sr.ReadLine()
stats = response.Split(" ")
ReDim Messages(totalmessages - 1)
For i = 0 To CInt(stats(1)) - 1
Messages(i) = (sr.ReadLine())
Next
' Now we can manipulate messages the following way
' To delete a message you should send DELE [x] where x is a message index (see above)
' The response is as usual either +OK [some text] (operation successful)
' or -ERR [some text] (error)
' This deletes message number 1 from the server (uncomment)
' sw.WriteLine("DELE 1")
' sw.Flush()
' response = sr.ReadLine()
' To retrieve (download) a message you should use RETR command
' Send (x is a message index):
' RETR [x]
' The message will be sent as plain text and will be terminated by a empty line and the "." character
' Again, possible answers are: +OK [some text] or -ERR [some text]
' For example:
' +OK 120 octets
' [message header]
' [message body]
' .
' Let's get all messages:
sr = New IO.StreamReader(ns, Encoding.Default, True)
For i = 0 To (totalmessages - 1)
sw.WriteLine("RETR " & (i + 1))
sw.Flush()
' We just dump the messages to the screen:
Do While response <> "."
response = sr.ReadLine
Console.WriteLine(response & Environment.NewLine)
Loop
Next
' Quit gracefully:
sw.WriteLine("QUIT") ' Disconnecting
' Closing the client
' client.Close
End Sub