IMAP example

Following example works for hotmail and gmail accounts as well. For gmail you need to allow Less Secure App Access here: https://myaccount.google.com/lesssecureapps

execute ibeblock
as
begin
  imapSess = ibec_imap_OpenSession('Host=imap.1und1.de; Port=993;
                                  User=user@email.de; Password=pwd;
                                  UseTLS; UseSSL; UID');
 try
  Res = ibec_imap_Connect(imapSess);
  if (Res) then
  begin
    sFolders = ibec_imap_List(imapSess, '');
    Res = ibec_imap_SelectFolder(imapSess, 'INBOX');
    arrMess = ibec_imap_SearchMessage(imapSess, 'UNSEEN');

    i=1;
    while(i<100) do
    begin
        sMess = ibec_imap_FetchMessage(imapSess, i);
        ibec_ForceDirectories('D:\mails2\'||i||'\');
        ibec_SaveToFile('D:\mails2\'||i||'\mail.eml',sMess,__stfOverwrite);
        ibec_Progress(i);
        eml = ibec_LoadFromFile('D:\mails2\'||i||'\mail.eml');
        mimeMess = ibec_mime_CreateMessage(eml);
        try
          iCount = ibec_mime_GetAttachmentCount(mimeMess);
          while (iCount > 0) do
          begin
            sFIle = ibec_mime_GetAttachmentFileName(mimeMess, iCount-1);
            sData = ibec_mime_GetAttachmentData(mimeMess, iCount-1);
            ibec_SaveToFile('D:\mails2\'||i||'\'||sFIle,sData,__stfOverwrite);
            iCount=iCount-1;
          end
        finally
          ibec_mime_FreeMessage(mimeMess);
        end
      i=i+1;
     end
   end
 finally
   ibec_imap_CloseSession(imapSess);
 end
end