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 8931591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:08:49+00:00 2026-06-15T09:08:49+00:00

How can I retrieve a specific e-mail based on a certain text contained in

  • 0

How can I retrieve a specific e-mail based on a certain text contained in the message ? For example how Gmail search works. If you search for a specific text, which is in the e-mail, then the Gmail will retrieve the message that is associated with the text. Preferably without any looping.

  • 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-15T09:08:51+00:00Added an answer on June 15, 2026 at 9:08 am

    You’re looking for the SearchMailBox method. Here’s a simple example expecting that you have the IMAP client (in this case, the IMAPClient variable of the TIdIMAP4 type) already connected to the Gmail server. For those looking for how to do so, take a look for instance at this post and put this code inside the try..finally block near IMAPClient.Connect and IMAPClient.Disconnect.

    var
      // in this example is not shown how to connect to Gmail IMAP server but
      // it's expected that the IMAPClient object is already connected there
      IMAPClient: TIdIMAP4;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      I: Integer;
      MsgObject: TIdMessage;
      SearchInfo: array of TIdIMAP4SearchRec;
    begin
      // if the mailbox selection succeed, then...
      if IMAPClient.SelectMailBox('INBOX') then
      begin
        // set length of the search criteria to 1
        SetLength(SearchInfo, 1);
        // the SearchKey set to skBody means to search only in message body texts
        // for more options and explanation, see comments at the TIdIMAP4SearchKey
        // enumeration in the IdIMAP4.pas unit
        SearchInfo[0].SearchKey := skBody;
        // term you want to search
        SearchInfo[0].Text := 'Search term';
    
        // if the search in the selected mailbox succeed, then...
        if IMAPClient.SearchMailBox(SearchInfo) then
        begin
          // iterate the search results
          for I := 0 to High(IMAPClient.MailBox.SearchResult) do
          begin
            // make an instance of the message object
            MsgObject := TIdMessage.Create(nil);
            try
              // try to retrieve currently iterated message from search results
              // and if this succeed you can work with the MsgObject
              if IMAPClient.Retrieve(IMAPClient.MailBox.SearchResult[I], 
                MsgObject) then
              begin
                // here you have retrieved message in the MsgObject variable, so
                // let's do what what you need with the >> MsgObject <<
              end;
            finally
              MsgObject.Free;
            end;
          end;
        end;
      end;
    end;
    

    Here’s the quick implementation of the IMAP search for UTF-8 charset. It uses interposed class due to protected ParseSearchResult method:

    type
      TBasicSearchKey = (bskBcc, bskBody, bskCc, bskFrom, bskHeader, bskKeyword,
        bskSubject, bskText, bskTo);
    const
      IMAPSearchKeys: array [TBasicSearchKey] of string = ('BCC', 'BODY', 'CC',
        'FROM', 'HEADER', 'KEYWORD', 'SUBJECT', 'TEXT', 'TO');
    type
      TIdIMAP4 = class(IdIMAP4.TIdIMAP4)
      public
        function SearchMailBoxUTF8(const ASearchText: string;
          ASearchKey: TBasicSearchKey): Boolean;
      end;
    
    implementation
    
    { TIdIMAP4 }
    
    function TIdIMAP4.SearchMailBoxUTF8(const ASearchText: string;
      ASearchKey: TBasicSearchKey): Boolean;
    var
      SearchText: RawByteString;
    begin
      Result := False;
      CheckConnectionState(csSelected);
    
      SearchText := UTF8Encode(ASearchText);
      SendCmd(Format('SEARCH CHARSET UTF-8 %s {%d}', [IMAPSearchKeys[ASearchKey],
        Length(SearchText)]), ['SEARCH']);
      if LastCmdResult.Code = IMAP_CONT then
        IOHandler.WriteLn(SearchText, TEncoding.UTF8);
    
      if GetInternalResponse(LastCmdCounter, ['SEARCH'], False) = IMAP_OK then
      begin
        ParseSearchResult(FMailBox, LastCmdResult.Text);
        Result := True;
      end;
    end;
    

    And the usage:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      I: Integer;
      MsgObject: TIdMessage;
    begin
      if IMAPClient.SelectMailBox('INBOX') and
        IMAPClient.SearchMailBoxUTF8('Search term', bskText) then
      begin
        for I := 0 to High(IMAPClient.MailBox.SearchResult) do
        begin
          MsgObject := TIdMessage.Create(nil);
          try
            if IMAPClient.Retrieve(IMAPClient.MailBox.SearchResult[I],
              MsgObject) then
            begin
              // here you have retrieved message in the MsgObject variable, so
              // let's do what what you need with the >> MsgObject <<
            end;
          finally
            MsgObject.Free;
          end;
        end;
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I developp a Java application using Windows Desktop Search from which I can retrieve
I know I can retrieve some settings.xml parameters using properties, like, for example ${settings.localRepository}
I have generic list which must be a preserved order so I can retrieve
I have a blob saved in my database (a text file). I can retrieve
I'm just wondering how I can retrieve a specific value (only 1 thing will
I know that I can retrieve a specific revision of a document via http://localhost:5984/mydb/626b345059c2a54fbe8b8009ba87a409?rev=2-3696048776
I have a lot of pages with text and I have to retrieve specific
Can I make my EF objects retrieve only specific columns in the sql executed?
How can one retrieve the tables' names into a List<string> from a specific database
How can I retrieve a rotation specific applicationFrame/screen bounds? The applicationFrame method always returns

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.