Class MailboxClient

  • All Implemented Interfaces:
    MailReceiver, java.io.Closeable, java.lang.AutoCloseable, java.lang.Iterable<javax.mail.internet.MimeMessage>

    public final class MailboxClient
    extends MailClientImp
    Wrapper around the JavaMail API supporting all the standard protocols that are available.

    POP3 folders also differ from IMAP folders in how they are manipulated.
    POP3 supports only a single folder named "INBOX".

    POP3 supports no permanent flags ( Folder.getPermanentFlags()).

    In this implementation all messages in a POP3 mailbox are considered new. Therefore in order to avoid re-processing messages, purge(true) should be called prior to disconnection

    If getNextMessage() is invoked, then that message is immediatedly marked as "SEEN" and "DELETED" to avoid processing by other connections.

    Message.saveChanges() is invoked to commit the change to an IMAP folder, if purge(true) is called prior to disconnection, then those messages are deleted.

    It is possible to use this mailbox client to filter messages based on the subject, sender and a pre-defined customHeader field.

    Subject / Sender / recipient / custom Filtering is supported via the use of the org.apache.oro suite, specify your preferred regular expression type using the setRegularExpressionCompiler() method and then you can specify the filter using your preferred regular expression using the various setXXXFilter methods.

     
      MailboxClient pop3 = new MailboxClient
                        ("pop3://username:password@mymailhost:110/INBOX");
      pop3.setSenderFilter("FromMe*");
      pop3.setSubjectFilter("Test*");
      pop3.connect();
      System.out.println(pop3.getMessageCount() + " new matched messages");
      Message payload;
      while ((payload = pop3.getNextMessage()) != null) {
        payload.writeTo(System.out);
      }
      pop3.disconnect();
     
     
    • Constructor Detail

      • MailboxClient

        public MailboxClient​(javax.mail.URLName name)
        Constructor.
        Parameters:
        name - the host to poll for messages pop3://user:pw@hostname:port/INBOX or imap://user:pw@hostname:port/
      • MailboxClient

        public MailboxClient​(java.lang.String mailboxUrl)
        Constructor.
        Parameters:
        mailboxUrl - the POP3 host to poll for messages pop3://user:pw@hostname:port/INBOX or imap://user:pw@hostname:port/
      • MailboxClient

        public MailboxClient​(java.lang.String host,
                             java.lang.String username,
                             java.lang.String password)
        Constructor.

        If this constructor is used, then we assume that a POP3 connection is required.

        Parameters:
        host - the host
        username - the username
        password - the password.
      • MailboxClient

        public MailboxClient​(java.lang.String host,
                             int port,
                             java.lang.String username,
                             java.lang.String password)
        Constructor.

        If this constructor is used, then we assume that a POP3 connection is required.

        Parameters:
        host - the host
        username - the username
        password - the password.
        port - the port
    • Method Detail

      • setSessionProperties

        public void setSessionProperties​(java.util.Properties p)
      • iterator

        public java.util.Iterator<javax.mail.internet.MimeMessage> iterator()
        Description copied from interface: MailReceiver
        Iterate over the list of messages in the mailbox.
        Returns:
        an iterator of filtered messages.
      • disconnectLocal

        protected void disconnectLocal()
        Disconnect from the mail server.
        Specified by:
        disconnectLocal in class MailClientImp