Read POP3 Mail using Winsock

Working pop3Email class is based on the code from http://fox.wikis.com/wc.dll?Wiki~GetPopEmail by William Steinford.

Sample code that shows how to use the class is included below it.

It will only run on PC with VFP installed. See Appropriate license for this class not found error at runtime

for details.
In order to run it w/o VFP installed, create a new form, drop MS Winsock ActiveX control on it and save as Winsock class in Winsock.vcx. After that change the Init of the class as follows

1
2
3
4
5
6
    PROCEDURE INIT 
    THIS.oWsform = NEWOBJECT("Winsock", "Winsock") 
    THIS.oSocket = THIS.oWsform.Olecontrol1.Object
    *THIS.oSocket = CREATEOBJECT("mswinsock.winsock") 
    ENDPROC 

Pop3Email class

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

* pop3EmailClass.prg

#DEFINE crlf CHR(13) + CHR(10) 
#DEFINE CONNECTION_CHECK 0 
#DEFINE USER_CHECK 1 
#DEFINE PASSWORD_CHECK 2 
#DEFINE QUIT_CHECK 3 
#DEFINE DELETE_CHECK 4 
#DEFINE RSET_CHECK 5 
#DEFINE STAT_CHECK 6 
#DEFINE NOOP_CHECK 7 
#DEFINE LIST_CHECK 8 
#DEFINE RETR_CHECK 9 
#DEFINE vbString 8 
#DEFINE TIME_OUT 5 

