Retrieve Attachments From Email ( Indy )

Hi, can you show me some example code how to use indy 9 in delphi 7 to retrieve attachments from email account ?
0
eddy
8/2/2013 8:01:36 AM
embarcadero.delphi.winsock 1874 articles. 2 followers. Follow

14 Replies
2475 Views

Similar Articles

[PageSpeed] 15

eddy wrote:

> Hi, can you show me some example code how to use indy 9 in
> delphi 7 to retrieve attachments from email account ?

Code examples are readily available online in various forums, blogs, etc. 
 A simple web search should be able to find them.

All you have to do is download an email into a TIdMessage component using 
TIdPOP3.Retrieve() or TIdIMAP4.Retrieve() (depending on which protocol you 
are using to access the account) and then loop through the TIdMessage.MessageParts 
collection looking for TIdAttachment objects.

--
Remy Lebeau (TeamB)
0
Remy
8/2/2013 4:00:34 PM
<eddy kurniawan> wrote in message news:601263@forums.embarcadero.com...
> Hi, can you show me some example code how to use indy 9 in delphi 7 to 
> retrieve attachments from email account ?

As Remy said you'll need to use a pop3 client to get the mail but once 
you've got it in TIdMessage, this is the basics of what you need:

Msg.MessageParts.CountParts;

For I := 0 to Msg.MessageParts.Count -1 do
   begin
   if Msg.MessageParts[I] is TIdText then
      begin
      //Do your thing..
      end;

   if Msg.MessageParts[I] is TIdAttachment then
      begin
      //Do your thing
      end;
   end;
0
Justin
8/2/2013 10:54:17 PM
thanks for your reply.
i use imap protocol to access my email. i've got an example code from embarcadero archived site :

begin
    IMAP4.Connect;
    try
      if IMAP4.SelectMailBox('INBOX') then
      begin
        msgs := IMAP4.MailBox.MessageList;
        IMAP4.RetrieveAllMsgs(msgs);
        for i := 0 to msgs.Count - 1 do begin
          msgs.Messages[i].MessageParts.CountParts;
          for j := 0 to msgs.Messages[i].MessageParts.Count - 1 do begin 
            if msgs.Messages[i].MessageParts.Items[j] is TIdAttachment then
               // do something
          end;
        end;
      end;
    finally
      IMAP4.Disconnect;
    end;
end;

but why result from msgs.Messages[i].MessageParts.Count always 0 ?
and another problem, if i use imap4.retrieveallmsgs(msgs), when i disconnect imap, it's pop up an error "unrecognized imap4 response header". but when i use imap4.retrieveallheaders(msgs), nothing happens when i disconnect imap.

thanks.
0
eddy
8/3/2013 3:15:38 AM
<eddy kurniawan> wrote in message news:601436@forums.embarcadero.com...
> but why result from msgs.Messages[i].MessageParts.Count always 0 ?

Because apparently sometimes TIdMessage likes to store stuff in the 
TIdMessage.Body property instead just to confuse you. Remy mentioned this to 
me the other day in my topic and explained it there. Here is a copy of what 
he wrote:

"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message 
news:601145@forums.embarcadero.com...

>>> Not all email formats use MessageParts.
>>
>> I wasn't ignoring it, I simply didn't know! How should I test then?
>
> You have to analyze the TIdMessage properties a bit to figure out where 
> things
> are.
>
> if TIdMessage.NoDecode is True, the raw data is in the TIdMessage.Body 
> property.
>
> MIME-encoded emails store nested "multipart/..." types in the 
> TIdMessage.MessageParts
> property, and prolog/epilog text in the TIdMessage.Body property.  But 
> MIME
> emails that do not have nested types (usually true when there are no HTML
> and attachments present) may store text in the TIdMessage.Body property 
> instead.
>
> UUE/XXE-encoded emails (used to be very common in newsgroups) store text
> in the TIdMessage.Body, but store attachments in the 
> TIdMessage.MessageParts.
>
>> The test emails I were sending had both plain text and html text yet
>> MessageParts.Count was set to 0.
>
> The only way that should happen is if TIdMessage.NoDecode is True so the
> raw text gets stored in the TIdMessage.Body.  If a MIME-encoded email has
> both plain text and HTML, and TIdMessage.NoDecode is False, then it should
> be using the TIdMessage.MessageParts instead, where there is a TIdText for
> the plain text and a TIdText for the HTML.

