Making a POP3 Client
When you send some data, you have a keyword first, such as USER, then the value, and then a vbCrlf character. Below I have listed some of the commands used in the sample project:
- USER - Sends the username
- PASS - Sends the password
- QUIT - Ends the current session
- DELE - Deletes a mail message from the server
- RSET - Undos any changes made within the current session
- STAT - Returns the number of messages on the server
- RETR - Retrieves the content of a message
I won't go through what each one does because the descriptions are pretty self explanatory. Anyway, after sending a message, you need to listen for a reply. If everything is working, then you will receive a +OK message back from the server. If there is a problem, then the server will return a -ER message. Here is some code that sends a user id:
Winsock1.SendData "USER Sam" & vbCrlf
To get the response from the server, we need to look in the Winsock1_DataArrival event. This event does not actually provide us with any data, it just notifies us that data is arriving and how big it is. We need to use the Winsock1.GetData method to retrieve something:
Private Sub WInsock1_DataArrival(ByVal bytesTotal As Long) Dim strData As String Winsock1.GetData strData, vbString WholeThing = WholeThing & strData ResponseState = Left$(WholeThing, 3) End Sub
The GetData method accepts two parameters. The first is a variable where the data retrieved can be placed in and the second is the format in which to receive the data. In the example above, we fill the ResponseState variable with what will either be a +OK command or a -ER command. The best thing to do is to make the ResponseState variable a private/public member of the module so that the program can detect whether it is ok to proceed.
Page 3 of 6
This article was originally published on November 20, 2002