Java Reference
In-Depth Information
Each message will have a set of flags that can then tell whether the message has
been read ( Flags.Flag.SEEN ) or whether the message has been replied to
( Flags.Flag.ANSWERED ). By parsing the SEEN flag, you can then process mes-
sages that haven't been read before.
To set a message as being read (or answered), call the message.setFlag()
method. This method allows you to set (or reset) e-mail flags. If you're setting message
flags, you need to open the folder as READ_WRITE , which allows you to make
changes to e-mail flags. You also need to call inbox.close(true) at the end of
your code, which will tell the JavaMail API to flush the changes to the IMAP store.
Tip For IMAP over SSL, you should use session.getStore("imaps") . This
creates a secure IMAP store.
19-7. Monitoring an E-Mail Account
Problem
You want to monitor when e-mails arrive to a certain account, and you want to process
them depending upon their content.
Solution
Begin with the implementation from Recipe 19-6. Then add IMAP flag manipulation to
create a robust e-mail monitor for your application. In the following example, the
checkForMail() method is used to process mail that is being sent to a mailing list.
In this scenario, users can subscribe or unsubscribe from the list by placing one of
those words in the subject line. The following example checks the subject of new mes-
sages and deals with them appropriately. The example also uses message flags to delete
processed messages so they need not be read twice. Messages that can't be processed
are marked as read but left in the server for troubleshooting by a human.
private void checkForMail() {
System.out.println("Checking for mail");
Properties properties = new Properties();
String username = "username";
Search WWH ::




Custom Search