Search code examples
emaildelphigmailindy

Delphi and Indy 10 unable to read gmail emails 2nd. time after reading once


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;

Solution

  • 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.