javax.mail.AuthenticationFailedException: [AUTH] 需要 Web 登录

javax.mail.AuthenticationFailedException: [AUTH] Web login required

我使用 Java 构建了一个应用程序来阅读电子邮件。它过去几天没有任何错误地工作。但是今天突然出现这样的错误

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)

我不知道如何解决这个问题。我没有进行双向身份验证。而且我还允许使用不太安全的应用程序。所以我不知道出了什么问题。任何人都可以帮助我吗?非常感谢。

这是我使用的代码,

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);

我的工作片段如下所示:

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);

   }

}

错误是由于 Google 处的错误导致 POP3 服务无法正常工作。 2天后修复。

找不到官方声明,只能找到论坛帖子。相关资料: 1, 2, 3

我的问题是相同的代码在本地运行但在远程云(Bitbucket 管道)上不起作用,尽管我设置了不太安全的启用。我通过启用两步验证并创建应用密码解决了这个问题。然后在代码中使用这个应用密码代替普通密码。

您还可以查看以下内容 link:https://docs.maildev.com/article/121-gmail-web-login-required-error---answer78754-failure