Multipart email messages are structured using the MIME (Multipurpose
Internet Mail Extensions) standard, which allows a single email to
contain multiple content types, such as plain text, HTML, and file
attachments, within a single message body. These components are
organized as discrete parts, each with its own headers and body,
separated by unique MIME boundaries. In a typical multipart/alternative
message, part 0 often contains the message preamble (e.g.: "This is a
multipart message in MIME format"), part 1 may contain the plain text
version of the message, and part 2 may contain the HTML version.
However, multipart messages are not required to include both plain text
and HTML content. A message may only include an HTML part and one or
more attachments, or any other combination of content types. The
structure and ordering of parts depends entirely on how the original
sender or mail client composed the message.
To simplify navigating these multipart structures, the SetPart
and GetPart methods enable you to iterate through
the individual parts of a message. The message parts are zero-based,
with index 0 always referring to the top-level message headers and preamble.
By calling SetPart, you can change the currently
selected message part, enabling functions like GetHeader
or GetText to operate on that specific segment.
Calling GetPart returns the current part index,
which can be useful for saving and restoring state while iterating
over a message.
The internal MIME parser flattens multipart
messages, meaning that if a message contains nested multipart sections
(such as a multipart/mixed part containing a multipart/alternative
part), all leaf parts are presented in a linear sequence. This
simplifies access and avoids the complexity of recursive traversal.
You can determine the total number of parts using
GetPartCount, and then iterate from part 0 to part N-1 to
examine or extract each segment in order. This approach provides both
fine-grained access to MIME message parts and an intuitive model for
parsing even complex message structures.