我曾尝试实现一个使用 pop3 协议读取 Outlook 收件箱的简单程序,但即使有 2 封未读邮件,服务器也会返回零邮件。为什么会出现这个问题?它与 Microsoft Exchange 服务器设置有关吗?
public void retrieveEmail() {
// JavaMail API - Exchange server to allow IMAP access
// mail server connection parameters
String user = "";
String password = "";
// connect to my pop3 inbox
Properties properties = new Properties();
properties.put("mail.store.protocol", "pop3");
properties.put("mail.pop3.host", host);
properties.put("mail.pop3.port", "110");
properties.put("mail.pop3.user", user);
properties.put("mail.pop3.timeout", "158000");
properties.put("mail.pop3.connectiontimeout", "158000");
Session session = Session.getInstance(properties);
session.setDebug(true);
Store store = null;
Folder inbox = null;
try {
store = session.getStore("pop3");
store.connect(host, user, password);
inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
// get the list of inbox messages
int totalMsg = inbox.getMessageCount();
int unread = inbox.getUnreadMessageCount();
Message[] messages = inbox.getMessages();
Message[] msg = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false));
System.out.print("size " + messages .length);
if (messages.length == 0) System.out.println("No messages found.");
for (int i = 0; i < messages.length; i++) {
// stop after listing ten messages
if (i > 10) {
System.exit(0);
inbox.close(true);
store.close();
}
System.out.println("Message " + (i + 1));
System.out.println("From : " + messages[i].getFrom()[0]);
System.out.println("Subject : " + messages[i].getSubject());
Object content = messages[i].getContent();
if (content instanceof String)
System.out.print((String)content);
/* text/plain = String
* multipart" = Multipart
* MimeMessage
* input stream = Unknown Data Handler
*/
}
inbox.close(true);
store.close();
} catch (NoSuchProviderException e) {
System.out.println(e.getMessage());
} catch (MessagingException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
编辑1:
我调试它并意识到文件夹中的变量总数为零
请帮忙。
谢谢。