Javamail imap getMessages返回null

时间:2016-12-22 23:49:15

标签: java javamail imap

我正在使用IMAP连接到gmail。连接成功,getMessages返回收件箱中邮件数量的数组。但是,当我尝试访问数组的元素时,我得到一个空指针异常。

但是当我使用pop3时,效果很好。我尝试使用gmail和Zoho邮件,两者都表现得一样。

以下是代码:

Properties props = new Properties();

props.setProperty("mail.store.protocol", "imaps");
props.setProperty("mail.host", "imap.gmail.com");

try {
    Session session = Session.getInstance(props, null);
    Store store = session.getStore();
    store.connect("username", "password");
    Folder inbox = store.getFolder("INBOX");
    inbox.open(Folder.READ_ONLY);

    // get the list of inbox messages
    Message[] messages = inbox.getMessages();

    if (messages.length == 0) System.out.println("No messages found.");
    System.out.println(messages.length + " messages");
    for (int i = 0; i < messages.length && i<10; i++) {

      System.out.println(messages[i].getContent()); // this gets the null pointer exception

    }

    } catch (Exception mex) {
    mex.printStackTrace();
}

这是输出:

1 messages
java.lang.NullPointerException
    at javax.mail.internet.ParameterList.set(ParameterList.java:165)
    at com.sun.mail.imap.protocol.BODYSTRUCTURE.parseParameters(BODYSTRUCTURE.java:404)
    at com.sun.mail.imap.protocol.BODYSTRUCTURE.<init>(BODYSTRUCTURE.java:224)
    at com.sun.mail.imap.protocol.BODYSTRUCTURE.<init>(BODYSTRUCTURE.java:109)
    at com.sun.mail.imap.protocol.FetchResponse.parseItem(FetchResponse.java:198)
    at com.sun.mail.imap.protocol.FetchResponse.parse(FetchResponse.java:164)
    at com.sun.mail.imap.protocol.FetchResponse.<init>(FetchResponse.java:92)
    at com.sun.mail.imap.protocol.IMAPProtocol.readResponse(IMAPProtocol.java:288)
    at com.sun.mail.iap.Protocol.command(Protocol.java:313)
    at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:1543)
    at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:1535)
    at com.sun.mail.imap.protocol.IMAPProtocol.fetchBodyStructure(IMAPProtocol.java:1235)
    at com.sun.mail.imap.IMAPMessage.loadBODYSTRUCTURE(IMAPMessage.java:1294)
    at com.sun.mail.imap.IMAPMessage.getDataHandler(IMAPMessage.java:639)
    at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:923)
    at ar.com.yojunto.backend.tareasProgramadas.chequearVencimiento(tareasProgramadas.java:132)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

这是协议调试(imap到Zoho邮件):

