Retrieving a specified message from the IMAP server

procedure TForm1.btnGetClick(Sender: TObject);
var
  MsgUids: TStrings;
begin
  if clIMAP4.Active then Exit;

  clIMAP4.Server := 'mail.test.com';
  clIMAP4.UserName := 'test';
  clIMAP4.Password := 'test';
  clIMAP4.Open();

  clIMAP4.SelectMailBox('INBOX');

  MsgUids := TStringList.Create();
  try
    clIMAP4.UidSearchMessages('SUBJECT hello', MsgUids);

    if (MsgUids.Count > 0) then
    begin
      clIMAP4.UidRetrieveMessage(MsgUids[0], clMailMessage1);

      ShowMessage('From: ' + clMailMessage1.From.FullAddress + #13#10 +
        'Subject: ' + clMailMessage1.Subject);
    end;
  finally
    MsgUids.Free();
  end;

  clIMAP4.Close();
end;

Feedback

Add Feedback
HelloAt retrieve message have error: "a0003 BAD Could not parse command" or "The command is not valis in this state" or "List index out of bounds", what significant of this errors?In this code example missing SelectMailbox command, at select Mailbox before procedure, find 1 email, but have any emails with this subject in selected mailbox. Any way to select last email with current subject or retrieve all emails with current subject?In Gmail can not to select mailbox "Junk", how to select Junk Mailbox or other Mailbox? Or may be have any way to search message in all mailbox?Thanks
Paulina Olalla (October 21, 2020 at 5:47 PM)
Hello, thank you for your notes. In fact, this code was published incompletely, and we have already fixed this issue. Regarding your question, the IMAP protocol doesn't provide a method to search through all mailboxes. So the only way to search is to preliminary select a mailbox. Please check out the updated example and see how it works.
Sergey Shirokov (November 24, 2020 at 5:52 PM)

Add Feedback