Might be an idea to check message.body <> '' or something?
0
Justin
8/3/2013 8:18:13 AM
Justin wrote:

> As Remy said you'll need to use a pop3 client to get the mail

Or an IMAP client, or a file/TStream.

> once you've got it in TIdMessage, this is the basics of what you need:

> Msg.MessageParts.CountParts;

You don't need that.  IT is only used when *encoding* an email for sending/saving 
(And even then, Indy calls it internally for you).  It is not used when retreiving/loading 
an email.

--
Remy Lebeau (TeamB)
0
Remy
8/4/2013 9:52:44 PM
eddy wrote:

> i use imap protocol to access my email. i've got an example code from
> embarcadero archived site :
<snip>
> but why result from msgs.Messages[i].MessageParts.Count always 0 ?

That depends on the actual contents of the emails, and how well TIdMessage 
is able to parse them.  Maybe the emails are malformed in a way that prevents 
TIdMessage from parsing them correctly.  Maybe the emails are using a format 
that Indy does not recognize/support to begin with.  Maybe the emails simply 
do not have any attachments and/or individual message parts to begin with, 
so the TIdMessage.Body would be used instead of the TIdMessage.MessageParts 
property.  It is really hard to say without seeing the actual emails in question.

> if i use imap4.retrieveallmsgs(msgs), when i disconnect imap, it's pop up
> an error "unrecognized imap4 response header".

Without seeing the actual IMAP socket traffic back and forth, I can only 
speculate, but I would suspect that RetrieveAllMsgs() is likely leaving some 
unread data on the socket, so when Disconnect() sends a LOGOUT command to 
the server, TIdIMAP4 receives and processes that unread data as the LOGOUT 
reply instead of the real LOGOUT reply.

Which version of Indy are you using?  Which version of Delphi?  Are you using 
the version of Indy that shipped with that version of Delphi?  You might 
need to upgrade to a newer Indy release, as there have been quite a number 
of bug fixes (and feature enhancements) made to TIdIMAP4 in the past year.

--
Remy Lebeau (TeamB)
0
Remy
8/4/2013 10:03:43 PM
finally its work well. after i update indy version.
thanks for your help. :)
0
eddy
8/5/2013 8:42:13 AM
"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message 
news:601646@forums.embarcadero.com...
> Justin wrote:

>> Msg.MessageParts.CountParts;
>
> You don't need that.  IT is only used when *encoding* an email for 
> sending/saving
> (And even then, Indy calls it internally for you).  It is not used when 
> retreiving/loading
> an email.

Just to add to this, indy does not call CountParts in TIdSMTPServer (indy9) 
using rmRaw. I'd removed that line today and my attachment count went to 0 
for every email. Put a breakpoint inside CountParts and it isn't called 
once - unless I call it!

Doesn't seem to be called in ProcessMessage() for rmMessageParsed either. Am 
I missing something?
0
Justin
8/21/2013 10:46:07 PM
Justin wrote:

> Just to add to this, indy does not call CountParts in TIdSMTPServer
> (indy9) using rmRaw.

Like I said, CountParts() is only needed when *encoding* an email (and even 
then, TIdMessage calls it internally for you), not when *receiving* an email. 
 And besides, rmRaw does not use TIdMessage to begin with, since the raw 
email data is provided as a TStream instead.

> I'd removed that line today and my attachment count went to 0 for
> every email.

Of course, because there is no counting being performed, and you DON'T NEED 
IT to be performed.  Simply loop through the TIdMessage.MessageParts looking 
for existing TIdAttachment objects.  They are still in there regardless of 
whether CountParts() is called (since it is merely *counting* the existing 
parts, not *creating* parts).  That is all you really need to do.

> Doesn't seem to be called in ProcessMessage() for rmMessageParsed
> either.

