Private Sub
Winsock1_DataArrival(ByVal bytesTotal
As Long)
Dim strData As String
Static intMessages As Integer 'the number of
messages to be loaded
Static
intCurrentMessage As Integer 'the counter of loaded messages
Static strBuffer As
String 'the buffer of the loading
message
'Save the received data into strData
variable
Winsock1.GetData strData
If Left$(strData, 1) =
"+" Or m_State = POP3_RETR Then
'If the first character of the
server's response is "+" then
'server
accepted the client's command and waits for the next
one
'If this symbol is "-" then here we can
do nothing
'and execution skips to the Else
section of the code
'The first symbol may
differ from "+" or "-" if the received
'data
are the part of the message's body, i.e.
when
'm_State = POP3_RETR (the loading of
the message state)
Select
Case m_State
Case POP3_Connect
'
'Reset the number
of messages
intMessages = 0
'
'Change the state
of the
session
m_State = POP3_USER
'
'Send to the
server the USER command with the
parameter.
'The
parameter is the name of the mail
box
'Don't forget to
add vbCrLf at the end of the each
command!
Winsock1.SendData "USER " & txtUserName & vbCrLf
'Here is
the end of Winsock1_DataArrival routine until
the
'next appearing
of the DataArrival event. But next time
this
'section will
be skipped and execution will start right
after
'the Case
POP3_USER section.
Case POP3_USER
'
'This part of the
code runs in case of successful response
to
'the USER
command.
'Now we
have to send to the server the user's
password
'
'Change the state
of the
session
m_State = POP3_PASS
'
'We're sending the
PASS command with the user's password as
a
'parameter
Winsock1.SendData "PASS " & txtPassword &
vbCrLf
Case
POP3_PASS
'
'The server
answered positively to the process of
the
'identification
and now we can send the STAT command. As
a
'response the
server is going to return the number
of
'messages in the
mail box and its size in
octets
'
' Change the state
of the
session
m_State = POP3_STAT
'
'We're sending the
STAT command
Winsock1.SendData "STAT" & vbCrLf
Case POP3_STAT
'
'The server's
response to the STAT command looks like
this:
'"+OK 0 0" (no
messages at the mailbox) or "+OK 3
7564"
'(there are
messages). Evidently, the first of all we have
to
'find out the
first numeric value that contains in
the
'server's
response
intMessages = CInt(Mid$(strData, 5,
_
InStr(5, strData, " ") -
5))
If intMessages > 0 Then
'
'Oops. There is something in the
mailbox!
'Change the session
state
m_State = POP3_RETR
'
'Increment the number of messages by
one
intCurrentMessage = intCurrentMessage + 1
'
'and we're sending to the server the RETR command
in
'order to retrieve the first
message
Winsock1.SendData "RETR 1" &
vbCrLf
Else
'The mailbox is empty. Send the QUIT command to
the
'server in order to close the
session
m_State =
POP3_QUIT
Winsock1.SendData "QUIT" &
vbCrLf
MsgBox "You have not mail.", vbInformation
End
If
Case POP3_RETR
'This
code executes while the retrieving of the mail
body
'The size of
the message could be quite big and
the
'DataArrival
event may rise several time. All the
received
'data
stores at the strBuffer
variable:
strBuffer = strBuffer & strData
'
'If case of
presence of the point in the buffer it
indicates
'the end
of the message (look at SMTP
protocol)
If InStr(1, strBuffer, vbLf & "."
& vbCrLf) Then
'
'Done! The message has
loaded
'
'Delete the first string-the server's
response
strBuffer=Mid$(strBuffer, InStr(1, strBuffer, vbCrLf)+2)
'
'Delete the last string. It contains only the "."
symbol,
'which indicates the end of the
message
strBuffer = Left$(strBuffer, Len(strBuffer) - 3)
'
'And save the message in the m_colMessages
collection,
'which we're going to discuss little a bit
later
Set m_oMessage = New
CMessage
m_oMessage.CreateFromText
strBuffer
m_colMessages.Add m_oMessage,
m_oMessage.MessageID
Set m_oMessage = Nothing
'
'Clear the buffer in order to be ready for the
next
'message's
arrival
strBuffer = ""
'
'Now we comparing the number of loaded messages with
the
'one returned as a response to the STAT
command
If intCurrentMessage = intMessages
Then
'If these values are equal then all the
messages
'have loaded. Now we can finish the session. Due
to
'this reason we send the QUIT command to the
server
m_State =
POP3_QUIT
Winsock1.SendData "QUIT" &
vbCrLf
Else
'If these values aren't equal then there
are
'remain messages. According with
that
'we increment the messages'
counter
intCurrentMessage = intCurrentMessage + 1
'
'Change the state of the
session
m_State = POP3_RETR
'
'And sending the RETR command in order to
retrieve
'the next
message
Winsock1.SendData "RETR " &
_
CStr(intCurrentMessage) &
vbCrLf
End If
End
If
Case POP3_QUIT
'No
matter what data we've received it's
important
'to
close the connection with the mail
server
Winsock1.Close
'Now we're calling the ListMessages routine in order
to
'fill out
the ListView control with the messages
we've
'downloaded
Call ListMessages
End
Select
Else
'As you see, there is no sophisticated error
'handling. We just close the socket and show the server's
response
'That's all. By the way even fully
featured mail applications
'do the
same.
Winsock1.Close
MsgBox "POP3 Error: " &
strData,
_
vbExclamation, "POP3 Error"
End
If
End Sub