Atozed Forums

Full Version: Searching two string at a time in the Object of emails
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
  with SearchMailBox is possible to search emails with some criteria.

I need to search emails that Object contains the string "ACC: " and emails that Object contains the string "CON: ".

Can i search both strings at a time?

This code
Code:
  SearchInfo.Date := 0;
  SearchInfo.Size := 0;
  SearchInfo.SearchKey := skUnseen;
  SearchInfo.Text := '';
  SearchInfoArray[0] := SearchInfo;

  SearchInfo.SearchKey := skSubject;
  SearchInfo.Text := 'ACC: ';
  SearchInfoArray[1] := SearchInfo;

  SearchInfo.SearchKey := skSubject;
  SearchInfo.Text := 'CON: ';
  SearchInfoArray[2] := SearchInfo;

  if IMAP.SearchMailBox(SearchInfoArray) then
is right?

It seems not right because I dont find the emails.
if i search only one string at a time i found the emails.

Where I'm wrong?

Thanks
(01-07-2022, 06:22 PM)luigisic Wrote: [ -> ]Can i search both strings at a time?

When you specify multiple criteria to the IMAP SEARCH command, it returns the intersection (AND) of all matching criteria. Per RFC 3501 Section 6.4.4:

Quote:When multiple keys are specified, the result is the intersection (AND function) of all the messages that match those keys. For example, the criteria DELETED FROM "SMITH" SINCE 1-Feb-1994 refers to all deleted messages from Smith that were placed in the mailbox since February 1, 1994. A search key can also be a parenthesized list of one or more search keys (e.g., for use with the OR and NOT keys).

So, in your example, you are searching for emails that:
  • are marked as Unseen
  • AND whose subject contains 'ACC: '
  • AND whose subject contains 'CON: '

Which I suspect is probably not what you want. You probably want something more like this instead:
  • are marked as Unseen
  • AND whose subject contains EITHER 'ACC: ' OR 'CON: '

The SEARCH command has an OR <search-key1> <search-key2> search-key for that very purpose, so try this:

Code:
SetLength(SearchInfoArray, 4);
SearchInfo.Date := 0;
SearchInfo.Size := 0;
SearchInfo.Text := '';

SearchInfo.SearchKey := skUnseen;
SearchInfoArray[0] := SearchInfo;

SearchInfo.SearchKey := skOr;
SearchInfoArray[1] := SearchInfo;

SearchInfo.SearchKey := skSubject;
SearchInfo.Text := 'ACC: ';
SearchInfoArray[2] := SearchInfo;

SearchInfo.SearchKey := skSubject;
SearchInfo.Text := 'CON: ';
SearchInfoArray[3] := SearchInfo;

if IMAP.SearchMailBox(SearchInfoArray) then
(01-07-2022, 07:11 PM)rlebeau Wrote: [ -> ]Which I suspect is probably not what you want.  You probably want something more like this instead:
  • are marked as Unseen
  • AND whose subject contains EITHER 'ACC: ' OR 'CON: '

Yes, You are right.

(01-07-2022, 07:11 PM)rlebeau Wrote: [ -> ]The SEARCH command has an OR <search-key1> <search-key2> search-key for that very purpose, so try this:

Code:
SetLength(SearchInfoArray, 4);
SearchInfo.Date := 0;
SearchInfo.Size := 0;
SearchInfo.Text := '';

SearchInfo.SearchKey := skUnseen;
SearchInfoArray[0] := SearchInfo;

SearchInfo.SearchKey := skOr;
SearchInfoArray[1] := SearchInfo;

SearchInfo.SearchKey := skSubject;
SearchInfo.Text := 'ACC: ';
SearchInfoArray[2] := SearchInfo;

SearchInfo.SearchKey := skSubject;
SearchInfo.Text := 'CON: ';
SearchInfoArray[3] := SearchInfo;

if IMAP.SearchMailBox(SearchInfoArray) then

This code work as I want.

You are an irreplaceable resource Smile

Thanks