【发布时间】:2017-08-29 06:26:47
【问题描述】:
您好,我正在尝试获取 Gmail 收件箱的所有邮件,但它没有返回所有邮件。它返回一些太旧的邮件。不是最新的。我正在使用以下代码进行测试:
public class ReadEmail {
public static void check(String host, String storeType, final String user, final 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);
emailSession.setDebug(true);
// 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);
// 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";
String mailStoreType = "pop3";
String username = "****@gmail.com";// change accordingly
String password = "****";// change accordingly
check(host, mailStoreType, username, password);
}
}
我没有发现我的代码有什么问题。我已经完成了给定here 的其他设置。
我还想只获取主要标签邮件。如何在我的代码中应用标签级过滤器?
【问题讨论】:
-
主标签?如果您在谈论 gmail,请用标签指定它。
-
@Clijsters:是的,我正在尝试使用 gmail。
标签: java gmail jakarta-mail