Retrieving Email
To retrieve email from a server, the email client establishes a connection to the server on port 110 and then sends appropriate POP3 commands. Example A-3 shows how this is done, using the helper procedures introduced in Example A-1. This sample code logs onto a POP3 server and inquires whether any email is waiting. If there is, additional commands are sent to retrieve the first email, which is then displayed in a text box.
Example A-3. Retrieving an Email
Private Sub RetrieveAnEmail( ) Dim strResponse As String Dim nMsgCount As Long Dim nStart As Long Dim nLength As Long ' Connect to the email server. ConnectAndWait "mail.execpc.com", 110 ' Log in. SendAndWait "USER dg" SendAndWait "PASS MyPassword" ' Find out how many emails there are. strResponse = SendAndWait("STAT") nStart = InStr(strResponse, " ") + 1 nLength = InStr(nStart, strResponse, " ") - nStart nMsgCount = CLng(Mid(strResponse, nStart, nLength)) ' If there are any emails at all, retrieve the first one. If nMsgCount > 0 Then SendAndWait "RETR 1" txtMsg.Text = SendAndWait("NOOP") Else txtMsg.Text = "(There are no messages on the server.)" End If ' Close the connection. CloseConnection End Sub ' RetrieveAnEmail
The debug output generated by this example is shown in Example A-4.
Example A-4. A Sample POP3 Conversation
Client: (Initiates connection on port 110) Server: +OK POPROX (version 1.0) at pop05.execpc.com starting. Client: USER dg Server: +OK Password required for dg. Client: PASS MyPassword Server: ...
Get CDO & MAPI Programming with Visual Basic: now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.