package using:
com.sun.mail:javax.mail:1.5.6 from maven
I wrote a scala program where I use javax.mail
to deal with emails. In the first part I get some mail id by message.getMessageNumber
and later when I tried to retrieve the mail by these id IndexOutOfBoundsException
happened. Mail server has nothing changed during the process.
Here is the code I get the id of messages.
val Final = new AndTerm(Subject,Size)
//val FinalTerm = new AndTerm(From)
val messages = inbox.search(Final).map{
message=>
val date = trim(message.getSubject)
(date,message.getMessageNumber)
}.filter(_._1.isDefined).map(_._2)
inbox.close(true)
store.close
And here is the code Exception throwed.
//newed another store and Folder with the same name
val ContentType = messages.map(id=>inbox.getMessage(id).getContentType())
inbox.close(true)
store.close
The Exception Message:
Exception in thread "main" java.lang.IndexOutOfBoundsException: 416 > 64
at com.sun.mail.imap.IMAPFolder.checkRange(IMAPFolder.java:513)
at com.sun.mail.imap.IMAPFolder.getMessage(IMAPFolder.java:1770)
at EmailReader.MessageByNumber(EmailReader.scala:67)
at Main$$anonfun$main$1.apply(Main.scala:43)
at Main$$anonfun$main$1.apply(Main.scala:41)
at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:186)
at Main$.main(Main.scala:40)
at Main.main(Main.scala)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
The 416 > 64
gives me a hint that maybe there is some server-side limitation, is that true?
It looks like you're closing the Folder after fetching the Message objects. Message numbers (and Message objects) are only valid while the folder is open.