无法使用Java

时间:2018-04-17 13:06:50

标签: java gmail javamail

我正在尝试用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.jaractivation.jar

下载了必要的.jar文件

经过一番调查后,我发现这个问题可能来自谷歌方面,这意味着身份验证正在进行中。密码和名称是正确的(我已多次检查)。我已启用IMAP和POP连接,但我允许连接不太安全,但仍然出现javax.mail.AuthenticationFailedException错误。

任何想法为什么会发生这种情况?

3 个答案:

答案 0 :(得分:1)

您在SSL端口上连接但未启用SSL。关注Gmail instructions in the JavaMail FAQ。摆脱端口设置并设置mail.pop3.ssl.enable

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

答案 1 :(得分:0)

在下面更改您的Gmail帐户,以便从外部访问

  1. 登录Gmail。
  2. https://www.google.com/settings/security/lesssecureapps
  3. 访问网址
  4. 选择“开启”

答案 2 :(得分:0)

对于将来会以同样的方式挣扎的人们:

在应用Bill Shannon的建议后,我收到了另一个错误,我发现它是由于我没有正确配置ssl证书而导致的。由于我几乎没有任何线索,我在网上搜索

Properties properties = new Properties();
  properties.put("mail.imap.host", host);
  properties.put("mail.imap.user", user);
  properties.setProperty("mail.imap.ssl.enable", "true");
  properties.put("mail.imap.ssl.trust", "*");

我将pop3协议更改为IMAP,因为后者允许我执行更多任务,但仅出于检索我的电子邮件的目的,两者都以相同的方式工作。

在我的理解中,最后一行将接受任何证书,这可能不是正确的方法,但目前我不知道哪一个是正确的。

谢谢大家的帮助。

******如前所述,用户必须在其帐户中启用POP3 / IMAP连接,并打开安全性较低的应用程序。 ******

相关问题