Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lazarus IMAP i got error
#1
Hello i tried to read email with idimap indy on lazarus but i get this error..
 
unable to execute command  wrong connection state, current connection state authenticated 

procedure TFormMain.getunreademails(Sender: TObject);
Var  lj,i,id,attachs:Integer;
    CurrUId:String;
    msg: TIdMessage;
    BodyTexts: TStringList;
    lParts:TIdImapMessageParts;
    SearchInfo: array of TIdIMAP4SearchRec;
begin
//  if( IdIMAP.Connected=false) then begin
//    IdIMAP.Disconnect();
//    sleep(200);
//    IdIMAP.Connect();
//    FolderList:= TStringList.Create;
//    IdIMAP.StatusMailBox('Inbox', IdIMAP.MailBox);
//    IdImap.ListMailBoxes(FolderList);
//    IdImap.SelectMailBox('Inbox');
//  end;

  if(IDIMAP.Connected=false) then begin;
    SetLength(SearchInfo, 1);
    SearchInfo[0].SearchKey := skUnseen;
    IDIMAP.UIDSearchMailBox(SearchInfo);
  end else begin
    SetLength(SearchInfo, 1);
    SearchInfo[0].SearchKey := skUnseen;
    IDIMAP.UIDSearchMailBox(SearchInfo);  // here is generated the error
  end;
  lParts := TIdImapMessageParts.Create(nil);
  if( IDImap.Connected=true) then begin
    for i := 0 to High(IDIMAP.MailBox.SearchResult) do begin
        MSG := TIdMessage.Create(nil);
        Id:=IDIMAP.MailBox.SearchResult[i];
        CurrUId:=Inttostr(IDIMAP.MailBox.SearchResult[i]);
        try

            if( not BdEmails.Locate('UID', currUID , [loCaseInsensitive])) then begin
              IDIMAP.UIDRetrieve( CurrUID, msg );
              IDImap.UIDRetrieveStructure(Inttostr(IDIMAP.MailBox.SearchResult[i]), lParts);
              attachs:=0;
              for lj := 0 to msg.MessageParts.Count - 1  do begin
                if msg.MessageParts.Items[lj] is TIdAttachment then  begin
                    if( lParts[lJ].FileName <>'') then begin
                        Inc(attachs);
                        IDImap.UidRetrievePartToFile(Inttostr(IDIMAP.MailBox.SearchResult[i]), lParts[lJ].ImapPartNumber, i , Path+'emails\attach\'+lParts[lJ].FileName, lParts[lJ].ContentTransferEncoding);
                        BDAttachs.Append;
                        BDAttachs.FieldByName('Account').AsString  :=BDAcct.FieldByName('emailAcc').AsString ;
                        BDAttachs.FieldByName('ID').AsInteger      :=id ;
                        BDAttachs.FieldByName('UID').AsString      := CurrUID;
                        BDAttachs.FieldByName('Attachs').AsInteger := lj;
                        BDAttachs.FieldByName('FileName').AsString := lParts[lJ].FileName;
                        BDAttachs.post;
                    end;
                  end;
              end;
//              fileName_MailSource := TmpFolder  + Inttostr(IDIMAP.MailBox.SearchResult[i])+ '.eml';
              BDEmails.Append;
              BDEmails.FieldByName('Account').AsString  := BDAcct.FieldByName('emailAcc').AsString;
              BDEmails.FieldByName('ID').AsInteger      := id;
              BDEmails.FieldByName('UID').AsString      := currUID ;
              BDEmails.FieldByName('Date').AsDateTime  := msg.Date;
              BDEmails.FieldByName('subject').AsString  := msg.Subject;
              BDEmails.FieldByName('attachs').AsInteger  := attachs;
              BDEmails.FieldByName('fromaddr').AsString := msg.From.Address;
              BDEmails.post;
          end;

        finally
          MSG.Free;
        end;

    end;
  end;
  BDEmails.First;

end;         


some examples to read unseen emails.... still using query search,.. or what i need to change to get that...

i can not download documentation of indy...

thanks..
any help great...


Attached Files Thumbnail(s)
   
Reply
#2
(11-28-2023, 05:31 PM)eldonfsr Wrote:   if(IDIMAP.Connected=false) then begin;
    SetLength(SearchInfo, 1);
    SearchInfo[0].SearchKey := skUnseen;
    IDIMAP.UIDSearchMailBox(SearchInfo);
  end else begin
    SetLength(SearchInfo, 1);
    SearchInfo[0].SearchKey := skUnseen;
    IDIMAP.UIDSearchMailBox(SearchInfo);  // here is generated the error
  end;

You have an erroneous ; on the begin of your if statement (I'm surprised the code compiles at all). You need to remove that ;

if(IDIMAP.Connected=false) then begin; // <-- HERE

Besides that, you are executing UIDSearchMailBox() regardless of whether Connected is True or False.  The error message is telling you that are in the Authenticated state, which means you don't have a mailbox selected.  You need to select a mailbox before you can search its emails. There is no command to search an un-selected mailbox.

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)