Loading javamail.default.providers from jar:file:/Applications/sts-bundle/pivotal-tc-server-developer-3.1.4.RELEASE/base-instance/wtpwebapps/webapp/WEB-INF/lib/javax.mail_1.4.0.v201005080615.jar!/META-INF/javamail.default.providers
DEBUG: loading new provider protocol=smtp, className=org.apache.geronimo.javamail.transport.smtp.SMTPTransport, vendor=Apache Software Foundation, version=1.0
DEBUG: loading new provider protocol=smtps, className=org.apache.geronimo.javamail.transport.smtp.SMTPTSransport, vendor=Apache Software Foundation, version=1.0
DEBUG: loading new provider protocol=nntp-post, className=org.apache.geronimo.javamail.transport.nntp.NNTPTransport, vendor=Apache Software Foundation, version=1.0
DEBUG: loading new provider protocol=nntp, className=org.apache.geronimo.javamail.store.nntp.NNTPStore, vendor=Apache Software Foundation, version=1.0
DEBUG: loading new provider protocol=pop3, className=org.apache.geronimo.javamail.store.pop3.POP3Store, vendor=Apache Software Foundation, version=1.0
Loading javamail.default.providers from jar:file:/Applications/sts-bundle/pivotal-tc-server-developer-3.1.4.RELEASE/base-instance/wtpwebapps/webapp/WEB-INF/lib/mail-1.4.7.jar!/META-INF/javamail.default.providers
DEBUG: loading new provider protocol=imap, className=com.sun.mail.imap.IMAPStore, vendor=Oracle, version=null
DEBUG: loading new provider protocol=imaps, className=com.sun.mail.imap.IMAPSSLStore, vendor=Oracle, version=null
DEBUG: loading new provider protocol=smtp, className=com.sun.mail.smtp.SMTPTransport, vendor=Oracle, version=null
DEBUG: loading new provider protocol=smtps, className=com.sun.mail.smtp.SMTPSSLTransport, vendor=Oracle, version=null
DEBUG: loading new provider protocol=pop3, className=com.sun.mail.pop3.POP3Store, vendor=Oracle, version=null
DEBUG: loading new provider protocol=pop3s, className=com.sun.mail.pop3.POP3SSLStore, vendor=Oracle, version=null
DEBUG: getProvider() returning provider protocol=imaps; type=javax.mail.Provider$Type@f33b6f; class=com.sun.mail.imap.IMAPSSLStore; vendor=Oracle
DEBUG IMAPS: mail.imap.fetchsize: 16384
DEBUG IMAPS: mail.imap.ignorebodystructuresize: false
DEBUG IMAPS: mail.imap.statuscachetimeout: 1000
DEBUG IMAPS: mail.imap.appendbuffersize: -1
DEBUG IMAPS: mail.imap.minidletime: 10
DEBUG IMAPS: trying to connect to host "imappro.zoho.com", port 993, isSSL true
* OK svwall.zoho.com IMAP4 Server (Zoho Mail IMAP4rev1 Server version 1.0)
A0 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT CHILDREN XLIST NAMESPACE IDLE MOVE ID AUTH=PLAIN SASL-IR UIDPLUS ESEARCH LIST-EXTENDED LIST-STATUS
A0 OK CAPABILITY completed
DEBUG IMAPS: AUTH: PLAIN
DEBUG IMAPS: protocolConnect login, host=imappro.zoho.com, user=***username***, password=<non-null>
DEBUG IMAPS: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAPS: AUTHENTICATE PLAIN command result: A1 OK Success
A2 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT CHILDREN XLIST NAMESPACE IDLE MOVE ID AUTH=PLAIN SASL-IR UIDPLUS ESEARCH LIST-EXTENDED LIST-STATUS
A2 OK CAPABILITY completed
DEBUG IMAPS: AUTH: PLAIN
DEBUG IMAPS: connection available -- size: 1
A3 EXAMINE INBOX
* 1 EXISTS
* 0 RECENT
* OK [UNSEEN 1]
* OK [UIDVALIDITY 1] UIDs valid 
* OK [UIDNEXT 18] Predicted next UID 
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
* OK [PERMANENTFLAGS ()] No permanent flags permitted
A3 OK [READ-ONLY] EXAMINE completed
1 messages
A4 FETCH 1 (BODYSTRUCTURE)
* 1 FETCH (BODYSTRUCTURE (("TEXT" "PLAIN" ("charset" "UTF-8") NIL NIL "QUOTED-PRINTABLE" 197 7 NIL NIL NIL NIL)("TEXT" "HTML" ("charset" "UTF-8") NIL NIL "QUOTED-PRINTABLE" 534 9 NIL NIL NIL NIL) "ALTERNATIVE" ("boundary" "001a1149299c47b400053382c2fb") NIL NIL NIL))
A4 OK Success
java.lang.NullPointerException
.....

1 个答案:

答案 0 :(得分:1)

您的类路径中混合了新旧JavaMail类。确保您只有一个带有javax.mail或com.sun.mail类的jar文件。