WHAT A 250 MEANS. When an SMTP server answers 250 to the end of DATA it has told the client the message is its problem no longer - the client is entitled to delete its copy, and every other MTA does. So the accept has to be durable BEFORE the reply, and delivery has to keep being attempted afterwards, across restarts, until it succeeds or is abandoned with a bounce. That is what this unit is: the queue behind StreamSec.Mail.SMTPServer. THE COMMIT IS ONE RENAME. Store writes the message body into queue\<id>.msg, then writes the envelope into tmp\<id>.env and RENAMES it into queue\. The presence of the .env is what makes a message queued, so a crash can leave an orphan .msg (collected on the next scan) but never a half-written message that looks deliverable. With FlushToDisk set - the default - both files are flushed to the platter before the rename, so the guarantee survives power loss and not merely process death. DELIVERY IS SOMEBODY ELSE'S JOB. The queue decides WHEN to try and what to do with the answer; iSmtpDeliveryAgent does the trying. tSmtpSmarthostAgent is the shipped implementation and drives TsmCustomSMTPClient at a configured smarthost, which is what a submission relay wants; anything else - MX lookup, a local mailbox store, a test double - is an interface away. WHAT IT IS NOT. It is not a full MTA queue. There is NO per-recipient resolution: a message is delivered, deferred or bounced AS A WHOLE, because the client API underneath reports one outcome per submission. A message to ten recipients of which one is permanently rejected therefore bounces in full rather than being split, which is the one place a real MTA would do better - it is the honest limit of the layer below, not an oversight, and the shape of iSmtpDeliveryAgent is what a per-recipient version would widen.
Feature notes: Mail subsystem notes (SMTP / POP3 / IMAP)
inherits from Exception
(no public members)
DOING THE ACTUAL SENDING. The queue owns the schedule; this owns the wire. Deliver runs on a spool worker thread, one message at a time per thread, and may block for as long as the transfer takes - that is the whole reason delivery is off the reactor. aError is what goes into the bounce and the log, so put the far end's own reply text in it.
function Deliver(const aEnvelope: iSmtpEnvelope; const aData: tBytes; var aError: string): tSmtpDeliveryOutcome;
(no public members)
One queued message. The BODY IS NOT HERE - it stays on disk until an attempt is made, so a queue of ten thousand messages costs ten thousand envelopes of memory rather than the mail itself.
property Id: string read fId;
property Envelope: iSmtpEnvelope read fEnvelope;
property Attempts: Integer read fAttempts;
property FirstAttempt: TDateTime read fFirstAttempt;
property NextAttempt: TDateTime read fNextAttempt;
property LastError: string read fLastError;
THE QUEUE. Create it, point it at a directory, give it an agent, Start it. Store is then safe to call from any thread - and specifically from the reactor thread that is holding an SMTP client waiting for its 250.
constructor Create;
destructor Destroy; override;
procedure Store(const aEnvelope: iSmtpEnvelope; const aData: tBytes);
procedure Start;
procedure RequestStop(aFreezeWrites: Boolean = False);
procedure Stop;
procedure Poke;
procedure Snapshot(aDest: TList);
function OldestReceivedAt: TDateTime;
property Count: Integer read GetCount;
property Active: Boolean read fActive;
property Stored: Int64 read fStored;
property Delivered: Int64 read fDelivered;
property Bounced: Int64 read fBounced;
property Directory: string read fDirectory write SetDirectory;
property StoreGate: tMaildir read fStoreGate write SetStoreGate;
property Agent: iSmtpDeliveryAgent read fAgent write fAgent;
property RetryIntervals: string read fRetryIntervals write SetRetryIntervals;
property MaxAge: Integer read fMaxAge write fMaxAge;
property DeliveryThreads: Integer read fDeliveryThreads write SetDeliveryThreads;
property DrainTimeout: Cardinal read fDrainTimeout write fDrainTimeout;
property FlushToDisk: Boolean read fFlushToDisk write fFlushToDisk;
property Postmaster: string read fPostmaster write fPostmaster;
property ServerName: string read fServerName write fServerName;
property OnDelivered: tSmtpSpoolEvent read fOnDelivered write fOnDelivered;
property OnDeferred: tSmtpSpoolEvent read fOnDeferred write fOnDeferred;
property OnUndeliverable: tSmtpSpoolFailureEvent read fOnUndeliverable write fOnUndeliverable;
property OnLog: tSmtpSpoolLogEvent read fOnLog write fOnLog;
WHERE AN ATTEMPT ENDED AND WHAT ENDED IT, for a consumer whose policy turns on more than the outcome. The agent frees its client inside Deliver, so without this the stage and the reply code die there and the only thing left to reason from is aError's text - which this record exists to abolish as a programming interface.
Stage: tSmtpDeliveryStage;
ReplyCode: Integer;
SmtpError: Boolean;
WHAT A COMMISSIONING PROBE FOUND. Nothing is ever submitted.
Opened: Boolean;
Authenticated: Boolean;
SenderTried: Boolean;
SenderAccepted: Boolean;
Report: tSmtpDeliveryReport;
Extensions: TArray<string>;
inherits from TInterfacedObject
implements iSmtpDeliveryAgent
THE SHIPPED AGENT: everything goes to one smarthost. That is the submission relay shape - an application hands its mail to a provider that owns the MX lookups, the reputation and the retry policy of the wider internet, and this queue owns only the hop to it. A CLIENT PER DELIVERY, deliberately: TsmCustomSMTPClient is a stateful component with a live connection, holding one open across idle periods invites the far end to close it under us, and reconnecting is a few milliseconds against a transfer measured in hundreds.
constructor Create;
function Deliver(const aEnvelope: iSmtpEnvelope; const aData: tBytes; var aError: string): tSmtpDeliveryOutcome; overload;
function Deliver(const aEnvelope: iSmtpEnvelope; const aData: tBytes; var aError: string; out aReport: tSmtpDeliveryReport): tSmtpDeliveryOutcome; overload;
function Probe(const aMailFrom: string; var aError: string): tSmtpProbeResult;
procedure SetPassword(const aValue: string);
property Host: string read fHost write fHost;
property Port: Word read fPort write fPort;
property TLSServer: TsmSimpleTLSInternalServer read fTLSServer write fTLSServer;
property TLSMode: tSmtpDeliveryTlsMode read fTLSMode write fTLSMode;
property UserName: string read fUserName write fUserName;
property Password: iSecretKey read fPassword write fPassword;
property HeloName: string read fHeloName write fHeloName;
property SourceIP: string read fSourceIP write fSourceIP;
property ConnectTimeout: Cardinal read fConnectTimeout write fConnectTimeout;
property OnConfigure: tSmtpAgentConfigureEvent read fOnConfigure write fOnConfigure;
What an attempt at delivery came to.
sdoDelivered gone; the spool files are removed.
sdoTransient 4xx, a connection failure, a timeout - try again later.
sdoPermanent 5xx - the destination has refused it and will refuse it again, so retrying is only noise. Bounces.
HOW MUCH TLS THE ONWARD LEG INSISTS ON. One property rather than an implicit-TLS flag beside a require-TLS flag: the two together can express states nobody wants and can disagree with each other, and which of them wins is exactly the kind of thing that gets discovered in production. THERE IS NO 'never' MEMBER, deliberately. The client upgrades whenever the far end advertises STARTTLS and there is no way to tell it not to, so a cleartext-only mode would be a name that lies.
Why a message left the queue without being delivered - what OnUndeliverable is told, and what the bounce says.
Anything worth telling an operator. aFatal marks the ones that mean the queue is not working - an unwritable directory, a corrupt spool file - as opposed to the ordinary business of a deferred message.
Configures the SMTP client for one delivery: trust anchors, a client certificate for mutual TLS, timeouts. Called on the worker thread with a freshly created client each time.
NOT REDECLARED HERE, RE-EXPORTED. tSmtpDeliveryStage is declared in StreamSec.Mail.SMTPClient - the unit whose own code moves it - so that a consumer sees ONE type and cannot end up holding two enumerations that drift apart. The member names belong to the declaring unit: name it in the uses clause to write sdsMail rather than to merely pass the value on.