The server side of RFC 5321, with no socket, no TLS and no threads in it: octets go in through Receive, reply octets come out through OnOutput, and a completed message is handed to an iSmtpServerPolicy. That is the same shape as StreamSec.Http.Http2Conn and it exists for the same reason - SMTP framing is the security-critical half of an SMTP server, every smuggling technique is an attack on exactly these rules, and a parser that cannot be driven without a socket is one whose hostile-input cases never get tested. SmtpServerSelfTest at the bottom drives them. WHAT SPEAKS IT. StreamSec.Mail.SMTPServer wraps one of these per connection for TsmAsyncTLSServer, which is implicit TLS from byte 0 - so the deployment is SMTPS on port 465 (RFC 8314) and STARTTLS never appears. The engine itself carries no such assumption: it is TOLD what its channel is (SecureChannel, TransportInfo) and enforces policy against that. THE END-OF-DATA RULE, WHICH IS THE WHOLE OF SMTP SMUGGLING. A message body ends at <CRLF>.<CRLF> and at nothing else. This engine finds line boundaries in the DATA phase by scanning for CRLF ONLY - never for a bare CR, never for a bare LF - so <LF>.<LF> and <CR>.<CR> cannot terminate a message however they are spelled, and a downstream MTA with a laxer rule cannot be fed a second envelope through this one (CVE-2023-51764 and the family around it). A bare CR or LF inside DATA is then a separate question - what to STORE - and BareNewlinePolicy answers it; it can never be a question of where the message ended. DELIVERY IS NOT HERE. The engine accepts or refuses; what happens to an accepted message is iSmtpServerPolicy.MessageComplete's business - an event, a spool file, an onward SMTP session. See StreamSec.Mail.SMTPSpool and StreamSec.Mail.SMTPServer.
Feature notes: Mail subsystem notes (SMTP / POP3 / IMAP)
inherits from Exception
(no public members)
WHAT THE SESSION AGREED, separate from the message it carried: the SMTP envelope is not the RFC 5322 header block and the two routinely disagree - that is what a Bcc IS. Delivery is decided by this, never by the To: line.
EVERY STRING HERE IS PEER-SUPPLIED except RemoteAddress, TransportInfo and SecureChannel, which the transport fills in. HeloName in particular is whatever the client typed and is verified against nothing.
function GetId: string;
function GetHeloName: string;
function GetMailFrom: string;
function GetRecipientCount: Integer;
function GetRecipient(aIndex: Integer): string;
function GetAuthUser: string;
function GetAuthenticated: Boolean;
function GetRemoteAddress: string;
function GetTransportInfo: string;
function GetSecureChannel: Boolean;
function GetDeclaredSize: Int64;
function GetEightBitMime: Boolean;
function GetSmtpUtf8: Boolean;
function GetReceivedAt: TDateTime;
property Id: string read GetId;
property HeloName: string read GetHeloName;
property MailFrom: string read GetMailFrom;
property RecipientCount: Integer read GetRecipientCount;
property Recipients[aIndex: Integer]: string read GetRecipient;
property AuthUser: string read GetAuthUser;
property Authenticated: Boolean read GetAuthenticated;
property RemoteAddress: string read GetRemoteAddress;
property TransportInfo: string read GetTransportInfo;
property SecureChannel: Boolean read GetSecureChannel;
property DeclaredSize: Int64 read GetDeclaredSize;
property EightBitMime: Boolean read GetEightBitMime;
property SmtpUtf8: Boolean read GetSmtpUtf8;
property ReceivedAt: TDateTime read GetReceivedAt;
inherits from TInterfacedObject
implements iSmtpEnvelope
The implementation, and the spool's serialization format. The setters belong to the engine; a consumer holds the interface, which is read-only.
constructor Create;
destructor Destroy; override;
procedure ResetTransaction;
function AddRecipient(const aPath: string): Boolean;
procedure Assign(aSource: tSmtpEnvelope);
procedure SaveToStream(aDest: TStream);
procedure LoadFromStream(aSource: TStream);
property Id: string read fId write fId;
property HeloName: string read fHeloName write fHeloName;
property MailFrom: string read fMailFrom write fMailFrom;
property MailFromSet: Boolean read fMailFromSet write fMailFromSet;
property Recipients: TStringList read fRecipients;
property RecipientCount: Integer read GetRecipientCount;
property AuthUser: string read fAuthUser write fAuthUser;
property Authenticated: Boolean read fAuthenticated write fAuthenticated;
property AuthForTransaction: Boolean read fAuthForTransaction write fAuthForTransaction;
property RemoteAddress: string read fRemoteAddress write fRemoteAddress;
property TransportInfo: string read fTransportInfo write fTransportInfo;
property SecureChannel: Boolean read fSecureChannel write fSecureChannel;
property DeclaredSize: Int64 read fDeclaredSize write fDeclaredSize;
property EightBitMime: Boolean read fEightBitMime write fEightBitMime;
property SmtpUtf8: Boolean read fSmtpUtf8 write fSmtpUtf8;
property ReceivedAt: TDateTime read fReceivedAt write fReceivedAt;
inherits from TPersistent
The bounds one session enforces, as a TPersistent so a component can publish it as a sub-object - the tCustomHTTPLimits pattern, and for the same reason: an operator who needs a 50 MiB message limit should not have to recompile. 0 MEANS UNBOUNDED on every one of them and is a decision to take deliberately; a negative normalises to 0 on the way in.
constructor Create(aOwner: TComponent); virtual;
procedure AssignTo(Dest: TPersistent); override;
property MessageBytes: Integer read fMessageBytes write SetMessageBytes default cSmtpDefaultMaxMessageBytes;
property Recipients: Integer read fRecipients write SetRecipients default cSmtpDefaultMaxRecipients;
property MessagesPerConnection: Integer read fMessagesPerConnection write SetMessagesPerConnection default cSmtpDefaultMaxMessagesPerConnection;
property TextLineBytes: Integer read fTextLineBytes write SetTextLineBytes default cSmtpDefaultMaxTextLine;
property Errors: Integer read fErrors write SetErrors default cSmtpDefaultMaxErrors;
property AuthFailures: Integer read fAuthFailures write SetAuthFailures default cSmtpDefaultMaxAuthFailures;
(no public members)
EVERY DECISION THE ENGINE DOES NOT MAKE ITSELF. The engine owns framing, sequencing and the bounds; who may send, who may be sent to, and what happens to an accepted message are the application's, and they arrive here.
aCode AND aText ARRIVE PRE-LOADED with the answer that applies if the implementor does nothing, so a policy that cares about one question can leave the rest alone. Returning False sends the refusal; returning True sends the success reply. Where the pre-loaded answer is a REFUSAL this is said on the method - CheckRecipient is the one that matters.
CALLED ON WHATEVER THREAD DROVE Receive. Under the async TLS server that is a reactor I/O thread with the connection's gate held, so an implementation must not block - see AsyncTLSServer.txt section 6.
function CheckConnection(const aSession: tSmtpSession; var aCode: Integer; var aText: string): Boolean;
function Greeting(const aSession: tSmtpSession): string;
function CheckHelo(const aSession: tSmtpSession; const aName: string; var aCode: Integer; var aText: string): Boolean;
function Authenticate(const aSession: tSmtpSession; const aUser: string; const aPassword: iSecretKey; var aAuthorizedAs: string; var aCode: Integer; var aText: string): Boolean;
function CheckSender(const aSession: tSmtpSession; const aPath: string; var aCode: Integer; var aText: string): Boolean;
function CheckRecipient(const aSession: tSmtpSession; const aPath: string; var aCode: Integer; var aText: string): Boolean;
function MessageComplete(const aSession: tSmtpSession; const aEnvelope: iSmtpEnvelope; const aData: tBytes; var aCode: Integer; var aText: string): Boolean;
procedure SessionClosed(const aSession: tSmtpSession);
ONE CONNECTION'S SMTP CONVERSATION.
Drive it with Start (which queues the banner) and then Receive for every octet the peer sends. Replies leave through OnOutput, COALESCED: everything a single Receive produced is emitted in one call, so a pipelined batch of commands (RFC 2920) costs one write and one TLS record rather than one per reply.
NOT THREAD-SAFE, and does not need to be: it belongs to one connection, and the async TLS server serializes that connection's callbacks behind its per-connection gate.
constructor Create(const aPolicy: iSmtpServerPolicy; aLimits: tSmtpLimits = nil);
destructor Destroy; override;
procedure Start;
procedure Receive(aData: Pointer; aLen: Integer);
procedure SecureChannelEstablished(const aTransportInfo: string);
property Envelope: tSmtpEnvelope read fEnvelope;
property State: tSmtpState read fState;
property Limits: tSmtpLimits read fLimits;
property PendingInputLen: Integer read fInLen;
property MessagesServed: Integer read fMessagesServed;
property SecureChannel: Boolean read fSecureChannel write fSecureChannel;
property RemoteAddress: string read fRemoteAddress write fRemoteAddress;
property TransportInfo: string read fTransportInfo write SetTransportInfo;
property ServerName: string read fServerName write fServerName;
property AuthMechanisms: tSmtpServerAuths read fAuthMechanisms write fAuthMechanisms;
property RequireAuth: Boolean read fRequireAuth write fRequireAuth;
property BareNewlinePolicy: tSmtpBareNewlinePolicy read fBareNewlinePolicy write fBareNewlinePolicy;
property OfferStartTLS: Boolean read fOfferStartTLS write fOfferStartTLS;
property RequireSecureChannel: Boolean read fRequireSecureChannel write fRequireSecureChannel;
property OfferSmtpUtf8: Boolean read fOfferSmtpUtf8 write fOfferSmtpUtf8;
property OnOutput: tSmtpOutputEvent read fOnOutput write fOnOutput;
property OnException: tSmtpExceptionEvent read fOnException write fOnException;
property OnStoreUnavailable: tSmtpStoreUnavailableEvent read fOnStoreUnavailable write fOnStoreUnavailable;
property OnCloseRequested: TNotifyEvent read fOnCloseRequested write fOnCloseRequested;
property OnStartTLSRequested: TNotifyEvent read fOnStartTLSRequested write fOnStartTLSRequested;
Where the session is in RFC 5321 sec.4.1. smsGreeting is the state before Start has emitted the banner; smsClosing is set once a reply has been queued that ends the session, and every further octet from the peer is discarded (the close is asynchronous, so bytes already in flight arrive after it).
smsStartTLS is the window between the 220 that promises a handshake and the handshake itself. It exists to STOP the line loop dead: every octet the peer pipelined behind STARTTLS has already been destroyed by then, and no further command may be read from a buffer that is about to stop being cleartext. Appended rather than inserted so every existing ordinal stays put.
The SASL mechanisms this engine can run. Named ssa* rather than sam* because StreamSec.Mail.SMTPClient already has a tSMTPAuthMechanism whose members are sam*, and a relay unit uses BOTH - Delphi would resolve the duplicate to whichever unit came last in the uses clause, silently.
What to do about a bare CR or bare LF inside DATA. It is never a question of where the message ENDS - see the unit header - only of what is stored.
sbnReject answer 521 and drop the connection, which is what Postfix's smtpd_forbid_bare_newline does and the safe default: a peer sending bare newlines is either broken or probing, and passing the message on unchanged would hand the ambiguity to the next hop.
sbnNormalize store it as CRLF and carry on. Accepts mail from sloppy clients at the cost of rewriting their octets - which breaks any signature computed over the original (DKIM, S/MIME), and can turn an embedded <LF>.<LF> into a stored line holding a single dot. That is safe HERE because everything this library re-transmits is dot-stuffed on the way out (TsmCustomSMTPClient), and it is exactly what would not be safe in a pipeline that re-emits the octets raw.
Reply octets. No Sender, because this is an engine rather than a component - the same shape as tHttp2OutputEvent.