3

I try to get the number of emails from a gmail account, it works fine for the first time, and every time after, as long I do not disconnect form the server (I stop the execution via debugger before it does the disconnect command). But when I execute the disconnect, I cannot retrieve the email anymore, number of emails is always 0., but the email is still in the inbox when I check online, and I can open the email online and read the content, bit cannot retrieve the email anymore with Indy.

This only happen with gmail, not with other email accounts I tried.

Below a part of the code. pop.CheckMessages returns 1 for the first time, but once I disconnect and start again, it always returns 0.

Does anybody have a clue what I am doing wrong? Its like the mail is somehow marked and cannot be read again via email clients.

pop:=tidpop3.Create(nil);
pop.Host := 'pop.gmail.com';
pop.Port := 995;
pop.Username := MyUserName;
pop.Password := MyPassword;
pop.ConnectTimeout := 10000;
ssl := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
ssl.SSLOptions.Method := sslvTLSv1;
ssl.SSLOptions.Mode := sslmClient;
pop.IOHandler := ssl;
pop.UseTLS := utUseImplicitTLS;
pop.Connect;
num:=pop.CheckMessages;
pop.Disconnect;
pop.Free;
ssl.Free;

2 Answers2

1

In Gmail's settings, there is a section that configures how emails are handled when accessed via POP:

pop

The "When messages are accessed with POP" setting has 4 options:

options

It sounds like you have "delete Gmail's copy" option enabled.

This is specific to Gmail only. Most email providers do not separate their POP and IMAP implementations like this. They usually access just one inbox and keep the two protocols in sync.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • It sounds like OP is still seeing their mail when accessing GMail through other clients, however (ie: webmail, etc - the messages are still on the server and marked unread) but that they cannot access them again via POP. It could be that GMail, regardless of this setting, flags the mail as read by POP and does not offer it again, even in the absence of a `DELE` command... I'll try to find a reference to support this as it's just supposition. – J... Oct 27 '17 at 19:06
  • @J...: that is not what I understand when I read the OP's question. There is nothing said to indicate that other POP/IMAP clients can see the emails after `TIdPOP3` has been used to access them. The question specifically states that when `TIdPOP3` connects, then disconnects, then reconnects, the earlier emails are gone. That implies Gmail's "delete Gmail's copy" setting is in effect. Other clients are not involved. "Check online" means the Gmail webmail interface, which does not use POP3 or IMAP, – Remy Lebeau Oct 27 '17 at 19:20
  • They state in the question `...when I check online, and I can open the email online and read the content, bit cannot retrieve the email anymore with Indy.` See the updates to my answer. – J... Oct 27 '17 at 19:21
  • I did more testing, the emails are really on the server, I can open gmail via browser and see them in my inbox, can open them etc., but no client, even not Outlook, can read them anymore after I read them once with Indy. –  Oct 28 '17 at 14:00
0

POP removes the email from the server - you only read it once (at least in most implementations - GMail separates POP and IMAP functionality which is slightly confusing and why you still see the mail online. From a POP viewpoint the mail is gone from the server but Google keeps it there anyway, flagged as dead for POP, to not break IMAP functionality). Use IMAP if you want to leave the mail on the server and remain sync'd with the server mail state.

In a traditional POP session you must explicitly tell the server that you wish to delete the server copy after reading it - clients were often configured to do this automatically as this was a common way to use POP. With TIdPOP3 you must explicitly call IdPOP3.Delete(), but GMail's implementation of POP is somewhat different.

How does normal mode work?

A POP client session starts with your mail client (Thunderbird, Outlook, Sparrow, etc.) asking your Gmail mailbox for a list of messages that haven't yet been downloaded. After Gmail provides the list of messages to your mail client, your client will begin downloading them. In POP normal mode, Gmail provides a list of about 250 of the oldest messages that have not yet been downloaded (Spam and Trash are excluded). Once a message is downloaded, Gmail marks it as 'popped'.

So, in "normal" mode GMail will only send a message once via POP. To access mail still on the server but which has already been 'popped' you have the option with GMail of using recent mode(see link above).

What happens to my messages in Gmail after they've been popped?

When using recent mode, 'popped' messages (downloaded in normal mode) will still be shown to mail clients. This means that even if one POP client (that uses normal mode) marks a message as popped, another POP client (that uses recent mode) will still be able to see the message (unless you've set Gmail to delete messages that have been downloaded via POP in the When messages are accessed with POP option, in which case the message will be sent to Trash after it's been downloaded by the POP client in normal mode).

Unlike in normal mode, you must set your POP client to leave messages on the server (and not delete them), because when a POP client issues a DELE (delete) command in recent mode, it is sent to Trash in Gmail, regardless of the user's When messages are accessed with POP setting. If one of the POP clients deletes messages, they won't be visible to the other POP clients ever again (unless moved out of Trash).

Remy's answer gives more detail on the When messages are accessed with POP setting.

POP is an ancient dinosaur from the last century. It was designed when server space was expensive, internet speeds were horrendously slow, and people generally had only one computer. Using POP was like using the server as a mailbox, literally. You would check for mail and download all messages to a local client, deleting them from the server (emptying the mailbox). This saved space on the server and made checking for new mail faster but meant that all your mail ended up stored on whatever computer you used to check your mail and that's the only place it existed. The times have changed - POP should generally be considered dead. Just don't use it.

Community
  • 1
  • 1
J...
  • 30,968
  • 6
  • 66
  • 143
  • 1
    POP itself does not remove emails unless the client explicitly deletes them with a `DELE` command AND ends the session via a `QUIT` command so deletes are processed (if the POP session ends abnormally, pending deletes are ignored). Since the code shown is not calling `TIdPOP3.Delete()`, the emails should be left on the server. However, Gmail is special in that it has an option to automatically delete emails that are accessed via POP. Make sure that option is turned off in your Gmail settings. – Remy Lebeau Oct 27 '17 at 18:46
  • @RemyLebeau I've updated with that and some extra information. – J... Oct 27 '17 at 19:17
  • "***unless you've set Gmail to delete messages that have been downloaded via POP** in the `When messages are accessed with POP` option, in which case the message will be sent to Trash after it's been downloaded by the POP client in normal mode*" - that is the likely culprit for the behavior being seen. – Remy Lebeau Oct 27 '17 at 19:23
  • @RemyLebeau No, because they can still see the messages in their inbox using the webmail client. They are using `normal mode` instead of `recent mode`, the latter would still return unpopped messages in the inbox. – J... Oct 27 '17 at 19:24
  • @RemyLebeau It's like GMail's default behaviour (normal mode) is to act as though the client is set up as an auto-DELE client regardless of whether the client actually sends a DELE command. In either case the behaviour for the *actual* messages is solely dictated by the `When messages are accessed with POP` setting, but for normal mode POP clients it is always as though a DELE was sent (they aren't seen again by a POP client). – J... Oct 27 '17 at 19:37
  • @RemyLebeau In "recent mode", though, the POP client interacts in what we would probably consider a more normal POP way, where DELE actually moves the messages to Trash (instead of strict deletion) but otherwise still retrieves any messages that are still on the server (which is what you would expect to happen). – J... Oct 27 '17 at 19:39