It is not supposed to be.

> Am I missing something?

Yes.  A fundamental misunderstanding of what CountParts() actually does and 
when it is meant to be used.

--
Remy Lebeau (TeamB)
0
Remy
8/21/2013 11:23:12 PM
"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message 
news:601646@forums.embarcadero.com...
> Justin wrote:

>> Msg.MessageParts.CountParts;
>
> You don't need that.  IT is only used when *encoding* an email for 
> sending/saving
> (And even then, Indy calls it internally for you).  It is not used when 
> retreiving/loading
> an email.

Just to add to this, indy does not call CountParts in TIdSMTPServer (indy9) 
using rmRaw. I'd removed that line today and my attachment count went to 0 
for every email. Put a breakpoint inside CountParts and it isn't called 
once - unless I call it!

Doesn't seem to be called in ProcessMessage() for rmMessageParsed either. Am 
I missing something?
0
Justin
8/22/2013 10:36:37 AM
Hi Remy

"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message 
news:605798@forums.embarcadero.com...
> Justin wrote:
>
>> Just to add to this, indy does not call CountParts in TIdSMTPServer
>> (indy9) using rmRaw.
>
> Like I said, CountParts() is only needed when *encoding* an email (and 
> even
> then, TIdMessage calls it internally for you), not when *receiving* an 
> email.
> And besides, rmRaw does not use TIdMessage to begin with, since the raw
> email data is provided as a TStream instead.

Yes I remember you saying this previously but from my POV, when you receive 
an email, it would be handy to know how many attachments there are. The 
logic here of the Indy pit crew thinking you wouldn't want to know how many 
attachments you have when receiving an email is baffling.

Fair point about the TIdMessage and rmRaw thing..

>> I'd removed that line today and my attachment count went to 0 for
>> every email.
>
> Of course, because there is no counting being performed, and you DON'T 
> NEED
> IT to be performed.  Simply loop through the TIdMessage.MessageParts 
> looking
> for existing TIdAttachment objects.  They are still in there regardless of
> whether CountParts() is called (since it is merely *counting* the existing
> parts, not *creating* parts).  That is all you really need to do.

ok!

>> Doesn't seem to be called in ProcessMessage() for rmMessageParsed
>> either.
>
> It is not supposed to be.
>
>> Am I missing something?
>
> Yes.  A fundamental misunderstanding of what CountParts() actually does 
> and
> when it is meant to be used.

From my POV there is no need to know how many parts there are when sending - 
as long as the message gets gone most folk won't care. What would be useful 
though to most folk is to know how many attachments they've received. The 
indy way of thinking here (from my POV) seems a bit back to front hence the 
issue coming up again.

From my POV countparts simply does that - counts how many parts there are 
and stores the count internally. Why would you only want to know this when 
sending and not receiving?
0
Justin
8/22/2013 10:36:39 AM
IGNORE THIS ONE!

My Outlook express screwed up and sent this duplicate message.

"Justin Case" <questorx1998-embar@yahoo.com> wrote in message 
news:605907@forums.embarcadero.com...
> "Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message 
> news:601646@forums.embarcadero.com...
>> Justin wrote:
>
>>> Msg.MessageParts.CountParts;
>>
>> You don't need that.  IT is only used when *encoding* an email for 
>> sending/saving
>> (And even then, Indy calls it internally for you).  It is not used when 
>> retreiving/loading
>> an email.
>
> Just to add to this, indy does not call CountParts in TIdSMTPServer 
> (indy9) using rmRaw. I'd removed that line today and my attachment count 
> went to 0 for every email. Put a breakpoint inside CountParts and it isn't 
> called once - unless I call it!
>
> Doesn't seem to be called in ProcessMessage() for rmMessageParsed either. 
> Am I missing something?
0
Justin
8/22/2013 10:38:35 AM
Justin wrote:

> Yes I remember you saying this previously but from my POV, when you
> receive an email, it would be handy to know how many attachments there
> are. The logic here of the Indy pit crew thinking you wouldn't want to
> know how many attachments you have when receiving an email is
> baffling.

