我曾尝试使用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:
我调试它并意识到Folder中的变量总数为零
请帮忙。
感谢。
答案 0 :(得分:0)
向您的电子邮件管理员询问上述电子邮件帐户的“别名”的兑换价值。如果他们,例如将值设置为'香蕉!'那么你必须改变上面的代码来读取store.getFolder(“Bananas!”)而不是store.getFolder(“收件箱”);