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.