FileDocCategorySizeDatePackage
MailMonitor.javaAPI DocExample2625Mon Nov 24 09:41:20 GMT 2003com.oreilly.patterns.chapter11

MailMonitor

public class MailMonitor extends Thread

Fields Summary
boolean
interupted
BlockingMailHandler
handler
private static String
username
private static String
password
private static String
mailServer
Constructors Summary
public MailMonitor(BlockingMailHandler mh)

  
     
    handler = mh;
  
Methods Summary
public voidrun()

    Session session = Session.getDefaultInstance(System.getProperties());
    Store store = null;
    try	 {
      store = session.getStore("pop3");
    } catch (NoSuchProviderException nspe) {
      nspe.printStackTrace();
      return;
    }

    while(!interupted) {
      System.out.println("Looping to collect mail");
      try {
        if (!store.isConnected()) // should always be true
          store.connect(mailServer, -1, username, password);
        System.out.println("Connected");  
        Folder folder = store.getDefaultFolder();
        folder = folder.getFolder("INBOX");
        if (folder == null) {
          System.out.println("Unable to open INBOX");
          return;
        }

      System.out.println("Opening folders");
      // Try to open read/write. Open read-only if that fails.
      try {
        folder.open(Folder.READ_WRITE);
      } catch (MessagingException ex) {
        folder.open(Folder.READ_ONLY);
      }

      int totalMessages = folder.getMessageCount();
      int newMessages = folder.getNewMessageCount();
      System.out.println("Total Messages: " + totalMessages);
      try {
        Message messages[] = null;
        messages = folder.getMessages(1, totalMessages);

        for (int i = 0, n = messages.length; i < n; i++) {
          boolean mbDelete = handler.handleMessage(messages[i]);									
          // Delete the message
          if (mbDelete) {
            messages[i].setFlag(Flags.Flag.DELETED, true);
          }
        } // end for
      } catch (MessagingException e) {
        System.out.println("Unable to get Messages");
      }

      // Close the folder and store
      folder.close(true);
      store.close();

      } catch (MessagingException e) {
        System.out.println(e.toString());
        return;
      } 
      
      try {
        this.sleep(10000);
      } catch (InterruptedException e) {
        System.out.println("Exiting");
        return;
      }
    }
  
public voidstopRunning()

    interupted = true;