user2618875
user2618875

Reputation: 890

Gmail POP3 not getting all messages in Java application

I have enabled POP3 settings for my gmail. I am able to connect to the POP3 store using my password in a Java app. I have around 10k messages in my inbox.

When I call getMessages on the Inbox folder it returns only 280 old messages. When I call getMessages in a loop, every call returns me same messages. I also tried getMessages(start, end) but it does not return other messages than those 280.

How do I retrieve the other messages?

Upvotes: 8

Views: 2295

Answers (2)

VSO
VSO

Reputation: 12656

Not a real answer, but I got around this by using Mailkit's IMAP. Also, this is C#, not Java code, but maybe it can help people running into the same problem:

      var emails = new List<EmailMessage>();

      using (var client = new ImapClient())
      {
        client.Connect("imap.gmail.com", _smtpConfig.SSLIMAPPort, SecureSocketOptions.SslOnConnect);
        client.ServerCertificateValidationCallback = (s, c, h, e) => true;
        client.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
        client.AuthenticationMechanisms.Remove("XOAUTH2");
        client.Authenticate(smtpConfig.PopUsername, smtpConfig.PopPassword);

        client.Inbox.Open(FolderAccess.ReadWrite);
        var items = client.Inbox.Fetch(0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure);

        foreach (var item in items)
        {
          if (item.TextBody != null)
          {
            var mime = (TextPart)client.Inbox.GetBodyPart(item.UniqueId, item.TextBody);
            var text = mime.Text;

            var email = new EmailMessage
            {
              Body = text
            };

            emails.Add(email);
          }
        }

        client.Disconnect(true);
      }

      return emails;

Thanks to jstedfast - it was all done using his docs.

Upvotes: 0

jstedfast
jstedfast

Reputation: 38608

By default, GMail's POP3 and IMAP server does not behave like standard POP3 or IMAP servers and hides messages from clients using those protocols (as well as having other non-standard behavior).

If you want to configure your GMail POP3 or IMAP settings to behave the way POP3 and IMAP are intended to behave according to their protocol specifications, you'll need to log in to your GMail account via your web browser and navigate to the Forwarding and POP/IMAP tab of your GMail Settings page and set your options to look like this:

POP3 Download

[X] Enable POP for all mail (even mail that's already been downloaded)

IMAP Access

[X] Enable IMAP

[X] Auto-expunge off - wait for the client to update the server

[X] Immediately delete the message forever

Upvotes: 1

Related Questions