繁体   English   中英

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)

我不知道如何解决这个问题。 我没有放 2 路身份验证。 而且我还允许使用不太安全的应用程序。 所以我无法弄清楚出了什么问题。 任何人都可以帮助我吗? 我非常感激。

这是我正在使用的代码,

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 管道)上运行,尽管我设置了不太安全的启用。 我通过启用两步验证并创建应用程序密码解决了这个问题。 然后在代码中使用此应用程序密码而不是普通密码。

您也可以查看以下链接: https : //docs.maildev.com/article/121-gmail-web-login-required-error---answer78754-failure

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM