Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To extract the TextBody from an openpgp encoded message in Delphi using Idmessage, follow these steps:

  1. Declare and create an instance of TIdMessage.
var
  IdMessage: TIdMessage;
begin
  IdMessage:= TIdMessage.Create(nil);
  1. Load the message into the TIdMessage instance.
IdMessage.LoadFromFile('C:\path\to\message.eml');
  1. Iterate through the message parts to find the one with a Content-Type of "text/plain" and a Content-Transfer-Encoding of "quoted-printable".
var
  i: Integer;
  Part: TIdMessagePart;
  TextBody: TStringList;
begin
  TextBody := TStringList.Create;
  try
    for i := 0 to IdMessage.MessageParts.Count - 1 do
    begin
      Part := IdMessage.MessageParts.Items[i];
      if Part.ContentType = 'text/plain' then
      begin
        if Part.ContentTransfer = 'quoted-printable' then
          IdDecoderQuotedPrintable.DecodeStream(Part.DecodedStream, TextBody)
        else if Part.ContentTransfer = 'base64' then
          IdDecoderMIME.DecodeStream(Part.DecodedStream, TextBody)
        else
          Part.DecodedStream.Seek(0, soBeginning); // Reset stream position
        TextBody.LoadFromStream(Part.DecodedStream);
        Break;
      end;
    end;
  finally
    TextBody.Free;
  end;
  1. The resulting TextBody is a TStringList containing the plain text content of the email message.
Memo1.Lines := TextBody;