Knowing the attachment count (or any part count, for that matter) is not 
a common requirement, so there is no reason to force Indy to waste time always 
counting the parts by default.  You can call CountParts() yourself if you 
really need a count.

> From my POV there is no need to know how many parts there are when
> sending

Actually, there is.  TIdMessage uses TextPartCount and AttachmentCount when 
it needs to decide on a default top-level ContentType for a MIME-encoded 
email when the user has not provided a ContentType.  MIME nesting in TIdMessage 
still relies on a flat single-level collection of message parts, so there 
is a small amount of pre-processing involved to know what message parts are 
actually present in TIdMessage and how they relate to each other.  In Indy 
11, that single-level collection will be replaced with a real tree of true 
nested collections, eliminating all of the guesswork (in both sending and 
receiving).

> What would be useful though to most folk is to know how many attachments
> they've received.

Actually no, most folks don't need or care about that info.  They care about 
what attachments are present, not how many are present.

> From my POV countparts simply does that - counts how many parts there
> are and stores the count internally. Why would you only want to know
> this when sending and not receiving?

Because it is not as useful during receiving as it is during sending (and 
when Indy 11 switches to a real tree of parts, it won't be needed for sending 
anymore).

--
Remy Lebeau (TeamB)
0
Remy
8/22/2013 5:53:39 PM
Hi Remy!

Thanks for your continuing support and explanations - it is appreciated!

"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message 
news:606041@forums.embarcadero.com...
> Justin wrote:
>
>> Yes I remember you saying this previously but from my POV, when you
>> receive an email, it would be handy to know how many attachments there
>> are. The logic here of the Indy pit crew thinking you wouldn't want to
>> know how many attachments you have when receiving an email is
>> baffling.
>
> You can call CountParts() yourself if you
> really need a count.

Ah, thats what I was trying to get at. Previously you said there was no need 
for using Countparts and from that I took it that you meant it *shouldn't* 
be used for some reason. I was using it as in my table I am recording the 
number of files attached which was why I was using countparts in the first 
place and couldn't understand why you seemed to be trying to sway me away 
from it. Of course now you've said that comment above it all makes sense :-)

Thanks for your help!

JC
0
Justin
8/22/2013 6:14:53 PM
Reply:

Similar Artilces:

[Indy] Retrieve attachments from email accounts [Edit]
Hi, I tried to retrieve attachments list from some email accounts (gmail and my own), using both TidPop3 and TidIMAP4: [POP3] .... {code} MsgCont := POP3.CheckMessages; if MsgCont > 0 then begin for i := 1 to MsgCont do begin msg := TIdMessage.Create; if POP3.RetrieveHeader(i,msg) then begin TheEmail := lvEmail.Items.Add; TheEmail.Caption := msg.From.Address; TheEmail.SubItems.Add('No'); msg.MessageParts.CountParts; if...

Problems sending emails with attachments in Indy SMTP in Delphi 2009
Hi, I'm working with Delphi 2009 fully updated (12.0.3420.21218 with Update Pack 3), and I had the original version of the Indy 10.2.5. All attachments were going with a file name ATT000043.dat, instead of the correct name. I updated to the latest Indy from the Dev Snapshot, and it went to version 10.5.7. However, now the attachment does not appear as an attachment at all, the the message appears as: This is a multi-part message in MIME format --HF2WOHttwwLp1TZg=_wwY6H2qbRC0RDTyU Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Content-Disposition: inline H...

Attaching emails as email attachments
Hi, Is this possible? I want to be able to attach a number of emails to another email and send it. A bit like you can do in Outlook where you can just drag the emails you want to send into the new email - they then appear as attachments with the envelope icon. I can attach other kinds of file without any problems, that all works fine. It's just attaching emails that's proving to be a problem. I'd like to do something like this: Dim oldEmail1 As New MailMessage(oldMailFrom1, oldMailTo1, oldSubject1, oldBody1) Dim oldEmail2 As New MailMessage(oldMailFrom2, oldMa...

