Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

delphi

indy10

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

like image 748
kapil sharma Avatar asked Mar 17 '11 09:03

kapil sharma


2 Answers

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;
like image 133
No'am Newman Avatar answered Oct 22 '22 18:10

No'am Newman


Attachments are stored as TIdAttachment objects in the TIdMessage.MessageParts collection.

like image 31
Remy Lebeau Avatar answered Oct 22 '22 18:10

Remy Lebeau