Public Class Pop3
Inherits System.Net.Sockets.TcpClient
Public Overloads Sub Connect(ByVal server As String, ByVal username As String, ByVal password As String)
Dim message As String
Dim response1 As String
Dim x As New Net.Sockets.TcpClient()
x.Connect(server, 110)
response1 = Response()
If (response1.Substring(0, 3) <> "+OK") Then
Dim Pop3Ex As Pop3Exception
Pop3Ex.Pop3Exception(response1)
End If
message = "USER " + username + vbCrLf
Write(message)
response1 = Response()
If (response1.Substring(0, 3) <> "+OK") Then
Dim Pop3Ex As Pop3Exception
Pop3Ex.Pop3Exception(response1)
End If
message = "PASS " + password + vbCrLf
Write(message)
response1 = Response()
If (response1.Substring(0, 3) <> "+OK") Then
Dim Pop3Ex As Pop3Exception
Pop3Ex.Pop3Exception(response1)
End If
End Sub
Public Sub Disconnect()
Dim message As String
Dim response1 As String
message = "QUIT" & vbCrLf
Write(message)
response1 = Response()
If (response1.Substring(0, 3) <> "+OK") Then
Dim Pop3Ex As Pop3Exception
Pop3Ex.Pop3Exception(response1)
End If
End Sub
Public Function List() As ArrayList
Dim message As String
Dim response1 As String
Dim retval As New ArrayList()
message = "LIST" & vbCrLf
Write(message)
response1 = Response()
If (response1.Substring(0, 3) <> "+OK") Then
Dim Pop3Ex As Pop3Exception
Pop3Ex.Pop3Exception(response1)
End If
Do While (True)
response1 = Response()
If (response1 = "." & vbCrLf) Then
Return retval
Else
Dim msg = New Pop3Message()
Dim seps As Char = "' '"
Dim values() As String = response1.Split(seps)
msg.number = Int32.Parse(values(0))
msg.bytes = Int32.Parse(values(1))
msg.retrieved = False
retval.Add(msg)
'continue()
End If
Loop
End Function
Public Function Retrieve(ByVal rhs As Pop3Message) As Pop3Message
Dim message As String
Dim response1 As String
Dim msg = New Pop3Message()
msg.bytes = rhs.bytes
msg.number = rhs.number
message = "RETR " + rhs.number + vbCrLf
Write(message)
response1 = Response()
If (response1.Substring(0, 3) <> "+OK") Then
Dim Pop3Ex As Pop3Exception
Pop3Ex.Pop3Exception(response1)
End If
msg.retrieved = True
Do While (True)
response1 = Response()
If (response1 = "." & vbCrLf) Then
Exit Do
Else
msg.message += Response()
End If
Loop
Return msg
End Function
Public Sub Delete(ByVal rhs As Pop3Message)
Dim message As String
Dim response1 As String
message = "DELE " + rhs.number + vbCrLf
Write(message)
response1 = Response()
If (response1.Substring(0, 3) <> "+OK") Then
Dim Pop3Ex As Pop3Exception
Pop3Ex.Pop3Exception(response1)
End If
End Sub
Private Sub Write(ByVal message As String)
Dim en = New System.Text.ASCIIEncoding()
Dim WriteBuffer(1024) As Byte
WriteBuffer = en.GetBytes(message)
Dim stream As Net.Sockets.NetworkStream
stream.Write(WriteBuffer, 0, WriteBuffer.Length)
Debug.WriteLine("WRITE:" + message)
End Sub
Private Function Response() As String
Dim enc = New System.Text.ASCIIEncoding()
Dim serverbuff(1024) As Byte
' **********************
' ERROR OCCURS ON THIS LINE
Dim stream As Net.Sockets.NetworkStream = GetStream()
Dim count As Integer = 0
Do While (True)
Dim buff(2) As Byte
Dim bytes As Integer = stream.Read(buff, 0, 1)
If (bytes = 1) Then
serverbuff(count) = buff(0)
count += 1
If (buff(0) = vbLf) Then
Exit Do
End If
Else
Exit Do
End If
Loop
Dim retval As String = enc.GetString(serverbuff, 0, count)
Debug.WriteLine("READ:" + retval)
Return retval
End Function
End Class