Contents · Forum Home · StreamSec Tools 4.1.5.365 documentation

Retrieve messages from a POP3 mailbox over TLS

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.

The components on this page are normally placed on a form or a data module at design time and set up in the Object Inspector. The sample creates them in code instead, because code shows at a glance which properties are changed from their default values.

Connect to the mail provider

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.

The client checks, on every connection, that the server's certificate is issued for the host name it connects to. Give it the DNS name the certificate carries, not the server's IP address: with an address the connection fails although the server's certificate is valid. To connect by address, set ServerName to the name in the certificate - see Download a page over HTTPS.

Collect messages and keep them on the server

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.

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.

Reference

TsmPOP3Client · StreamSec.Mail.POP3Client

Related tasks