Using Delphi 11.
After downloading an email using TIdIMAP4, scanning through the MessageParts, how do I detect if a MessagePart contains a MIME encoded attachment, and how do I decode this into the original format (Image, documents, etc)?
procedure TImapForm.ProcessEmail(MSG: TIdMessage);
begin
ContainsAttachement := false;
if Msg.MessageParts.Count > 0 then
begin
for i := 0 to Pred(Msg.MessageParts.Count) do
begin
if Msg.MessageParts.Items[i] is TIdText then
begin
// Process Text-only message here (Don't want HTML)
end else
begin
ContainsAttachement := true;
// if Msg.MessageParts[i].MessageParts.Encoding = meMIME then
if MSG.ContentTransferEncoding = 'base64' then //??
if Msg.MessageParts.Items[i].IsEncoded then //??
begin
// How to actually decode the MessagePart to a binary file?
end;
end;
end;
end;
end;
Email example:
This is a multi-part message in MIME format.
--------------wt6iyRLyQwO4w89MYm2jGb0w
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
test
--------------wt6iyRLyQwO4w89MYm2jGb0w
Content-Type: image/jpeg; name="Bart.jpg"
Content-Disposition: attachment; filename="Bart.jpg"
Content-Transfer-Encoding: base64
/9j/4AAQSkZJRgABAQEASABIAAD/4ThFRXhpZgAASUkqAAgAAAAMAA4BAgAgAAAAngAAAA8B
AgAUAAAAvgAAABABAgAHAAAA1gAAABIBAwABAAAAAQAAABoBBQABAAAA7gAAABsBBQABAAAA
9gAAACgBAwABAAAAAgAAADEBAgAIAAAA/gAAADIBAgAUAAAAHgEAABMCAwABAAAAAgAAAGmH
etc.
TIdMessagealready handles this for you. Look forTIdAttachmentmessage parts, eg:That being said, MIME-encoded emails can have complex structures (see HTML Messages on Indy's blog, for example). Because of this, message parts can be nested, and have relationships with other message parts. So, the correct way to process standalone attachments (as opposed to HTML-embedded images/media, for instance) in a MIME email is to loop backwards from last part to first part (because MIME parts are ordered from least complex to most complex) looking for attachments whose
ParentPartproperty is-1(ie, a top-level part).In general, if you find a message part whose
ContentTypeproperty ismultipart/...(likemultipart/related) that interests you, then you can dig into its nested content by looping again looking for message parts whoseParentPartproperty refers to that message part that interested you. And so on, and so on, as deep as you need to go.For example: