0

I build an application using Java to read emails. And It worked without any errors past days. But suddenly today came up an error like this.

javax.mail.AuthenticationFailedException: [AUTH] Web login required: https://support.google.com/mail/answer/78754
        at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:207)
        at javax.mail.Service.connect(Service.java:295)
        at javax.mail.Service.connect(Service.java:176)
        at MailReader.readMail(MailReader.java:44)
        at MailReader.run(MailReader.java:32)
        at java.util.TimerThread.mainLoop(Unknown Source)
        at java.util.TimerThread.run(Unknown Source)

I can't figure out how to fix this. I didn't put 2-way authentication. And also I put less secure app allowed. So I can't figure out what is wrong. Anybody can help me? I greatly appreciate that.

Here is the code I am using,

String host = "pop.gmail.com";
String username = "somename@gmail.com";
String password = "password";

Properties prop = new Properties();
Session session = Session.getInstance(prop, null);
Store store = session.getStore("pop3s");
store.connect(host, username, password);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
  • Did you follow the link in the error message? Did you try *all* the steps described? What happened when you tried each of those steps? – Bill Shannon Feb 20 '19 at 18:10
  • 1
    Google broke something related to POP3 usage of some accounts. The same happened to me and various people all over. We are waiting for a fix. – szako Feb 20 '19 at 20:43
  • @szako Do you think that is the reason for what we are facing now? When will they fix it or anything that we can do to avoid this? –  Feb 21 '19 at 02:51
  • 1
    Looks like Google fixed the issue, at least for me (and for others as I see in forums). If that was the cause you should try again running the code. Other sources related I found: [1](https://productforums.google.com/forum/#!topic/gmail-ru/S5zLz9sapBo;context-place=forum/gmail-ru), [2](https://productforums.google.com/forum/#!msg/gmail-fr/5fweYQrI2Vk/n9bvtXRxBAAJ), [3](http://forum.ixbt.com/topic.cgi?id=24:49492) – szako Feb 21 '19 at 04:34
  • @szako yes mine also fixed. Now I can read the emails. Thank you for the information. –  Feb 21 '19 at 06:14
  • @BillShannon Yes, I tried all the steps but those didn't fix the issue. But now the issue is fixed. Thanks for the support. –  Feb 21 '19 at 09:32
  • Well, don't keep us in suspense! Tell us exactly what it is that fixed the issue! – Bill Shannon Feb 22 '19 at 04:04
  • @BillShannon I think szako is right. I did some code changes and review my google settings but they didn't fix my issue. I changed my code to the original and yesterday morning I ran the program again. Then it is working perfectly. So I don't know what happened exactly, but I think the information gave me by szako are correct. –  Feb 22 '19 at 05:10
  • You can close the question now I think. – szako Feb 22 '19 at 07:31
  • @szako Can't find the correct option from flagging to close. –  Feb 22 '19 at 07:44
  • @Tech Guy: Made an answer. – szako Feb 22 '19 at 08:27
  • @szako Accepted the answer. Thank you for your support. –  Feb 22 '19 at 09:31

3 Answers3

1

My working snippet looks like below:

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;

public class CheckingMails {

   public static void check(String host, String user, String password) 
   {
      try {

      // create properties field
      Properties properties = new Properties();

      properties.put("mail.pop3s.host", host);
      properties.put("mail.pop3s.port", "995");
      properties.put("mail.pop3s.starttls.enable", "true");

      // Setup authentication, get session
      Session session = Session.getInstance(properties,
         new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(user, password);
            }
         });
      // session.setDebug(true);

      // create the POP3 store object and connect with the pop server
      Store store = session.getStore("pop3s");

      store.connect();

      // create the folder object and open it
      Folder emailFolder = store.getFolder("INBOX");
      emailFolder.open(Folder.READ_ONLY);

      // retrieve the messages from the folder in an array and print it
      Message[] messages = emailFolder.getMessages();
      System.out.println("messages.length---" + messages.length);

      for (int i = 0, n = messages.length; i < n; i++) {
         Message message = messages[i];
         System.out.println("---------------------------------");
         System.out.println("Email Number " + (i + 1));
         System.out.println("Subject: " + message.getSubject());
         System.out.println("From: " + message.getFrom()[0]);
         System.out.println("Text: " + message.getContent().toString());
      }

      // close the store and folder objects
      emailFolder.close(false);
      store.close();

      } catch (NoSuchProviderException e) {
         e.printStackTrace();
      } catch (MessagingException e) {
         e.printStackTrace();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   public static void main(String[] args) {

      String host = "pop.gmail.com";
      String username = "abc@gmail.com";// change accordingly
      String password = "*****";// change accordingly

      check(host, username, password);

   }

}
Eritrean
  • 15,851
  • 3
  • 22
  • 28
  • I got the same error again. I added your code and keep this also as same `store.connect(host, username, password);` –  Feb 20 '19 at 12:27
  • @TechGuy This is working for me, though I have no params in connect method. Only `Store store = session.getStore("pop3s"); store.connect();` – Eritrean Feb 20 '19 at 12:44
  • @TechGuy You may login to your gmail account and check if the Security settings are disabled. – Eritrean Feb 20 '19 at 12:47
  • I tried your code. But it gives me this `failed to connect, no password specified?` –  Feb 20 '19 at 16:15
  • Security settings mean like to allow less secure apps? –  Feb 20 '19 at 16:16
  • @TechGuy Yes, I meant allow less secure apps. Sorry, if it didn't help. I will edit my answer and post the class i used which works perfectly fine for me. You can try to find if you are missing something. otherwise I am afraid i can't help. – Eritrean Feb 20 '19 at 16:52
  • Thank you for your answer. But it keeps getting the error again. I don't know why. I tried with `imap` but it also didn't work. –  Feb 20 '19 at 17:07
1

The error was due to an error at Google, which caused POP3 services to work incorrectly. It was fixed after 2 days.

Could not find official statement, only forum posts. Related sources: 1, 2, 3

szako
  • 1,271
  • 1
  • 9
  • 12
0

My problem was that the same code was working on local but not on the remote cloud (Bitbucket pipeline) although I set the less secure enable. I solved it by enabled 2 step verification and create an app password. Then used this app password instead of the normal password in the code.

You can check the following link as well: https://docs.maildev.com/article/121-gmail-web-login-required-error---answer78754-failure

Talip
  • 1
  • 1