Sending Commands and Receiving Responses

After a connection is established, commands can be sent to the email server via the Winsock control’s SendData method. The SendData method takes as an argument the text that is to be sent. This line, for instance, sends the SMTP MAIL command:

Winsock1.SendData "MAIL FROM: dg@execpc.com" & vbCrLf

Note the vbCrLf appended to the end of the string. All data sent to SMTP and POP3 servers must be terminated by the carriage return plus linefeed character combination.

As with the Connect method, the SendData method is asynchronous—it returns control to the Visual Basic program even before it gets confirmation that the command was successfully received by the server. When confirmation does arrive, the Winsock control notifies your Visual Basic application via the DataArrival event. You will typically handle this event by calling the Winsock control’s GetData event to retrieve the response and to act on it. This code fragment displays a message box whenever data is received from the server:

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
   Dim strData As String
   Winsock1.GetData strData, vbString
   MsgBox strData, vbOKOnly + vbInformation, "Data received from server"
End Sub ' Winsock1_DataArrival

The bytesTotal parameter of the DataArrival event procedure indicates the size of the data received from the server. The vbString constant passed to the GetData method requests that the data be given in string format rather than as an array of bytes. ...

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.