Print all email attachments (including sub emails and their attachments)
--____OBYWTHBCXVXOFGPGVPTL____ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: base64 Content-Disposition: inline; modification-date="Wed, 31 Aug 2010 07:20:44 -0400" QW55IHdheSB0byBwcmludCBhbGwgZW1haWwgYXR0YWNobWVudCBOQU1FUyBvbmx5IChpbmNsdWRp bmcgYW55IHN1YiBlbWFpbHMgYW5kIHRoZWlyIGF0dGFjaG1lbnRzKSBhbGwgYXQgb25jZSA/Pz8= --____OBYWTHBCXVXOFGPGVPTL____ Content-Type: multipart/related; boundary="____UDFRXZIVSCHQTEJGXAIR____" --____UDFRXZIVSCHQTEJGXAIR____ Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: base64 Content-Dis...

email attachments not attaching
I am authoring a simple online application form which allows a user to upload cover letter and/or resume. The files are then stored in a database and also emailed to the correct person as attachments. The files are being stored in the database with no problems but they are not being attached to the email properly. Below is the snippet of the code used to attach the files and send the mail:// Create the mail message System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("noreply@mydomain.com", OfficeEmailHiddenField.Value, "Application", message); mail.IsBody...

email attachments with Indy [Edit]
Hello - In former days with Delphi 7 I used the standard TIdAttachment.Create(IdMessage1.MessageParts, 'C:\MyFile.com'); to send an attachment with my email. Now with D2010 and Indy 10.5.5 I can get the email to work as well as ever, but I am unable to get the attachment to work. I have searched extensively and have tried various things such as Attachment := TIdAttachmentFile.Create(IdMessage1.MessageParts, 'C:\MyFile.com') and with TIdAttachment(IdMessage1.MessageParts) do begin ContentType := 'text/plain'; ParentPart := -1; Fi...

Can't ready the email attachments which are inside email attachments.
Please tell me this can be fixed. In Thunderbird, when I get an email attachment and open it, it works fine. The email attachment opens and I can read it. However, if that email attachment has an email attachment inside it, it doesn't work. Thunderbird will not allow me to open the email attachment inside the email attachment. If I go over to Outlook and get it there everything works fine. I switched over to Thunderbird, because Outlook was pulling two and three copies of every email in my POP accounts. But if I have to choose between Outlook downloading multiple c...

sending emails with attachement delphi 2009
Hi, I have a problem, I'm using delphi 2009, I made a small application that uses TIdsmtp to send mails, for attachments I'm using TIDAttachmentFile class, like with TIdAttachmentFile.Create(IdMessage1, 'c:\image1.jpg') do begin ContentType := 'image/jpeg'; FileName := 'image1.jpg'; end; SMPT.send(IdMessage1); If I dont use attachment it works fine, but with attachemtns I'm getting email with some dummy name for attachemnt like ATI1284765.dat (if I change file extension to jpg it's ok then, it's that pictu...

How the Attachments are attached to Email Message
Hello Mate, Could you please give me a link to study about 1.Email Attachments and how they are added to the Email message & how to make custom Email classes.2.we are just providing the path of the file to be attached.But how it is uploded to the server and how it is transmitted through the internet to reach the client.3.How to code anti virus for attachments that had been added to the Email4.How to validate attachments for specific type like .txt,.doc etc..Much Obliged & Thanks a Lot,Myself Hi, take a look at these articles: http://www.systemnetmail.com/faq/3.4.aspx. Grz, Kr...

Sending email w/ attachments with Delphi 2?
Does anyone know if there is a version of Indy or any other component that would enable an application written with Delphi 2 to send email with attachments? The references I've come up with just report problems and limitations with it so I'm wondering if anyone knows of something more likely to work. I'm afraid you have to look here: http://gohistoric.com/places/318695 Maybe you take a higher Delphi version and create a DLL with the functionality you want. Use the DLL in your Delphi 2 application. > {quote:title=Philip Caracena wrote:}{quote} > Does anyone know if t...

