2016-12-22 12 views
0

IMAPを使用してGmailに接続しています。接続は成功し、getMessagesは受信ボックスにあるメッセージの数の配列を返します。しかし、配列の要素にアクセスしようとすると、nullポインタ例外が発生します。Javamail imap getMessagesはnullの配列を返します

しかし、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) 

は、プロトコルのデバッグ(ZohoのメールにIMAP)です:

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; [email protected]; 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 
..... 
+0

Javaメールライブラリのバグに遭ったようです。 BODYRESPONSEを解析できません。プロトコルのデバッグを有効にすることはできますか? – Max

+0

こんにちは、私はポストでデバッグを追加しています –

答えて

1

あなたはしましたクラスパスに古いJavaMailクラスと新しいJavaMailクラスが混在しています。 javax.mailまたはcom.sun.mailクラスを持つjarファイルが1つだけあることを確認してください。

+0

ありがとう!!私はEclipseLinkライブラリに別のjavax.mailを持っていました。私はそれを削除し、今それは動作しています。 –

関連する問題