TsmSMTPClient hands a message to a mail server for delivery - your provider's submission server, or your own. TsmEmail builds the message.
ServerName to the name in the certificate - see Download a page over HTTPS.uses System.SysUtils, System.IOUtils, StreamSec.Mail.Message, // TsmEmail StreamSec.Mail.SMTPClient; // TsmSMTPClient
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;
ImplicitTLS True (the default) the client speaks TLS from the first byte, which is what port 465 expects. With it False the client connects in the clear and upgrades with STARTTLS, which is what port 587 expects. Use the port your provider names.RequireTLS True (the default), a server that does not offer STARTTLS is refused, and so is one whose upgrade fails - the client does not carry on in the clear. Sign-in is never sent without TLS, whatever RequireTLS says.UserName is set, with PLAIN or LOGIN - PLAIN when the server offers it. A wrong password raises eSMTPClientError, whose ReplyCode is the server's answer.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.
TsmMailUsers keeps an Argon2id verifier, from which the password cannot be read back (see Store a password safely).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.
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.
Recipients is the To field (to is a reserved word in Delphi); Cc and Bcc work the same way. Several addresses are separated by commas.application/octet-stream unless you pass its media type as the third argument of AddAttachment - application/pdf, for instance.The mail notes cover the message format, the server side, and receiving mail with POP3 and IMAP.
TsmSMTPClient · TsmEmail · StreamSec.Mail.SMTPClient · StreamSec.Mail.Message