Sending email attachments through Outlook from Delphi programmatically
Is there some option that has to be set to allow this? I'm using ICS and it works fine for the email, but the attachment doesn't arrive. If I send to Outlook Express client, the attachment arrives. I'm sending through the same Exchange server to both clients. TIA -- -----Jon----- Am 29.01.2010 20:46, schrieb Jon Springs: > Is there some option that has to be set to allow this? > > > I'm using ICS and it works fine for the email, but the attachment doesn't > arrive. If I send to Outlook Express client, the attachment arrives. >...

Email with attachment and HTML email
Hi all, I know to use the Net::SMTP module, but I would like to send an email which has a fiew file attached and to be an HTML type because I want to include an image in it. Do you know which perl module can help me doing that? Thank you. Octavian Rasnita wrote: > I know to use the Net::SMTP module, but I would like to send an email which > has a fiew file attached and to be an HTML type because I want to include an > image in it. > > Do you know which perl module can help me doing that? Try Mail::Sender. -- Greg -------------------------------...

How do I download attachment file with Delphi Indy TIdIMap4
Delphi XE2, Indy 10, using TIdIMap4 to read emails to get attacments. Once I have the list of attachment file names, what is the best way (or any way) to download and store the actual attachments. eg. ================== IDIMap41.UIDRetrieve(mbMsgUID, mbMsgP); for liCount := 0 to mbMsgP.MessageParts.Count-1 do begin if mbMsgP.MessageParts[liCount] is TIdText then begin Memo1.Lines.AddStrings((mbMsgP.MessageParts[liCount] as TIdText).Body); end else begin if mbMsgP.MessageParts[liCount] is TIdAtt...

Email clients not retrieving email
A business client has been running ACT!2000 for a few years as an email client and calendar manager with a common database for all users running on a local server. Since upgrading to Windows XP Pro from Win95 earlier this year the email client has not been able to download any email from his ISP's server. He has been using Outlook Express instead for email but continues to use ACT!2000 for workplace calendar. Incidentally Outlook (not Express) exhibits the same behaviour. The client reports no mail on server, but a check with webmail client reveals mail sitting there waiting for p...

Web resources about - Retrieve Attachments From Email ( Indy ) - embarcadero.delphi.winsock

Attachment theory - Wikipedia, the free encyclopedia
Attachment theory describes the dynamics of long-term relationships between humans . Its most important tenet is that an infant needs to develop ...

Facebook Allowing Page Admins To ‘Refresh Share Attachment?’
Facebook appears to have given page administrators the option of refreshing links that they share in posts on their pages . continued... New ...

Chronik-Fotos - Mama Bean - Unconditional Attachment - Facebook
Graphic design students at the University of North Texas asked this inspiring mama, Monica, to be the face of their ad campaign. The campaign ...

New Facebook Messages Only Provides an Online Attachment Viewing Feature for Microsoft Docs
In another move highlighting Facebook’s strategic relationship with investor Microsoft, the company has given the established office software ...

App Store - Attachments.me- Gmail inbox software to efficiently manage emails, automatically send/upload ...
Get Attachments.me- Gmail inbox software to efficiently manage emails, automatically send/upload files to cloud storage(Dropbox, Box, and G Drive), ...

Team Talk: Attachments, Beliefs and Fears - YouTube
Hi, I'm John Fitzgerald. Welcome to this week's edition of Team WealthBuilders. Since I touched on Attachments, Beliefs and Fears, I've had a ...

Invention of the Day: New ‘Alternative’ Gun Attachment May Prevent Shooting Deaths
The Ferguson Police Department is testing out a new device which they think might help prevent unnecessary shooting deaths. “The Alternative” ...

100th anniversary of Anzac landings brings home strong attachments
The commemoration of the 100th anniversary of the Anzac landings draws our attention to events that so many of us have a strong attachment to. ...

iPhones - Is Our Attachment To Smartphones A Bad Thing?
They cater to our basic human hunger for connection, so is our attachment to mobiles such a bad thing?

Apple not encrypting email attachments in iOS 7, security researcher claims
Apple's Mail app in iOS 7 is failing to encrypt email attachments, leaving user data vulnerable to hackers, a security researcher claims.

Resources last updated: 11/23/2015 8:11:11 PM