Parses and composes Internet messages - .eml files and streams - as defined by RFC 5322 (message format), RFC 2045-2047 and RFC 2049 (MIME). The unit carries the tMailHeaders / tMimeEntity engine plus the TsmEmail component that applications drop on a form or create in code. THE ONE DESIGN RULE EVERYTHING ELSE FOLLOWS FROM: every entity RETAINS THE EXACT OCTETS it was parsed from, and re-emits them verbatim for as long as the entity has not been modified. S/MIME multipart/signed verification needs the byte-exact canonical source of the signed body part - headers included, transfer encoding preserved, CRLF intact - and a mail model that re-encodes parts on output (the reason the old Indy-based S/MIME demo could never verify a detached signature) destroys precisely those bytes. Mutating a header or a body invalidates the retained source of that entity and of every ancestor whose body contains it; untouched siblings still re-emit their original octets, so wrapping a parsed entity as a part of a new multipart/signed keeps the signed bytes stable. WHAT THIS UNIT IS NOT: it is no transport and no crypto. SMTP lives in StreamSec.Mail.SMTPClient; S/MIME wrapping lives in StreamSec.Mail.SMIMEBroker; both consume this engine. Data is tBytes throughout, for the same reason StreamSec.Http.Client is: OctetString is conditionally an AnsiString variant, a dynamic array or a record, and only the AnsiString form supports the Pos/Copy idioms a parser needs, whereas tBytes is a plain byte array on every supported compiler. Header values live in native strings, narrowed and widened BYTE-WISE (Latin-1); anything outside that range must travel through the RFC 2047 encoder, which is what real mail requires anyway. TOLERANT IN, STRICT OUT. The parser accepts bare-LF line ends, missing colons, unknown transfer encodings and folded headers of every provenance, because .eml files in the wild carry all of those. The composer emits CRLF line ends, folded headers below 78 columns and RFC 2047 encoded words - and SetField REJECTS names or values carrying CR or LF outright, because a header value assembled from attacker-influenced text is the header-injection family. Bounds (header block size, field count, nesting depth, part count) are enforced on parse so a hostile message cannot balloon memory.
Feature notes: Mail subsystem notes (SMTP / POP3 / IMAP)
inherits from Exception
(no public members)
One parsed mailbox: DisplayName is the human-readable phrase (RFC 2047 decoded), Address the bare addr-spec (local@domain).
DisplayName: string;
Address: string;
An ordered collection of header fields. Order is preserved and duplicate names are legal (Received, for one). Values are stored UNFOLDED and otherwise raw - RFC 2047 encoded words are decoded on demand by DecodedField, never in place, so a round-trip does not rewrite what it merely read. The original header block octets are retained and re-emitted verbatim until the first mutation (Pristine).
destructor Destroy; override;
procedure Clear;
property Pristine: Boolean read fPristine;
property Count: Integer read fCount;
property Names[aIndex: Integer]: string read GetName;
property Values[aIndex: Integer]: string read GetValue write SetValue;
function HasField(const aName: string): Boolean;
function Field(const aName: string): string;
function DecodedField(const aName: string): string;
procedure SetField(const aName, aValue: string);
procedure SetTextField(const aName, aText: string);
procedure AddField(const aName, aValue: string);
procedure RemoveField(const aName: string);
function ParamValue(const aName, aParam: string): string;
procedure ParseFrom(const aData: tBytes; aOffset, aCount: Integer);
procedure ComposeTo(aDst: TStream);
procedure CopyFrom(aSource: tMailHeaders);
One MIME entity: a header block plus either a leaf body or child parts. The root entity of a message is the message itself. RawSource re-emits the exact parsed octets for as long as the entity is unmodified - see the unit comment for why that is the load-bearing feature.
constructor Create(aParent: tMimeEntity = nil);
destructor Destroy; override;
procedure Clear;
property Parent: tMimeEntity read fParent;
property Headers: tMailHeaders read fHeaders;
function MediaType: string;
function IsMultipart: Boolean;
function Boundary: string;
function CharSet: string;
function TransferEncoding: tMailEncoding;
function FileName: string;
function IsAttachment: Boolean;
function RawBody: tBytes;
function DecodedBody: tBytes;
function BodyText: string;
procedure SetBody(const aData: tBytes; aEncoding: tMailEncoding; const aMediaType: string = '');
procedure SetRawBody(const aData: tBytes);
procedure SetBodyText(const aText: string; const aCharSet: string = 'utf-8');
function PartCount: Integer;
property Parts[aIndex: Integer]: tMimeEntity read GetPart;
function AddPart: tMimeEntity;
function AddPartFromBytes(const aData: tBytes): tMimeEntity;
procedure DeletePart(aIndex: Integer);
procedure MakeMultipart(const aSubType: string);
procedure Parse(const aData: tBytes);
procedure ParseWithBudget(const aData: tBytes; aDepth: Integer; var aPartTotal: Integer);
property NestingDepth: Integer read fNestingDepth;
procedure DiscardBodyStorage;
function RawSource: tBytes;
procedure ComposeTo(aDst: TStream);
inherits from TComponent
An Internet mail message: parse and compose .eml files or streams, read and write the envelope headers, walk or build the MIME part tree via Root. The published address and subject properties present the DECODED, human-readable view (setting them applies RFC 2047 encoding as needed); the raw header values remain reachable through Headers.
A parsed message that is saved unmodified round-trips byte for byte.
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Clear;
procedure LoadFromStream(aStream: TStream);
procedure LoadFromFile(const aFileName: string);
procedure LoadFromBytes(const aData: tBytes);
procedure SaveToStream(aStream: TStream);
procedure SaveToFile(const aFileName: string);
function SaveToBytes: tBytes;
property Root: tMimeEntity read fRoot;
function Headers: tMailHeaders;
procedure NewMessage;
procedure GenerateMessageId(const aDomain: string = '');
procedure SetTextBody(const aText: string; const aCharSet: string = 'utf-8');
function AddAttachment(const aData: tBytes; const aFileName: string; const aMediaType: string = ''): tMimeEntity;
function FindTextEntity: tMimeEntity;
function BodyText: string;
function FromAddress: string;
function SenderAddress: string;
function GetEnvelopeAddresses(aList: TStrings; aIncludeBcc: Boolean): Integer;
function Date: TDateTime;
procedure SetDate(aUTC: TDateTime);
property Subject: string read GetSubject write SetSubject;
property From: string read GetFrom write SetFrom;
property Sender: string read GetSender write SetSender;
property ReplyTo: string read GetReplyTo write SetReplyTo;
property Recipients: string read GetRecipients write SetRecipients;
property Cc: string read GetCc write SetCc;
property Bcc: string read GetBcc write SetBcc;
property MessageId: string read GetMessageId write SetMessageId;
The Content-Transfer-Encoding of an entity body. meUnknown preserves a foreign encoding verbatim: DecodedBody then returns the raw body rather than guessing.