How to receive emails using indy 10 and delphi 7 with the file attachment?
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;
Attachments are stored as TIdAttachment
objects in the TIdMessage.MessageParts
collection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With