我无法使用 TIdIMAP4 仅检索未读消息

Mar*_*lot 1 delphi imap indy delphi-10.1-berlin

我正在尝试使用 IMAP 检索所有未读电子邮件,并且我似乎可以连接并找到那些未读邮件(例如,我可以看到 SearchResult 返回与我的 3 个当前未读邮件相对应的 3 个项目),但是 IMAP .Retrieve 调用总是返回 false,不检索它们中的任何一个。

您是否看到我的代码中必须缺少什么?

procedure TForm1.btnUnreadMessagesClick(Sender: TObject);
var i: integer;
    SearchInfo: array of TIdIMAP4SearchRec;
    MSG: TIdMessage;
begin
  Memo1.Lines.Clear;

  IMAP.Host := 'outlook.office365.com';
  IMAP.Port := 993;
  IMAP.Username := 'xxxxx@acme.com';
  IMAP.Password := 'xxxxx';

  SSL.Host := IMAP.Host;
  SSL.Port := IMAP.Port;
  SSL.Destination := SSL.Host + ':' + IntToStr(SSL.Port);
  SSL.MaxLineLength := MaxInt;

  IMAP.IOHandler := SSL;
  IMAP.UseTLS := utUseImplicitTLS;

  IMAP.Connect;
  IMAP.SelectMailBox('INBOX');

  SetLength(SearchInfo, 1);
  SearchInfo[0].SearchKey := skUnseen;
  IMAP.UIDSearchMailBox(SearchInfo);

  for i := 0 to High(IMAP.MailBox.SearchResult) do begin
    MSG := TIdMessage.Create(nil);
    try
      if IMAP.Retrieve(IMAP.MailBox.SearchResult[i], MSG) then begin
        // Here is the problem, I never enter this section 
        Memo1.Lines.Add(MSG.From.Text);
      end;
    finally
      MSG.Free;
    end;
  end;
  IMAP.Disconnect;
end;
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助。

Rem*_*eau 5

使用时SearchMailBox()SearchResult数组包含序列号。

使用时UIDSearchMailBox()SearchResult数组包含 UID。

Retrieve()需要一个序列号。由于您有 UID,请使用UIDRetrieve(),例如:

IMAP.UIDRetrieve(IntToStr(IMAP.MailBox.SearchResult[i]), MSG)
Run Code Online (Sandbox Code Playgroud)