我正在开发一个android的电子邮件客户端应用程序。对于这些我使用 Java-Mail API实施 IMAP 客户端。我还添加了folder.addMessageCountListener
,但无法收听新电子邮件&#39 ; S。我也试过 folder.idle(),但没有成功。以下是我使用的代码:
new Thread(new Runnable() {
@Override
public void run() {
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
Properties properties = getServerProperties("imap", "mail.xyz.com", "143");
try {
Session session = Session.getDefaultInstance(properties);
Store store = session.getStore("imap");
store.connect(txt_name.getText().toString().trim(), txt_pwd.getText().toString().trim());
Folder[] f = store.getDefaultFolder().list();
// opens the inbox folder
folderInbox = store.getFolder("INBOX");
folderInbox.open(Folder.READ_WRITE);
//how many emails in the inbox
int count = folderInbox.getMessageCount();
folderInbox.addMessageCountListener(new MessageCountListener() {
@Override
public void messagesAdded(MessageCountEvent messageCountEvent) {
System.out.println("New Mails event fired.");
}
@Override
public void messagesRemoved(MessageCountEvent messageCountEvent) {
}
});
if (folderInbox instanceof IMAPFolder) {
IMAPFolder fol = (IMAPFolder)folderInbox;
fol.idle();
}
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}).start();
这是我的 Java Mail Debug 输出。完成调试报告。
I/System.out: DEBUG: setDebug: JavaMail version 1.6.0
I/System.out: DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
W/art: Before Android 4.1, method javax.mail.Session com.sun.mail.imap.IMAPStore.getSession() would have incorrectly overridden the package-private method in javax.mail.Service
14727-14872/com.imapmailapp I/System.out: DEBUG IMAP: mail.imap.fetchsize: 16384
I/System.out: DEBUG IMAP: mail.imap.ignorebodystructuresize: false
I/System.out: DEBUG IMAP: mail.imap.statuscachetimeout: 1000
I/System.out: DEBUG IMAP: mail.imap.appendbuffersize: -1
I/System.out: DEBUG IMAP: mail.imap.minidletime: 10
I/System.out: DEBUG IMAP: closeFoldersOnStoreFailure
I/System.out: DEBUG IMAP: trying to connect to host "mail.abc.com", port 993, isSSL false
I/System.out: * OK The Microsoft Exchange IMAP4 service is ready.
I/System.out: A0 CAPABILITY
I/System.out: * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
I/System.out: A0 OK CAPABILITY completed.
I/System.out: DEBUG IMAP: AUTH: PLAIN
[ 11-14 19:57:05.837 14727:14872 W/ ]
Unable to open '/system/framework/timakeystore.jar': No such file or directory
W/art: Failed to open zip archive '/system/framework/timakeystore.jar': I/O Error
I/System.out: DEBUG IMAP: protocolConnect login, host=mail.abc.com, user=test@abc.com, password=<non-null>
I/System.out: DEBUG IMAP: AUTHENTICATE PLAIN command trace suppressed
I/System.out: DEBUG IMAP: AUTHENTICATE PLAIN command result: A1 OK AUTHENTICATE completed.
I/System.out: A2 CAPABILITY
I/System.out: * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
I/System.out: A2 OK CAPABILITY completed.
I/System.out: DEBUG IMAP: AUTH: PLAIN
I/System.out: A3 LIST "" "%"
I/System.out: A3 OK LIST completed.
I/System.out: DEBUG IMAP: connection available -- size: 1
I/System.out: A4 SELECT INBOX
I/System.out: * 0 EXISTS
I/System.out: * 0 RECENT
I/System.out: * FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
I/System.out: * OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags
I/System.out: * OK [UIDVALIDITY 14] UIDVALIDITY value
I/System.out: * OK [UIDNEXT 7] The next unique identifier value
I/System.out: A4 OK [READ-WRITE] SELECT completed.
I/System.out: A5 IDLE
I/System.out: + IDLE accepted, awaiting DONE command.
I/System.out: DEBUG IMAP: startIdle: set to IDLE
I/System.out: DEBUG IMAP: startIdle: return true