11-28-2023, 05:31 PM
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...
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...