Delphi – How to receive emails using indy 10 and delphi 7 with the file attachment

delphiindy10

How to receive emails using indy 10 and delphi 7 with the file attachment?

Best Solution

This is working Indy 10 code. 'Files' is a stringlist which holds a list of attachments which have been downloaded - I'm interested in the attachments, not the letters themselves.

with IdPop31 do
begin
  ConnectTimeout := 5000;
  Connect;
  try
    files.Clear;
    for i := 1 to checkmessages do
    begin
      msg.clear;
      flag := false;
      if retrieve (i, msg) then
      begin
        for j := 0 to msg.MessageParts.Count-1 do
        begin
          if msg.MessageParts[j] is TIdAttachment then
          begin
            with TIdAttachment(msg.MessageParts[j]) do
            begin
              s := IncludeTrailingPathDelimiter(mydir) + ExtractFileName(FileName);
              log ('Downloaded ' + s);
              if not FileExists(s) then
              begin
                SaveToFile(s);
                files.Add(s);
              end;
             end;
            end;
            flag := true;
          end;
        end;
      end;
      if flag then Delete(i);  // remove the email from the server
    end;
  finally
    Disconnect;
  end
end;
Related Question