Solved

Downloading email attachments

Posted on 2016-09-13
2
54 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 25

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Granting full access permission allows users to access mailboxes present in their database. By giving full access permission one can open and read the content of any mailbox but cannot send emails from that mailbox.
Easy CSR creation in Exchange 2007,2010 and 2013
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now