Use TsmPOP3Client to collect new mail while leaving the originals in the mailbox. The sample asks the server for each message's persistent identifier and downloads the original message bytes only when your application needs them.
uses
System.SysUtils, StreamSec.Mail.POP3Client;
function NewPop3Reader(const aHost, aServerName, aCAFile, aUser: string; aPort: Word; aImplicitTLS: Boolean): TsmPOP3Client; begin Result := TsmPOP3Client.Create(nil); try Result.Host := aHost; Result.ServerName := aServerName; Result.Port := aPort; Result.ImplicitTLS := aImplicitTLS; Result.RequireTLS := True; if Result.TLSServer.LoadRootCertsFromFile(aCAFile) = 0 then raise ePOP3ClientError.CreateFmt('No trust anchors in %s', [aCAFile]); Result.UserName := aUser; except Result.Free; raise; end; end;
Use your provider's host and account name. For implicit TLS, pass port 995 and True. For a server that upgrades with STLS, pass port 110 and False. With RequireTLS enabled, the client ends the connection if the server does not offer the upgrade. Sign-in follows the TLS handshake.
Give the client the CA file you trust for this service. Leave ServerName empty to check the name in Host, or set it to the expected DNS name when connecting to a specific address. Free the returned client after collection.
ServerName to the name in the certificate - see Download a page over HTTPS.procedure CollectPop3Mail(aClient: TsmPOP3Client; const aPassword: string; const aHaveMessage: TFunc<string, Boolean>; const aSaveMessage: TProc<string, TBytes>); var lIndex, lCount: Integer; lOctets: Int64; lUID: string; begin aClient.SetPassword(aPassword); aClient.Open; try aClient.Stat(lCount, lOctets); for lIndex := 1 to lCount do begin lUID := aClient.UidlOne(lIndex); if lUID = '' then raise ePOP3ClientError.Create('Missing POP3 message identifier'); if not aHaveMessage(lUID) then aSaveMessage(lUID, aClient.Retrieve(lIndex)); end; finally aClient.Close; end; end;
Pass the password as typed on every call. The current POP3 client clears the stored password bytes during sign-in, so a second Open on the same client needs a fresh SetPassword.
Stat supplies the count for this session. POP3 numbers messages from 1 and can assign different numbers on the next connection. UidlOne asks for a persistent identifier. This collection method requires a server that implements UIDL.
Supply two callbacks for the mailbox being collected.
aHaveMessage looks up the exact, case-sensitive identifier in your saved messages for this account.aSaveMessage receives the identifier and the original bytes, including headers, body and attachments. Store both together. If saving fails, raise an exception so the next collection can retry that message.The sample issues no Delete commands. Closing the session leaves every message available to this and other clients. The client buffers one whole message for Retrieve. Its MaxResponse setting defaults to 64 MiB.
For folders and server-side flags, see Read messages from an IMAP mailbox over TLS. The mail notes describe both retrieval clients.
TsmPOP3Client · StreamSec.Mail.POP3Client