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

问题描述 投票:1回答:2

我使用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)

我无法弄清楚如何解决这个问题。我没有进行双向身份验证。而且我也允许使用不太安全的app。所以我无法弄清楚出了什么问题。有人可以帮帮我吗?我非常感谢。

这是我正在使用的代码,

String host = "pop.gmail.com";
String username = "[email protected]";
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);
java gmail javamail pop3
2个回答
1
投票

该错误是由于Google出错,导致POP3服务无法正常工作。它在2天后被修复。

找不到官方声明,只有论坛帖子。相关来源:123


1
投票

我的工作片段如下所示:

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 = "[email protected]";// change accordingly
      String password = "*****";// change accordingly

      check(host, username, password);

   }

}
© www.soinside.com 2019 - 2024. All rights reserved.