Skip to main content

MessageParts

property MessageParts: TIdMessageParts read;

Example

procedure ReportPartCount(var Value: Variant);
var
Msg: TIdMessage;
begin
Msg := Email;
if Msg = nil then Exit;

Value := Msg.MessageParts.Count;
end;

Usage

MessageParts returns the live message-owned collection of MIME text and attachment parts whose items can be created, reordered, normalised and destroyed in place.

Additional Technical Info

MessageParts returns the live TIdMessageParts collection created and owned by the message. The property reference is read-only, but the returned collection and its items are mutable. Do not free the collection, and do not retain it or any item beyond the owning message/action lifetime.

The collection represents MIME text parts, file/stream attachments and multipart markers. Use the documented concrete classes such as TIdText and TIdAttachmentFile to add items with this collection as owner. Calling the collection's inherited-looking Add is a trap: its override deliberately returns nil rather than creating an abstract base part.

Mutation and cached metadata

Adding, deleting or editing an item changes the live message immediately. AttachmentCount and TextPartCount are cached classifications, not continuously maintained values; call CountParts before depending on them. GenerateHeader recounts automatically, then may normalize each part's transfer encoding, infer message content type and regenerate boundaries.

Full normal output can also mutate parts by filling empty attachment content fields. With ConvertPreamble true, it can permanently append a new text part copied from Body. These side effects mean repeated save/send operations on the same message can produce different state or output.

Clearing, loading and temporary files

ClearBody calls MessageParts.Clear before clearing Body. Destruction of collection items closes their resources and can delete decoded temporary attachment files. LoadFromStream is clear-first and then repopulates this same owned collection unless NoDecode suppresses MIME decoding.

Do not modify the collection while it is being generated, sent, saved, cleared or destroyed. Validate untrusted message sizes and part counts before expensive downstream processing. The example is a source-reviewed observation only and did not parse or send mail.

External references

Created 2026-07-15