【发布时间】:2020-07-10 10:44:55
【问题描述】:
我需要阅读来自 Gmail 帐户的电子邮件。我写了以下代码:
public class ReadResponseToEmailTest {
@Test
public void testGmailConnection() throws MessagingException {
Folder emailFolder = null;
Store store = null;
try {
//create properties field
Properties properties = new Properties();
properties.put("mail.pop3.host", "pop.gmail.com");
properties.put("mail.pop3.port", Integer.toString(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 = emailSession.getStore("pop3s");
store.connect("pop.gmail.com", "myemail@gmail.com", "XXXXXXXX");
//create the folder object and open it
emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
// retrieve the messages from the folder in an array and print it
Message foundMessage = null;
Message[] messages = emailFolder.getMessages();
for (final Message msg : messages) {
final String subject = msg.getSubject();
}
}
finally {
if (emailFolder != null) {
emailFolder.close(false);
}
if (store != null) {
store.close();
}
}
}
}
messages 仅包含旧消息。此外,这些邮件大部分不在收件箱中,而是存档。
我需要如何更改上述代码才能阅读 GMail 收件箱中的电子邮件(尤其是最近收到的电子邮件)?
【问题讨论】:
标签: java email gmail jakarta-mail