【问题标题】:Java API works in reading GMAIL any more?Java API 可用于阅读 GMAIL 吗?
【发布时间】:2020-02-18 08:29:03
【问题描述】:

我尝试了不同的 Java(基于 API)程序来读取 Gmail,如下所示,使用正确的电子邮件/密码,但我总是低于异常。 java 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 CheckingMails {
    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 = "correctEmailId@gmail.com";// change accordingly
        String password = "CorrectPassword";// change accordingly

        check(host, mailStoreType, username, password);

    }

}

错误日志:

javax.mail.AuthenticationFailedException: [AUTH] 用户名和密码不被接受。在 com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:209) 在 javax.mail.Service.connect(Service.java:295) 在 javax.mail.Service.connect(Service.java:176) 在zEmails.CheckingMails.main(CheckingMails.java:71) 上的 zEmails.CheckingMails.check(CheckingMails.java:31)

【问题讨论】:

  • javax.mail.AuthenticationFailedException: [AUTH] 用户名和密码不被接受。在 com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:209) 在 javax.mail.Service.connect(Service.java:295) 在 javax.mail.Service.connect(Service.java:176) 在zEmails.CheckingMails.main(CheckingMails.java:71) 上的 zEmails.CheckingMails.check(CheckingMails.java:31)
  • 这是我得到的例外。如果 JAVA API 完全可以在 GMAIL 上工作,请帮忙?
  • 有一个“编辑”按钮可以在问题中而不是在 cmets 中发布异常
  • 您正在使用 POP3。 gmail-imap 因此与它无关。这里的问题显然是用户名或密码,而不是 Java API。讲道理。

标签: java email gmail jakarta-mail


【解决方案1】:

感谢朋友和 Seshidhar G

允许电子邮件 ID 的“低安全应用程序”能够连接和阅读电子邮件。目前我正在处理其余的要求。

【讨论】:

    猜你喜欢
    • 2015-12-10
    • 2015-09-15
    • 2016-01-16
    • 2018-05-11
    • 2015-06-04
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 2017-04-21
    相关资源
    最近更新 更多