java - 无法使用 Java 从 Gmail 检索电子邮件

标签 java gmail jakarta-mail

我正在尝试用 Java 编写一个简单的程序,以便检查我的邮箱并获取收件箱中的任何电子邮件。

我阅读了各种教程并观看了 youtube 上的视频,我发现大多数人都会做类似于 this 的事情

所以我已经获取了在那里找到的代码:

import java.util.Properties;

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

public class CheckingMails {

   public static void check(String host, String storeType, String user,
      String password) 
   {
      try {
      //create properties field
      Properties properties = new Properties();
      properties.put("mail.pop3.host", host);
      properties.put("mail.pop3.port", "995");
      properties.put("mail.pop3.starttls.enable", "true");
      Session emailSession = Session.getDefaultInstance(properties);
      //create the POP3 store object and connect with the pop server
      Store store = emailSession.getStore("pop3");
      store.connect(host, user, password);
      //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";// change accordingly
      String mailStoreType = "pop3";
      String username = "giannisthedrummer2@gmail.com";// change accordingly
      String password = "********";// change accordingly
      check(host, mailStoreType, username, password);
   } 
}

但是我收不到邮件。相反,出现此错误:

javax.mail.AuthenticationFailedException: EOF on socket
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:104)
at javax.mail.Service.connect(Service.java:233)
at javax.mail.Service.connect(Service.java:134)
at CheckingMails.check(CheckingMails.java:29)
at CheckingMails.main(CheckingMails.java:69)

我已在电子邮件中启用了 POP 和 IMAP 辅助功能,并启用了安全性较低的连接。

我从 mail.jar 下载了必要的 .jar 文件和 activation.jar

环顾四周后,我发现问题可能出在 Google 这边,这意味着身份验证出现了问题。密码和名称正确(我已检查多次)。我已启用 IMAP 和 POP 连接,并且允许安全性较低的连接,但仍然收到 javax.mail.AuthenticationFailedException 错误。

知道为什么会发生这种情况吗?

最佳答案

您正在 SSL 端口上进行连接,但未启用 SSL。关注Gmail instructions in the JavaMail FAQ 。删除端口设置并设置 mail.pop3.ssl.enable

并确保您了解 pop3 与 imap 的局限性。

关于java - 无法使用 Java 从 Gmail 检索电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49878983/

相关文章:

java - 如何让 Java 使用 Scanner 读取非常大的文件?

java - 井字游戏问题

javascript - 如何在Chrome中放一个类似Gmail的蓝点?

java - 使用带有 accountmanager token 的 imap 访问 gmail

java - 无法使用 javamail api 通过 Gmail 发送邮件

java - 公共(public)代码消除在进行 JIT 时会优化这种双重检查吗?

php - 使用 SMTP 身份验证时通过 PEAR 发送 HTML 消息返回错误

javax.mail.MessagingException : Could not connect to SMTP host : <host name> port : 25 response: 554

JavaMail API 不适用于我,但有异常 com.sun.mail.util.MailConnectException : Couldn't connect to host, 端口

java - JMenu 使用不同的监听器生成菜单