TPop3

Fuente: source/classes/tpop3.prg

TPop3 is FiveWin's email retrieval class, implementing the POP3 protocol to receive messages from a mail server. It handles the full POP3 session lifecycle: connect, authenticate, list messages, retrieve content, optionally delete messages from the server, and quit. Like TSmtp, it is built on top of TSocket.

Protocol Flow

sequenceDiagram participant App as FiveWin App participant POP3 as TPop3 participant Server as POP3 Server App->>POP3: GetMail() POP3->>Server: USER username Server-->>POP3: +OK POP3->>Server: PASS password Server-->>POP3: +OK POP3->>Server: STAT Server-->>POP3: +OK count size loop For each message POP3->>Server: RETR n Server-->>POP3: [message content] POP3->>Server: DELE n (if lDelMsgs=.T.) Server-->>POP3: +OK end POP3->>Server: QUIT Server-->>POP3: +OK

Key DATA Members

DATATypeDefaultDescription
cIPServerCharacterPOP3 server hostname or IP address
nPortNumeric110POP3 server port
UserCharacterUsername for authentication
PassCharacterPassword for authentication
aMsgsArrayArray of retrieved email messages (raw POP3 format)
nAtMsgNumericIndex of the message currently being retrieved
lDelMsgsLogical.T.Delete messages from server after retrieval
lHeaderOnlyLogical.F.Retrieve only email headers (using TOP command)
AllOkLogical.F.Set to .T. when all operations complete successfully
bConnectingBlockCallback while connecting
bConnectedBlockCallback when connected and authenticated
bDoneBlockCallback when all messages have been retrieved

Methods

MethodDescription
New( cIPServer, nPort, User, Pass )Create a new POP3 session. nPort defaults to 110.
GetMail()Connect to the server and begin the mail retrieval sequence. Messages are stored in aMsgs[].

Example: Check Inbox

#include "FiveWin.ch"

function Main()

   local oPop3, n

   oPop3 := TPop3():New( "mail.example.com", 110, ;
                         "user@example.com", "password" )

   oPop3:bConnected := { || MsgInfo( "Connected! Messages: " + ;
                                     LTrim( Str( Len( oPop3:aMsgs ) ) ) ) }

   oPop3:bDone := { ||
      MsgInfo( "Retrieved " + LTrim( Str( Len( oPop3:aMsgs ) ) ) + ;
               " messages." )
   }

   oPop3:GetMail()

   // Wait for async operation to complete
   while ! oPop3:AllOk
      SysRefresh()
   end

   // Display messages
   for n := 1 to Len( oPop3:aMsgs )
      ? "Message", n, ":", SubStr( oPop3:aMsgs[ n ], 1, 200 )
   next

return nil

Example: Retrieve Headers Only

#include "FiveWin.ch"

function Main()

   local oPop3, n

   oPop3 := TPop3():New( "mail.example.com", 110, ;
                         "user@example.com", "password" )

   // Only retrieve headers (faster, less bandwidth)
   oPop3:lHeaderOnly := .T.

   // Keep messages on the server
   oPop3:lDelMsgs := .F.

   oPop3:GetMail()

   while ! oPop3:AllOk
      SysRefresh()
   end

   // Display headers
   for n := 1 to Len( oPop3:aMsgs )
      ? "--- Message", n, "---"
      ? SubStr( oPop3:aMsgs[ n ], 1, 500 )
   next

return nil

Notes

Ver También