Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9242477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:35:13+00:00 2026-06-18T08:35:13+00:00

I use the following code to eval the msg. content (body / lines) of

  • 0

I use the following code to eval the msg. content (body / lines) of an E Mail msg received with the INDY 10 components

function LinesFromMsg(aMsg: TIdMessage): TStrings; 
var
  i: Integer; 
begin
  for i := 0 to aMsg.MessageParts.AttachmentCount-1 do
  begin
    if (amsg.MessageParts.Items[i].ContentType ='HTML') then
    begin
      if (amsg.MessageParts.Items[i] is Tidtext) then
        Result := TidText(amsg.MessageParts.Items[i]).body;
    end;
  end; 
end;

regarding this code I have 2 questions :

a) is this the correct way of finding the Tlines part in an arbitray mail message ?
( consider the advice shown at INDY 10 EMAIL MSG PARTS )

b) where can I find a tutorial of all the different Contenttype string values?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:35:13+00:00Added an answer on June 18, 2026 at 8:35 am

    The correct ContentType value to look for is text/html. Use Indy’s IsHeaderMediaType() function to check it, as the ContentType value may have additional attributes associated with it that your comparison needs to ignore.

    You also need to take the TIdMessage.ContentType into account as well, as HTML emails may not be MIME encoded and thus not use the TIdMessage.MessageParts` collection at all.

    And lastly, you loop needs to use the MessageParts.Count property instead of the MessageParts.AttachmentsCount property.

    Try this:

    function HTMLFromMsg(aMsg: TIdMessage): TStrings; 
    var
      i: Integer; 
      Part: TIdMessagePart;
    begin
      Result := nil;
      if IsHeaderMediaType(aMsg.ContentType, 'text/html') then
      begin
        Result := aMsg.Body;
        Exit;
      end;
      for i := 0 to aMsg.MessageParts.Count-1 do
      begin
        Part := aMsg.MessageParts.Items[i];
        if (Part is TIdText) and IsHeaderMediaType(Part.ContentType, 'text/html') then
        begin
          Result := TIdText(Part).Body;
          Exit;
        end;
      end; 
    end;
    

    With that said, this is technically not the correct way to handle MIME. Officially, a conforming reader is supposed to loop backwards through the MIME parts, as they are ordered from the simpliest form downwards towards the most complex form. So you loop backwards, taking MIME nesting into account, looking for the most complex form you support. Something more like this (untested):

    procedure DisplayPlainText(Body: TStrings);
    begin
      // display plain text as needed...
    end;
    
    procedure DisplayHTML(Body: TStrings);
    begin
      // display html as needed...
    end;
    
    procedure DisplayMultiPartAlternative(aMsg: TIdMessage; aParentIndex, aLastIndex: Integer);
    var
      Part: TIdMessagePart;
      i: Integer:
    begin
      for i := aLastIndex-1 downto aParentIndex+1 do
      begin
        Part := aMsg.MessageParts.Items[i];
        if (Part.ParentPart = aParentIndex) and (Part is TIdText) then
        begin
          if IsHeaderMediaType(Part.ContentType, 'text/html') then
          begin
            DisplayHTML(TIdText(Part).Body);
            Exit;
          end;
          if IsHeaderMediaType(Part.ContentType, 'text/plain') then
          begin
            DisplayPlainText(TIdText(Part).Body);
            Exit;
          end;
        end;
      end;
      // nothing supported to display...
    end;
    
    procedure DisplayMultiPartMixed(aMsg: TIdMessage; aParentIndex, aLastIndex: Integer);
    var
      Part: TIdMessagePart;
      i: Integer;
    begin
      for i := aLastIndex-1 downto aParentIndex+1 do
      begin
        Part := aMsg.MessageParts.Items[i];
        if (Part.ParentPart = aParentIndex) and (Part is TIdText) then
        begin
          if IsHeaderMediaType(Part.ContentType, 'multipart/alternative') then
          begin
            DisplayMultiPartAlternative(aMsg, ParentPart.Index, aLastIndex);
            Exit;
          end;
          if IsHeaderMediaType(ParentPart.ContentType, 'text/html') then
          begin
            DisplayHTML(TIdText(Part).Body);
            Exit;
          end;
          if IsHeaderMediaType(Part.ContentType, 'text/plain') then
          begin
            DisplayPlainText(TIdText(Part).Body);
            Exit;
          end;
          aLastIndex := i;
        end;
      end;
      // nothing supported to display...
    end;
    
    procedure DisplayMsg(aMsg: TIdMessage); 
    var
      ContentType: string;
    begin
      ContentType := ExtractHeaderMediaType(aMsg.ContentType);
      case PosInStrArray(ContentType, ['multipart/mixed', 'multipart/alternative', 'text/html', 'text/plain'], False) of
        0: begin
          DisplayMultiPartAlternative(aMsg, -1, aMsg.MessageParts.Count);
          Exit;
        end;
        1: begin
          DisplayMultiPartMixed(aMsg, -1, aMsg.MessageParts.Count);
          Exit;
        end;
        2: begin
          DisplayHTML(aMsg.Body);
          Exit;
        end;
        3: begin
          DisplayPlainText(aMsg.Body);
          Exit;
        end;
      else
        // nothing supported to display...
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the following code... $mail = new PHPMailer(); $mail->IsSMTP(); // send via SMTP
Is it possible to have just normal php code and not use eval() function
I use following code: Create a retry policy, when error, retry after 1 second,
I just use following code to add an image to my project, var paper
I use VS2010, C# to develop Silverlight 4 app, I use following code in
I have an array of objects, and i use following code to get in
I use the following code to crop an image according to a rectangular selection
I use the following code to copy a particular directory and its contents to
I use the following code to fill all empty keys in sub-arrays with ``
I use the following code to add a Music player in my site, <div

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.