Contents · Forum Home · StreamSec Tools 4.1.5.365 documentation

Read messages from an IMAP mailbox over TLS

Use TsmIMAPClient to open a mailbox read-only and fetch each message into a TsmEmail. The messages keep their unread status.

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.
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.

Connect to the IMAP service

uses
  System.SysUtils, StreamSec.Mail.IMAPClient, StreamSec.Mail.Message;
function NewImapReader(const aHost: string; aPort: Word; aImplicitTLS: Boolean;
  const aCAFile, aUser, aPassword: string): TsmIMAPClient;
begin
  Result := TsmIMAPClient.Create(nil);
  try
    Result.Host := aHost;
    Result.Port := aPort;
    Result.ImplicitTLS := aImplicitTLS;
    Result.RequireTLS := True;
    if Result.TLSServer.LoadRootCertsFromFile(aCAFile) = 0 then
      raise eIMAPClientError.CreateFmt('No certificate in %s', [aCAFile]);
    Result.UserName := aUser;
    Result.SetPassword(aPassword);
  except
    Result.Free;
    raise;
  end;
end;

Give NewImapReader the provider's DNS name, the port, the TLS mode, a trusted issuer file and the account credentials. Use port 993 and aImplicitTLS = True for TLS from the first byte. For an account configured for STARTTLS, use port 143 and False. With RequireTLS set, the client must complete the upgrade before it signs in. For more trust options, see Choose which servers a client trusts.

Read the messages by UID

procedure ReadImapMailbox(aClient: TsmIMAPClient; const aMailbox: string;
  const aReadMessage: TProc<Cardinal, TsmEmail>);
var
  lUid: Cardinal;
  lMessage: TsmEmail;
begin
  aClient.Open;
  try
    aClient.SelectMailbox(aMailbox, True);  // EXAMINE the mailbox read-only
    lMessage := TsmEmail.Create(nil);
    try
      for lUid in aClient.Search('ALL') do begin
        aClient.FetchMessage(lUid, lMessage);
        aReadMessage(lUid, lMessage);
      end;
    finally
      lMessage.Free;
    end;
  finally
    aClient.Close;
  end;
end;

Pass the client, the mailbox name (for example, INBOX), and a procedure that takes a Cardinal UID and a TsmEmail. Inside the procedure, read Subject and BodyText, or copy the values you want to keep. The sample reuses the message object for the next UID and frees it before returning. Free the client when you have finished with it.

The mail notes cover mailbox selection, search and other IMAP commands.

Reference

TsmIMAPClient · TsmEmail · StreamSec.Mail.IMAPClient · StreamSec.Mail.Message

Related tasks