Solved

Downloading email attachments

Posted on 2016-09-13
2
79 Views
Last Modified: 2016-09-26
How do I download email attachments from my own WebSite's emails via Delphi.
0
Comment
Question by:Allan_Fernandes
2 Comments
 
LVL 26

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 41796956
I don't want to re-invent a wheel - so here is a newsgroup thread how receiving mail attachment could be accomplished with Indy components..Basically, there is Indy TIdAttachment component which do most part...
0
 

Author Comment

by:Allan_Fernandes
ID: 41808259
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

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nearly six years ago I was hired by a company to be their senior server engineer. One of my first projects was to implement Exchange Server 2007 on a Windows Server 2008 Single Copy Cluster for high availability. That was the easy part; read on to l…
Import PST to Exchange using Power Shell new-mailboximportrequest command, you can simply import the PST file into Exchange mailbox or archived. To know How to import PST into Exchange  2013 read the complete article.
In this video we show how to create a Distribution Group in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >>…
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question