Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Using IMAP for POP3 type operations



Permlink Replies: 7 - Last Post: Jun 23, 2014 4:30 PM Last Post By: Adam Hair
Adam Hair

Posts: 31
Registered: 9/2/00
Using IMAP for POP3 type operations
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 19, 2014 5:56 PM
Hi,

I've got an application that presently uses POP3 to collect messages,
and then delete the emails off the server.

It's used for transmitting and receiving data automatically in the
backgorund and does not require user interaction with the messages.

However there is now a requirement to use IMAP instead of POP3. (Due to
network restrictions at some of my clients).

I'm not overly familiar with IMAP, but effectively my goal is to use it
in a similar method. To collect the message, and then delete it off the
server - the same as POP3. (ie, to use it locally)

Is this a fine practise, and OK to use IMAP like this- or am I getting
myself in trouble.

At present all I know about IMAP is that the messages are normally kept
server side, and that the client application normally 'syncs' the
messages, but they're never removed off the server.

Thanks & Regards

Adam.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Using IMAP for POP3 type operations
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 19, 2014 6:59 PM   in response to: Adam Hair in response to: Adam Hair
Adam wrote:

I'm not overly familiar with IMAP

Indy has a TIdIMAP4 component.

To collect the message

TIdIMAP4.RetrieveAllEnvelopes()
TIdIMAP4.RetrieveEnvelope()
TIdIMAP4.RetrieveEnvelopeRaw()
TIdIMAP4.RetrieveAllHeaders()
TIdIMAP4.RetrieveFirstHeaders()
TIdIMAP4.RetrieveAllMsgs()
TIdIMAP4.RetrieveFirstMsgs()
TIdIMAP4.Retrieve()

TIdIMAP4.UIDRetrieveAllEnvelopes()
TIdIMAP4.UIDRetrieveEnvelope()
TIdIMAP4.UIDRetrieveEnvelopeRaw()
TIdIMAP4.UIDRetrieve()
TIdIMAP4.UIDRetrieveNoDecodeToFile()
TIdIMAP4.UIDRetrieveNoDecodeToStream()

IMAP is also capable of retreiving layout of emails:

TIdIMAP4.RetrieveStructure()
TIdIMAP4.UIDRetrieveStructure()

Which then allows you to retreive portions of data from inside of emails
(which is useful for downloading email content on an as-needed basis, saving
storage space and bandwidth):

TIdIMAP4.RetrievePart()
TIdIMAP4.RetrievePartPeek()
TIdIMAP4.RetrievePartToFile()
TIdIMAP4.RetrievePartToFilePeek()
TIdIMAP4.RetrievePartHeader()
TIdIMAP4.RetrieveText()
TIdIMAP4.RetrieveText2()
TIdIMAP4.RetrieveTextPeek()
TIdIMAP4.RetrieveTextPeek2()

TIdIMAP4.UIDRetrievePart()
TIdIMAP4.UIDRetrievePartPeek()
TIdIMAP4.UIDRetrievePartToFile()
TIdIMAP4.UIDRetrievePartToFilePeek()
TIdIMAP4.UIDRetrievePartHeader()
TIdIMAP4.UIDRetrieveText()
TIdIMAP4.UIDRetrieveText2()
TIdIMAP4.UIDRetrieveTextPeek()
TIdIMAP4.UIDRetrieveTextPeek2()

and then delete it off the server

TIdIMAP4.DeleteMsgs()
TIdIMAP4.UIDDeleteMsg()
TIdIMAP4.UIDDeleteMsgs()
TIdIMAP4.ExpungeMailBox()

Is this a fine practise, and OK to use IMAP like this- or am I getting
myself in trouble.

IMAP is specifically designed for managing mailboxes and their content, so
it has a lot of functionality that POP3 does not. Everything you can do
in POP3 (which is not very much) can be done in IMAP.

Besides, it is not like you have a choice since you are being required to
support IMAP :)

At present all I know about IMAP is that the messages are normally
kept server side

The same is true with POP3. You are not required to delete messages from
a POP3 server, and many POP3 client apps have an option to leave downloaded
messages on the server.

and that the client application normally 'syncs' the messages,
but they're never removed off the server.

Messages can be removed from an IMAP server. In fact, IMAP can even undelete
messages. Deleted messages are simply marked with a deleted flag, and undeleting
simply removes that flag. Messages that are flagged as deleted are not physically
removed from the server until the mailbox is expunged.

