无法使用 Java 从 Gmail 检索电子邮件
Unable to retrieve emails from Gmail using Java
我正在尝试在 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 and activation.jar
下载了必要的 .jar 文件
仔细查看了一下,我发现问题可能出在Google这边,这意味着身份验证正在失效。密码和名称是正确的(我已经检查过多次)。我同时启用了 IMAP 和 POP 连接,并且允许安全性较低的连接,但我仍然收到 javax.mail.AuthenticationFailedException
错误。
知道为什么会这样吗?
对您的 Gmail 帐户进行以下更改以从外部访问它
- 登录 Gmail。
- 访问 URL 作为 https://www.google.com/settings/security/lesssecureapps
- Select "Turn on"
您正在 SSL 端口上连接,但未启用 SSL。关注Gmail instructions in the JavaMail FAQ。摆脱端口设置并设置 mail.pop3.ssl.enable
.
并确保您了解 pop3 与 imap 的局限性。
致未来和我一样奋斗的人们:
在应用 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 连接并打开安全性较低的应用程序,如前所述。 ******
我正在尝试在 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 and activation.jar
下载了必要的 .jar 文件仔细查看了一下,我发现问题可能出在Google这边,这意味着身份验证正在失效。密码和名称是正确的(我已经检查过多次)。我同时启用了 IMAP 和 POP 连接,并且允许安全性较低的连接,但我仍然收到 javax.mail.AuthenticationFailedException
错误。
知道为什么会这样吗?
对您的 Gmail 帐户进行以下更改以从外部访问它
- 登录 Gmail。
- 访问 URL 作为 https://www.google.com/settings/security/lesssecureapps
- Select "Turn on"
您正在 SSL 端口上连接,但未启用 SSL。关注Gmail instructions in the JavaMail FAQ。摆脱端口设置并设置 mail.pop3.ssl.enable
.
并确保您了解 pop3 与 imap 的局限性。
致未来和我一样奋斗的人们:
在应用 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 连接并打开安全性较低的应用程序,如前所述。 ******