DEFINE CLASS Pop3Mail AS CUSTOM 
    cErrorMessage = "" 
    cPassword = "" 
    cUser = "" 
 
    cServerPort = 110 
    cServerName = "" 
 
    cMsgContents = "" 
    nTotalMailSize = 0 
    nNumberOfEmails = 0 
 
    lConnected = .F. 
    nConnectTimeOut = 5 
 
    cServerResponse = "" 
    oWsform = .NULL.
    oSocket = .NULL. 
    DIMENSION aSizeOfMsg[1] 
 
 
    PROCEDURE INIT 
    *THIS.oWsform = NEWOBJECT("Winsock", "Winsock") 
    *THIS.oSocket = THIS.oWsform.Olecontrol1.Object
    THIS.oSocket = CREATEOBJECT("mswinsock.winsock") 
    ENDPROC 
 
    FUNCTION Connect(tcServerName, tcUser, tcPassword) 
    LOCAL lnTime, lConnected 
    THIS.cServerName = ALLTRIM( IIF( VARTYPE(tcServerName)="C", tcServerName, THIS.cServerName ) ) 
    THIS.cUser = ALLTRIM( IIF( VARTYPE(tcUser)="C", tcUser, THIS.cUser ) ) 
    THIS.cPassword = IIF( VARTYPE(tcPassword)="C", tcPassword, THIS.cPassword ) 
 
    This.lConnected = .F. 
    THIS.oSocket.CONNECT(THIS.cServerName, THIS.cServerPort) && 110 Pop3 Port 
    lnTime = SECONDS() 
    DO WHILE THIS.oSocket.State <> 7 
        * ?"Waiting to connect..." 
        INKEY(0.01) 
        * ?"state="+tran(THIS.oSocket.State) 
        IF This.Elapsed(SECONDS(), lnTime) > This.nConnectTimeOut 
            THIS.cErrorMessage = "Time out connecting to the server " + THIS.cServerName 
            RETURN .F. 
        ENDIF 
    ENDDO 
    This.lConnected = .T. 
    * ?"State="+tran(THIS.oSocket.State) 
    IF NOT THIS.CheckResponse(CONNECTION_CHECK) 
        RETURN .F. 
    ENDIF 
 
    IF NOT THIS.SendMessageOk( "USER " + This.cUser, User_CHECK ) 
        RETURN .F. 
    ENDIF 
    IF NOT THIS.SendMessageOk( "PASS " + This.cPassword, Password_CHECK ) 
        RETURN .F. 
    ENDIF 
    RETURN .T. 
    *--------------------------------------------------- 
 
    FUNCTION DELETE(tnMsgNumber) 
    RETURN THIS.SendMessageOk( "DELE " + TRANSFORM(tnMsgNumber), DELETE_CHECK ) 
    *--------------------------------------------------- 
 
    FUNCTION NOOP 
    RETURN THIS.SendMessageOk( "NOOP ", NOOP_CHECK ) 
    *--------------------------------------------------- 
    * Return the Msg Size for given message number 
    FUNCTION GetMessageSize(tnMsgNumber) 
    IF ALEN(THIS.aSizeOfMsg) <= tnMsgNumber 
        RETURN 0 
    ENDIF 
    RETURN THIS.aSizeOfMsg[tnMsgNumber+1] 
    *--------------------------------------------------- 
 
    FUNCTION RESET() 
    RETURN THIS.SendMessageOk( "RSET ", RSET_CHECK ) 
    *--------------------------------------------------- 
    * cMsgContents will hold the message body 
    FUNCTION Retrieve(tnMsgNumber) 
    RETURN THIS.SendMessageOk( "RETR " + TRANSFORM(tnMsgNumber), RETR_CHECK ) 
    *--------------------------------------------------- 
 
    FUNCTION Statistics() 
    RETURN THIS.SendMessageOk( "STAT ", STAT_CHECK ) 
    *--------------------------------------------------- 
 
    FUNCTION ListMessages 
    RETURN THIS.SendMessageOk( "LIST ", LIST_CHECK ) 
    *--------------------------------------------------- 
 
    FUNCTION Disconnect() 
    RETURN THIS.SendMessageOk( "QUIT ", QUIT_CHECK ) 
    *--------------------------------------------------- 
 
    FUNCTION SendMessageOk( tcMsg, tnType ) 
    THIS.oSocket.SendData(tcMsg+crlf) 
    RETURN THIS.CheckResponse(tnType) 
    *--------------------------------------------------- 
 
    FUNCTION CheckResponse(ResponseType) 
    LOCAL lcBuf, laList, lnI, lcSamp, lnLine 
    lcBuf = THIS.ReadData() 
    THIS.cServerResponse = lcBuf 
    DO CASE 
    CASE ResponseType = CONNECTION_CHECK 
        IF lcBuf="-ERR" 
            THIS.cErrorMessage = "Bad Connection" 
            RETURN .F. 
        ENDIF 


    CASE ResponseType = User_CHECK 
        IF lcBuf="-ERR" 
            cErrorMessage = "Bad User Name" 
            RETURN .F. 
        ENDIF 
    CASE ResponseType = Password_CHECK 
        IF lcBuf="-ERR" 
            THIS.cErrorMessage = "Bad Password Name" 
            RETURN .F. 
        ENDIF 
    CASE ResponseType = QUIT_CHECK 
        IF lcBuf="-ERR" 
            THIS.cErrorMessage = "Error occurred during QUIT" 
            RETURN .F. 
        ENDIF 
    CASE ResponseType = DELETE_CHECK 
        IF lcBuf="-ERR" 
            THIS.cErrorMessage = "Error occurred during DELE" 
            RETURN .F. 
        ENDIF 
    CASE ResponseType = RSET_CHECK 
        IF lcBuf="-ERR" 
            THIS.cErrorMessage = "Error occurred during RSET" 
            RETURN .F. 
        ENDIF 
    CASE ResponseType = STAT_CHECK 
        IF lcBuf="-ERR" 
            THIS.cErrorMessage = "Error occurred during STAT" 
            RETURN .F. 
        ENDIF 
 
        DIMENSION laList[1] 
        IF ALINES(laList, lcBuf,.T., SPACE(1)) <> 3 
            THIS.cErrorMessage = "Error occurred during STAT" 
            RETURN .F. 
        ENDIF 
        THIS.nNumberOfEmails = INT(VAL(laList[2])) 
        THIS.nTotalMailSize  = INT(VAL(laList[3])) 
 
    CASE ResponseType = NOOP_CHECK 
        IF lcBuf="-ERR" 
            THIS.cErrorMessage = "Error occurred during NOOP" 
            RETURN .F. 
        ENDIF 
    CASE ResponseType = LIST_CHECK 
        IF lcBuf="-ERR" 
            THIS.cErrorMessage = "Error occurred during LIST" 
            RETURN .F. 
        ENDIF 
 
        lnI = 1 
        lcSamp = SUBSTR(lcBuf,lnI,1) 
        lnLine = 1 
        DO WHILE lcSamp<>"." AND lnI<1000 && "\0" 
            IF lcSamp=CHR(13) 
                lnLine = lnLine + 1 
            ENDIF 
            IF lnLine > 1 
                IF (lcSamp=CHR(9) OR lcSamp=" ") && *p == "\t" || *p == " ") 
                    DIMENSION THIS.aSizeOfMsg[ ALEN(THIS.aSizeOfMsg)+1 ] 
                    THIS.aSizeOfMsg[ALEN(THIS.aSizeOfMsg)] = VAL(SUBSTR(lcBuf,lnI)) 
                    *? " Email #"+TRAN(ALEN(THIS.aSizeOfMsg)-1)+" size="+TRAN(THIS.aSizeOfMsg[alen(THIS.aSizeOfMsg)]) 
                ENDIF 
            ENDIF 
            lnI = lnI + 1 
            lcSamp = SUBSTR(lcBuf,lnI,1) 
        ENDDO 
        THIS.nNumberOfEmails = ALEN(THIS.aSizeOfMsg)-1 
 
    CASE ResponseType = RETR_CHECK 
        IF lcBuf="-ERR" 
            THIS.cErrorMessage = "Error occurred during RETR" 
            RETURN .F. 
        ELSE 
            THIS.cMsgContents = lcBuf + THIS.ReadData() 
        ENDIF 
    ENDCASE 
    RETURN .T. 
    *--------------------------------------------------- 
 
    FUNCTION ReadData 
    LOCAL lcMsgIn, lnTime, lcBuffer 
    lnTime = SECONDS() 
    DO WHILE THIS.oSocket.BytesReceived = 0 
        * ?"Waiting to Receive data..." 
        INKEY(0.2) 
        *Sleep(100) 
        IF This.Elapsed(SECONDS(), lnTime) > This.nConnectTimeOut 
            * ?"Timed Out" 
            RETURN "" 
        ENDIF 
        DOEVENTS 
    ENDDO 
    lcBuffer = REPL(CHR(0),10000) && chr(0),10000) 
    * ?"(in) Bytes Received: "+tran(THIS.oSocket.BytesReceived) 
    * THIS.oSocket.Receive(lcBuf, len(lcBuf)) 
 
    DOEVENTS 
 
    lcMsgIn = "" 
    IF THIS.oSocket.State = 7 
        DO WHILE THIS.oSocket.BytesReceived > 0 
            THIS.oSocket.GETDATA(@lcBuffer,vbString) 
            lcMsgIn =  lcMsgIn + lcBuffer 
            INKEY(0.1) 
        ENDDO 
    ENDIF 
    * lcMsgIn = LEFT( lcMsgIn, AT(chr(0),lcMsgIn) ) 
    * ?"(in) Data Read: ("+tran(len(lcMsgIn))+","+tran(THIS.oSocket.BytesReceived)+" bytes) "+lcMsgIn 
    RETURN lcMsgIn 
    *--------------------------------------------------- 
 
    PROCEDURE DESTROY 
    DODEFAULT() 
    IF THIS.lConnected 
        THIS.Disconnect() 
    ENDIF 
    THIS.oWsform = NULL
    
    *--------------------------------------------------- 

    FUNCTION Elapsed 
	LPARAMETERS tnENdSeconds, tnStartSeconds  
	LOCAL lnElapsed 
	lnElapsed = tnENdSeconds - tnStartSeconds  
	DO WHILE (lnElapsed < 0) 
	    lnElapsed = lnElapsed + 86400       && 1day = 24h*60m*60s 
	ENDDO 
	RETURN lnElapsed        

