Working with mail using POP3 protocol



POP3-CHECK: "hostname-or-IP" "pop3user" "password"

Returns the number of messages available in specified mailbox. Returns FALSE (0) if a specified mailbox is empty or not accessible using POP3 protocol.

There also exists a postfix version of this word:

S" hostname-or-ip" S" pop3user" S" password" POP3-CHECK

POP3-CONNECT ( a-host u1 a-name u2 a-pass u3 -- ior )

This postfix word establishes a connection with a POP3 mail server. Tthree string arguments are passed to the POP3-CONNECT word: server name, user name and password. The word POP3-CONNECT returns a result of an I/O operation (IOR). This is a flag value that conveys information about the type of failure, if any, from an operation with I/O device. There was no error if IOR = 0. To disconnect from the mail server use a special POP3-QUIT word.

Example:

#( test_pop3_connect
NoActive
Action:
    \ connecting to the POP3 server
    S" hostname" S" username" S" password" POP3-CONNECT 0=
    IF
        \ doing smth useful
        \ ...
        \ disconnecting from POP3 server
        POP3-QUIT THROW
    ELSE
        ERR-MSG: "Error when connecting to the POP3 server"
    THEN
)#

After you have successfully establish a connection to the POP3 mail server, you can use the following words to manage your mail:

Examples:

#( test_pop3_connect1
NoActive
Action:
    \ connecting to the POP3 server
    S" hostname" S" username" S" password" POP3-CONNECT 0=
    IF
        \ displaying the count of messages on a POP3 server
        MSG: "%POP3-STAT THROW%
        \ saving the numbers and size of all the messages to the c:\mail.txt file
        POP3-LIST S" c:\mail.txt" FWRITE
        \ displaying the text of the latest message on a console
        POP3-STAT THROW
        POP3-RETR THROW TYPE CR
        \ disconnecting from POP3 server
        POP3-QUIT THROW
    ELSE
        ERR-MSG: "Error when connecting to the POP3 server"
    THEN
)#

#( test_pop3_connect2
NoActive
Action:
    \ connecting to the POP3 server
    S" hostname" S" username" S" password" POP3-CONNECT 0=
    IF
\ displaying the numbers and size of all the messages on the console POP3-LIST THROW TYPE CR
\ disconnecting from POP3 server
POP3-QUIT THROW ELSE ERR-MSG: "Error when connecting to the POP3 server"
THEN )# #( test_pop3_connect3 NoActive Action: \ connecting to the POP3 server S" hostname" S" username" S" password" POP3-CONNECT 0= IF
\ displaying the numbers and size of all the messages on the console POP3-LIST THROW TYPE CR
\ getting the number of the messages on a POP3 server POP3-STAT THROW ?DUP
IF \ displaying the text of every message on a console in a loop 1+ 1 DO I POP3-RETR THROW TYPE CR \ deleting every message in a loop
I POP3-DELE THROW
LOOP
THEN \ disconnecting from POP3 server
POP3-QUIT THROW ELSE ERR-MSG: "Error when connecting to the POP3 server"
THEN )#

POP3-GET ( a u a1 u1 a2 u2 u3 -- a u )

This postfix word retreives the specified message from the POP3 mail server (by it's number) and returns a string containing the full text of the specified message (including the message header). There are four argements, which are passed to the POP3-GET word: three strings - he mail server name (or IP-address), user name and password and one number - the number of the message to retrieve.

Example:

#( test_get_mail 
NoActive 
Action: 
    \ getting the message #10
    S" mail.server.com" S" user" S" password" 10 POP3-GET 
    \ printing the message text to console
    TYPE CR
)#

#( test_get_mail1
NoActive 
Action: 
    \ getting the message #5
    S" mail.server.com" S" user" S" password" 5 POP3-GET 
    \ saving it to the file 'c:\get.mail'
    S" c:\get.mail" FWRITE 
)#

#( test_get_mail2
NoActive 
\ getting the latest message
0 VALUE number_of_messages 
Action: 
    \ getting the number of messages 
    S" mail.server.com" S" user" S" password" POP3-CHECK 
    \ saving the number of messages to VALUE-variable 
    TO number_of_messages 
    number_of_messages 0<>
    IF
        \ getting the latest message 
        S" mail.server.com" S" user" S" password" number_of_messages POP3-GET 
        \ saving it to the file 'c:\get.mail' 
        S" c:\get.mail" FWRITE 
    THEN
)#