Contents · Forum Home · StreamSec Tools 4.1.5.364 documentation

Send an e-mail over TLS

TsmSMTPClient hands a message to a mail server for delivery - your provider's submission server, or your own. TsmEmail builds the message.

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.

The units

uses
  System.SysUtils, System.IOUtils,
  StreamSec.Mail.Message,      // TsmEmail
  StreamSec.Mail.SMTPClient;   // TsmSMTPClient

Set up the client

function NewSmtpClient(const aHost: string; aPort: Integer;
  const aCAFile, aUser, aPassword: string): TsmSMTPClient;
begin
  Result := TsmSMTPClient.Create(nil);
  try
    { Port 465 is TLS from the first byte; port 587 starts in the clear and
      upgrades with STARTTLS, so the greeting, EHLO and STARTTLS itself go
      in the clear. Either way the sign-in and the message wait for TLS: a
      server that does not offer STARTTLS is refused (RequireTLS). }
    Result.Host := aHost;   // checked against the server's certificate
    Result.Port := aPort;
    Result.ImplicitTLS := aPort = 465;
    { The server's CA. For a public mail provider, give the engine the
      public roots instead - see "Choose which servers a client trusts". }
    if Result.TLSServer.LoadRootCertsFromFile(aCAFile) = 0 then
      raise eSMTPClientError.CreateFmt('No certificate in %s', [aCAFile]);
    { Sign-in, as typed. It is only ever sent over TLS. }
    Result.UserName := aUser;
    Result.SetPassword(aPassword);
    { The defaults (5 and 10 minutes) are for unattended relays. }
    Result.ReplyTimeout := 30000;
  except
    Result.Free;
    raise;
  end;
end;

Why only PLAIN and LOGIN

The client signs in with PLAIN or LOGIN, and the library's mail servers accept no other way of signing in: SMTP offers PLAIN and LOGIN, and IMAP and POP3 offer the same two besides their own LOGIN and USER/PASS commands, which carry the password in the same way. This is a decision for security, not a gap.

SCRAM, the newest of those mechanisms, stores a verifier rather than the password, but one derived with PBKDF2, which is not memory-hard: for the same work on the server, each guess costs an attacker far less than with Argon2id.

Send a message

procedure SendText(aClient: TsmSMTPClient; const aFrom, aTo, aSubject,
  aText: string; const aAttachmentFile: string);
var
  lMail: TsmEmail;
begin
  lMail := TsmEmail.Create(nil);
  try
    lMail.NewMessage;                    // MIME-Version and Date
    lMail.From := aFrom;                 // 'Name <user@example.com>'
    lMail.Recipients := aTo;             // the To field
    lMail.Subject := aSubject;
    lMail.SetTextBody(aText);            // UTF-8
    if aAttachmentFile <> '' then
      lMail.AddAttachment(TFile.ReadAllBytes(aAttachmentFile),
                          ExtractFileName(aAttachmentFile));
    aClient.Open;                        // connect, TLS, sign in
    try
      { The envelope comes from the message: From, To, Cc and Bcc. A Bcc
        field is not sent. }
      aClient.SendMail(lMail);
    finally
      aClient.Close;
    end;
  finally
    lMail.Free;
  end;
end;

SendText(lClient, 'Reports <reports@example.com>', 'alice@example.com', 'Monthly report', 'The report is attached.', 'C:\Reports\november.pdf') sends one message with one attachment.

The mail notes cover the message format, the server side, and receiving mail with POP3 and IMAP.

Reference

TsmSMTPClient · TsmEmail · StreamSec.Mail.SMTPClient · StreamSec.Mail.Message