--
Remy Lebeau (TeamB)
Adam Hair

Posts: 31
Registered: 9/2/00
Re: Using IMAP for POP3 type operations
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 19, 2014 9:27 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hi Remy,

Excellent - thanks for that. IMAP is a whole new world to me, but if I
can just retrieve the message, and then delete (the same as I would with
POP3) that will make the initial migration quite simple.

I just wasn't sure if there were any 'rules' or traps that I should be
watching out for - but it sounds like it's pretty straight forward from
what you've mentioned.

Have a great weekend, and thanks again for taking the time to help.

Cheers

Adam.
Adam Hair

Posts: 31
Registered: 9/2/00
Re: Using IMAP for POP3 type operations
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 22, 2014 4:39 PM   in response to: Adam Hair in response to: Adam Hair
Hi Remy,

I'm having some problems when it comes to deleting messages. Using the
code below, the messages appear to be marked as deleted (if I log in
using webmail to view) - but everytime I run the script, it downloads
the deleted messages again.

I'm not sure where I've gone wrong and was wondering if you could please
assist:

var
msgs : integer;
i : integer;
msgid : string;
begin
imap.connect;
imap.SelectMailBox('INBOX');
msgs := imap.MailBox.TotalMsgs;

for i := 0 to msgs - 1 do
begin
imap.GetUID(i+1, MsgID);
imap.UIDRetrieve(MsgID, idmessage1);
idmessage1.SaveToFile('c:\temp\msgs\'+msgid+'.txt', false);
imap.UIDDeleteMsg(MsgID);
end;

imap.Disconnect;
end;
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Using IMAP for POP3 type operations
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 23, 2014 11:40 AM   in response to: Adam Hair in response to: Adam Hair
Adam wrote:

I'm having some problems when it comes to deleting messages. Using
the code below, the messages appear to be marked as deleted (if I log
in using webmail to view) - but everytime I run the script, it downloads
the deleted messages again.

You are not expunging the mailbox. I told you earlier that you need to do
that:

and then delete it off the server
TIdIMAP4.DeleteMsgs()
TIdIMAP4.UIDDeleteMsg()
TIdIMAP4.UIDDeleteMsgs()
TIdIMAP4.ExpungeMailBox() ***
...
Messages that are flagged as deleted are not physically removed from the
server until the mailbox is expunged.

--
Remy Lebeau (TeamB)
Adam Hair

Posts: 31
Registered: 9/2/00
Re: Using IMAP for POP3 type operations
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 23, 2014 3:32 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hi Remy,

Thanks for your reply. I guess what I was hoping to do was to mark the
messages as deleted, but not actually expunge them - but not have them
be counted again with the next download with TotalMsgs. (As a
precaution, and as an option to my client)

However I'm beginning to see now, that unless I expunge them - they will
always be counted in the count, and it will get to a stage where there
will be several thousand messages to 'go through' each time, so I might
just expunge them and not give the user an option.

Thanks again for your help!

Best Regards

Adam.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Using IMAP for POP3 type operations
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 23, 2014 3:42 PM   in response to: Adam Hair in response to: Adam Hair
Adam wrote:

Thanks for your reply. I guess what I was hoping to do was to mark the
messages as deleted, but not actually expunge them - but not have them
be counted again with the next download with TotalMsgs.

Sorry, IMAP does not work that way. Neither does POP3, for that matter.

However I'm beginning to see now, that unless I expunge them - they
will always be counted in the count, and it will get to a stage where
there will be several thousand messages to 'go through' each time, so
I might just expunge them and not give the user an option.

Alternatively, you could:

1. mark the messages as 'Deleted', and then the next time you can search
for 'Undeleted' messages.

2. mark the messages as 'Seen' and, and then the next time you can search
for messages that are 'Unseen', or even 'New' (messages that are 'Recent'
and not 'Seen').

3. you can remember the server-side timestamp of the last message you looked
at, and then the next time you can search for messages 'Since' that timestamp.

Have a look at the TIdIMAP4.(UID)SearchMailBox() methods, IMAP offers a lot
of different search criteria.

--
Remy Lebeau (TeamB)
Adam Hair

Posts: 31
Registered: 9/2/00
Re: Using IMAP for POP3 type operations
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 23, 2014 4:30 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hi Remy,

Fantastic - thanks again for the time you've taken to help me!

Have a great evening.

Adam.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02