How to receive emails using indy 10 and delphi 7 with the file attachment?
How to receive emails using i开发者_StackOverflowndy 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.
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.
精彩评论