I adopted the example "monitor.java" provided: https://javaee.github.io/javamail/#Samples
And I successfully receive new e-mail as they arrive.
Now the thing is, I'd like a way to gracefully stop the monitor and continue processing the received messages.
Most of the topics about the monitor concern the Idle-time in which the server disconnects the monitor.
My case is the opposite. I want to be able to end the monitor if the required message:
- has not been received within n-seconds (time-out)
- is received, and I'm not interested in other messages (satisfied)
For now I'm forcing an exception by calling : store.finalize().
This will give me:
java.lang.IllegalStateException: This operation is not allowed on a closed folder
So here's my question: Can somebody help me with a clean method?
Thank's in advance.
This is the piece of code I created the exception:
// snippet taken from original example:monitor.java
// Just dump out the new messages
for (int i = 0; i < msgs.length; i++) {
try {
System.out.println("-----");
System.out.println("Message " +
msgs[i].getMessageNumber() + ":");
msgs[i].writeTo(System.out);
}
catch (IOException ioex) { ioex.printStackTrace();}
catch (MessagingException mex) { mex.printStackTrace();}
// added code to force an exception in order to stop the monitor
if ( mail.requisted != "Not Found" )
store.finalize()
}