Home > Enterprise >  How to search email's SUBJECT or BODY with non-ASCII characters through TIdIMAP4
How to search email's SUBJECT or BODY with non-ASCII characters through TIdIMAP4

Time:09-17

TIdIMAP4.UIDSearchMailBox() works perfectly for ASCII characters, but for non-ASCII characters, such as in Persian/Farsi, the server doesn't return anything.

For example, when I search for emails that contain "foo" in the body, it works. But when I try to search for "سودابه", it does not work.

The IMAP command for my search is:

C9 UID SEARCH CHARSET UTF-8 UID 84:* BODY {N}\r\nsomething in Farsi that is encoded with UTF-8

Where 84:* is the range of email UIDs, N is the byte count of the UTF-8 encoded search text, and \r\n is a CRLF line break. The command will be submitted in two steps.

I'm pretty sure that there are some emails matching this search criteria.

I saw this issue on Gmail, Microsoft Exchange, and hMail servers.

Do you have any idea?

Here is abstract of my codes:

procedure TForm1.btn_GetMailsBySearchClick(Sender: TObject);
begin
   :
   DoLog('Searching mail box started');
   if not id_IMAP.UIDSearchMailBox(FSearchInfoArray) then
   begin
     DoLog(SearchMailBox failed, True);
     Exit;
   end;   
   :
end;

procedure TForm1.DoLog(const ADesc: String; ADoLogLastResponse: Boolean);
var
  AStr: String;
begin
  :
  AStr := Format('[%s]: %s', [DateTimeToStr(Now), ADesc]);
  if ADoLogLastResponse then 
    AStr := AStr   Format('. Last response is "%s"', [GetLastResponseStr]);
  :  //saving AStr in file or memo 
end;

function TForm1.GetLastResponseStr: String;
var
  i: Integer;
begin
  Result := '';
  with id_IMAP.LastCmdResult.Text do
    for i := 0 to Count - 1 do
    begin
      if Result <> '' then Result := Result   #13#10;
      Result := Result   Strings[i];
    end;
end; 

I didn't get any response via this approach, Did I go to wrong way?

CodePudding user response:

My problem is solved for hMail server by upgrading from version 3 to 4. but on other mail services is still remained.

CodePudding user response:

As per RFC3501 §6.4.4, you have to expect 3 types of responses:

  • OK = server understands and supports your request; no search results matched, though

  • NO = either the CHARSET or the search criteria is understood but not supported

  • BAD = command or arguments are not supported; sloppy server implementations might return this for an unsupported CHARSET

So, are you actually looking at what the server returns (i.e. thru .GetResponse()? Because "it does not work" might be missing that you have different expectations from what actually works as intended.

  • Related