Contents · Forum Home · StreamSec Tools 4.1.5.365 documentation

Run a mail server for local delivery with SMTP and IMAP

An application on this computer can deliver a message to a local account over SMTP. The account holder can then read it from INBOX over IMAP. Both listeners use STARTTLS on the loopback interface and share one mail store.

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.

Create the mailbox and its account

Use one instance of each component in the header. Start with both servers and the store inactive. The sample uses these units.

uses
  stSecUtils, StreamSec.Mobile.TlsInternalServer, StreamSec.Mail.Transport,
  StreamSec.Mail.Users, StreamSec.Mail.Maildir,
  StreamSec.Mail.SMTPServer, StreamSec.Mail.IMAPServer;

Choose a writable directory for the mail and a separate file in an existing directory for the accounts. Setting FileName loads the accounts. A missing file gives an empty account list. For the host name, use the DNS name that clients will use to connect.

procedure ConfigureLocalMailStore(aUsers: TsmMailUsers; aStore: TsmMailStore;
  const aAccountsFile, aRoot, aHost: string);
begin
  aUsers.FileName := aAccountsFile;
  aStore.Root := aRoot;
  aStore.HostName := aHost;
  aStore.Users := aUsers;
end;

Provision the first account with its full address, such as alice@example.test, and the password supplied by the account holder. With AutoSave enabled (the default), SetPassword saves an Argon2id verifier. On later starts, loading the same accounts file restores the account.

procedure AddLocalMailAccount(aUsers: TsmMailUsers;
  const aAddress, aPassword: string);
begin
  if not aUsers.UserExists(aAddress) then
    aUsers.SetPassword(aAddress, aPassword);
end;

Give both listeners the server certificate

Load a PFX containing the private key and the certificate for the DNS name the clients use. Give the clients the corresponding trust anchor. In this example SMTP accepts incoming mail for local recipients, and IMAP authenticates the account with its password after STARTTLS.

procedure LoadLocalMailIdentity(aTLS: TsmSimpleTLSInternalServer;
  const aPfxFile, aPfxPassword: string);
begin
  aTLS.Options.RequestClientCertificate := False;
  aTLS.Options.RequireClientCertificate := False;
  aTLS.ImportFromPFX(aPfxFile, tDualSecretKey.CreateForPFX(aPfxPassword));
end;

Accept local recipients and publish their INBOXes

Pass the hosted mail domain, such as example.test, to ConfigureInboundMail. The inbound SMTP listener accepts mail for known accounts and refuses relay to other domains. A local address with no mailbox gets a 550 reply. The server also accepts postmaster for the hosted domain.

procedure ConfigureInboundMail(aSMTP: TsmSMTPServer; aStore: TsmMailStore;
  aTLS: TsmSimpleTLSInternalServer; const aDomain: string; aPort: Word);
begin
  aSMTP.ServerName := aStore.HostName;
  aSMTP.TLSServer := aTLS;
  aSMTP.Store := aStore;
  aSMTP.LocalDomains.Text := aDomain;
  aSMTP.LocalDeliveryMode := sldMaildir;
  aSMTP.RequireKnownMailbox := True;
  aSMTP.AllowRelayOnInbound := False;
  aSMTP.Inbound.BindIP := '127.0.0.1';
  aSMTP.Inbound.Port := aPort;
  aSMTP.Inbound.TLSMode := mtmRequired;
  aSMTP.Inbound.Enabled := True;
end;

Pass the same store to IMAP so it sees the messages SMTP has committed. Each server uses its own loopback port. For example, use 2525 for SMTP and 1143 for IMAP and select STARTTLS in both clients.

procedure ConfigureMailboxAccess(aIMAP: TsmIMAPServer; aStore: TsmMailStore;
  aTLS: TsmSimpleTLSInternalServer; aPort: Word);
begin
  aIMAP.ServerName := aStore.HostName;
  aIMAP.TLSServer := aTLS;
  aIMAP.Store := aStore;
  aIMAP.Users := aStore.Users;
  aIMAP.Plain.BindIP := '127.0.0.1';
  aIMAP.Plain.Port := aPort;
  aIMAP.Plain.TLSMode := mtmRequired;
  aIMAP.Plain.Enabled := True;
end;

Start and stop the service

Start the store before the listeners. Keep the components alive while clients are connected, then stop both listeners before closing the store. Restarting with the same mail directory preserves delivered messages.

procedure StopLocalMail(aStore: TsmMailStore; aSMTP: TsmSMTPServer;
  aIMAP: TsmIMAPServer);
begin
  aSMTP.Active := False;
  aIMAP.Active := False;
  aStore.Active := False;
end;

procedure StartLocalMail(aStore: TsmMailStore; aSMTP: TsmSMTPServer;
  aIMAP: TsmIMAPServer);
begin
  aStore.Active := True;
  try
    aSMTP.Active := True;
    aIMAP.Active := True;
  except
    StopLocalMail(aStore, aSMTP, aIMAP);
    raise;
  end;
end;

To try it, connect an SMTP client to the inbound port with STARTTLS and send a message to the provisioned address. This listener accepts a local delivery without SMTP sign-in. Connect an IMAP client to its port, sign in with the full address and password, and open INBOX.

Read messages from an IMAP mailbox over TLS shows the client side. The mail notes cover inbound SMTP, authenticated submission, and mailbox storage.

Reference

TsmSMTPServer · TsmIMAPServer · TsmMailStore · TsmMailUsers · TsmSimpleTLSInternalServer · StreamSec.Mail.SMTPServer · StreamSec.Mail.IMAPServer · StreamSec.Mail.Maildir · StreamSec.Mail.Users · StreamSec.Mail.Transport · StreamSec.Mobile.TlsInternalServer · stSecUtils

Related tasks