Java API可以再阅读GMAIL吗?

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

我尝试使用不同的Java(基于API)程序读取Gmail,如下所示,并带有正确的电子邮件/密码,但是我总是在Exception以下。 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 = "[email protected]";// 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)

java email gmail javamail
1个回答
0
投票

感谢朋友和Seshidhar G

为emailID启用“安全性较低的应用”可以连接并阅读电子邮件。目前,我正在满足其余要求。

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