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 Part
property enables 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 changing
the value of this property, you can change the currently
selected message part, enabling methods like GetHeader
to operate on that specific message part.
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
the PartCount property 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.
For example, messages with file attachments typically consist of a
message part which describes the contents of the attachment, followed
by the attachment itself. For a message with one attached file, there
would be a total of three parts. Part 0 would refer to the main
message part, which contains the headers such as From, To, Subject,
Date and so on. For multipart messages, part 0 typically does not
have a message body, since any text is usually created as a separate
part (for those messages that do not contain multiple parts, the part
0 body contains the text message). Part 1 would contain the text
describing the attachment, and part 2 would contain the attachment
itself. If the attached file is binary, then the transfer encoding
type would usually be base64.