kapil sharma
kapil sharma

Reputation: 51

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

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

Upvotes: 5

Views: 9215

Answers (3)

Zikica
Zikica

Reputation: 1

Your code is working fine, but need correction in "begin-end" section where "s" is defining. If "FileName" is empty program has to skip saving. Probably you cut this line and "end" is hanging.

Upvotes: 0

No'am Newman
No'am Newman

Reputation: 6477

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;

Upvotes: 4

Remy Lebeau
Remy Lebeau

Reputation: 595762

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

Upvotes: 4

Related Questions