ENDDEFINE 

Sample code that shows how to use class to retrieve email

This is sample code. Add error handling and adjust to your requirements as necessary.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
LOCAL loMail
lcCrLf =  CHR(13) + CHR(10)
loMail = NEWOBJECT("pop3mail", "pop3EmailClass.fxp")
* Change configuration data to your POP3 server
loMail.cServerName = "mail.whatever.com"
loMail.cUser = "john"
loMail.cPassword = "secret"

lcTempPath = ADDBS(SYS(2023))
lcFilePrefix = "Mail"
lcFileSuffix = SYS(2015)
lcFileExt = ".txt"

IF NOT loMail.Connect()
	? "Error Connecting:" + loMail.cErrorMessage
	RETURN .F.
ENDIF

IF NOT loMail.Statistics()
	? "STAT failed"
	RETURN .F.
ENDIF

IF loMail.nNumberOfEmails = 0
	? "No Emails to download"
	RETURN .T.
ENDIF

IF loMail.nNumberOfEmails > 1
	? "Total # of Emails: " + TRANSFORM(loMail.nNumberOfEmails)
	? "Total Size of Emails: " + TRANSFORM(loMail.nTotalMailSize)
ENDIF

FOR lnI = 1 TO loMail.nNumberOfEmails
	IF loMail.nNumberOfEmails > 1
		? "Downloading Email # " + TRANSFORM(lnI)
	ENDIF
	=  loMail.Retrieve(lnI)
	lcEmail = loMail.cMsgContents
	IF lcEmail = "+OK" AND RIGHT(lcEmail, 5) = lcCrLf + "." + lcCrLf
		lnPos = AT(lcCrLf, lcEmail)
		lcEmail = SUBSTR(lcEmail, lnPos+2, LEN(lcEmail) - (lnPos+6))
		lcTempFileName = lcTempPath + lcFilePrefix + TRANSFORM(lnI) + lcFileSuffix + lcFileExt
		* Write email to file
		STRTOFILE(lcEmail + lcCrLf, lcTempFileName )
		* Process email here
		*...
		* Delete email from server
		*IF llDeleteMail AND NOT loMail.Delete(lnI)
		*    ? "Couldn't delete email.")
		*ENDIF

	ELSE
		? "Email " + TRANSFORM(lnI) +  + " wasn't downloaded correctly from POP3."
	ENDIF
ENDFOR

loMail = NULL

Comments

I used the example to download messages from Gmail, using "pop.gmail.com"
I believe it passed thru the .Connect() functionality but Returned "STAT FAILED" on .Statistics()

Could you help!

Regards

Stan

It's not going to work because Gmail POP3 Server requires SSL

how we can receive mail from gmail with this code.

Warm regards,
mk.

Hi
I want to save file attachments by Winstock !
Help me please
VanXeNguyen

Hi Sergey,
Why those code not working for Gmail or Hotmail? what the ISP should I used?

Hi,

Winsock not working with SSL mail server. The are no option for this so Winsock can not connect directly to Mail server with SSL option. You should simulate the full security part by yourself... :-( Better to use some other way.