【问题标题】:Reading email from gmail is not working从 gmail 阅读电子邮件不起作用
【发布时间】:2015-04-09 14:54:57
【问题描述】:

我正在使用 Java Mail API 和以下代码从我的 gmail 帐户中读取电子邮件。

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 CheckMails {

   public static void check(String host, String storeType, String user,
      String password) 
   {
      try {
      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("pop3s");

      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 = "myemai@gmail.com";// change accordingly
      String password = "*******";// change accordingly

      check(host, mailStoreType, username, password);

   }

}

但我得到以下例外:

javax.mail.AuthenticationFailedException: [AUTH] Web login required: `    https://support.google.com/mail/bin/answer.py?answer=78754`
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:209)
at javax.mail.Service.connect(Service.java:364)
at javax.mail.Service.connect(Service.java:245)
at CheckMails.check(CheckMails.java:26)
at CheckMails.main(CheckMails.java:66)

但我在收件箱中收到一封来自 gmail 的电子邮件,内容是 “我们最近阻止了一次登录您的 Google 帐户的尝试”。 如何让程序正常运行?

【问题讨论】:

  • 错误信息的哪一部分不清楚?你读过谷歌的常见问题解答吗?
  • 实际上可能正在发生登录尝试。否则我不会收到来自谷歌的关于登录尝试被阻止的电子邮件。但是为什么会被屏蔽呢?我需要做更多的身份验证吗?我不清楚
  • 老兄 - 看起来你从 this example 复制了你的代码。你要么忘记 - 要么故意选择 - 添加new javax.mail.Authenticator() (?!?)。建议: 1)看看我的链接。 2)查看 Neeraj Jain 的帖子。 3)如果有效,请务必“支持”和“接受”Neeraj Jain 的回复。

标签: java email gmail jakarta-mail


【解决方案1】:

使用

更改您的 emailSession 变量
Session emailSession = Session.getInstance(props, new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(userName, password);
    }
});

正如 Gmail 所说的

某些应用和设备使用安全性较低的登录技术,这使得 您的帐户更容易受到攻击。您可以关闭这些应用的访问权限, 我们推荐,或者如果您想使用它们,请打开访问权限,尽管 风险。了解更多

只需单击下面的链接并禁用 Gmail 安全设置。它会起作用。

Disable Security settings

这是一篇关于Password Authentication in Java Mail Api的好文章

【讨论】:

    猜你喜欢
    • 2015-12-03
    • 2018-05-11
    • 2015-06-04
    • 1970-01-01
    • 2015-03-28
    • 2014-06-03
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    相关资源
    最近更新 更多