Avatar of Allan_Fernandes
Allan_Fernandes
 asked on

Downloading email attachments

How do I download email attachments from my own WebSite's emails via Delphi.
DelphiEmail Servers

Avatar of undefined
Last Comment
Allan_Fernandes

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Sinisa Vuk

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Allan_Fernandes

ASKER
Have tried the code, made some changes too.  I get the all messages successfully.
Problem is that the attachment filenames are blank. Even If I save it with, say 'abc.csv',  then 0 bytes file is created.

procedure TForm1.btn_SaveattachmentsClick(Sender: TObject);
var
   TheUID,fName: string;
   TheMsg: TIdMessage;
   nMsgPartsCount: integer;
begin
  TheUID := StringGrid1.Cells[1, StringGrid1.Selection.Top];

  try
    TheMsg := TIdMessage.Create(nil);

    TheImap.UIDRetrieveStructure(TheUID,TheMsg);

    for nMsgPartsCount := 0 to TheMsg.MessageParts.Count-1 do
    begin
      if TheMsg.MessageParts[nMsgPartsCount] is TIdAttachment then
      begin
        if TheMsg.MessageParts[nMsgPartsCount] is TIdAttachmentFile then
        begin
          fName := TIdAttachmentFile(TheMsg.MessageParts.Items[nMsgPartsCount]).Filename;
          if (fName <> '') then
          begin
            fName := ExtractFilePath(Application.exename)+'download\' + fName;
            TIdAttachmentFile(TheMsg.MessageParts.Items[nMsgPartsCount]).SaveToFile(fName);
            ShowMessage('Attachment saved '+fName);
          end;
        end
        else
          ShowMessage('Attachment ignored - Not recognised as a file');
      end;
    end;
  finally
    TheMsg.Destroy;
  end;
end;

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck