Example usage for javax.mail Folder open

List of usage examples for javax.mail Folder open

Introduction

In this page you can find the example usage for javax.mail Folder open.

Prototype

public abstract void open(int mode) throws MessagingException;

Source Link

Document

Open this Folder.

Usage

From source file:com.googlecode.gmail4j.javamail.ImapGmailClient.java

/**
 * Returns list of unread/read priority {@link GmailMessage} objects 
 * based on the {@code unreadOnly} value
 * /*w ww.j a  v a  2s  .  c  o m*/
 * @param unreadOnly {@code true} to unread priority {@link GmailMessage} 
 * objects only, {@code false} to read priority {@link GmailMessage} 
 * objects only
 * @return List of unread/read priority messages
 * @throws GmailException if unable to get unread/read priority messages
 */
public List<GmailMessage> getPriorityMessages(boolean unreadOnly) {
    try {
        final List<GmailMessage> priorityMessages = new ArrayList<GmailMessage>();
        final Store store = openGmailStore();
        Folder folder = getFolder(ImapGmailLabel.IMPORTANT.getName(), store);
        folder.open(Folder.READ_ONLY);
        for (final Message msg : folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), !unreadOnly))) {
            priorityMessages.add(new JavaMailGmailMessage(msg));
        }

        return priorityMessages;
    } catch (final Exception e) {
        throw new GmailException("Failed getting priority messages", e);
    }
}

From source file:com.googlecode.gmail4j.javamail.ImapGmailClient.java

@Override
public GmailMessageList getMessagesBy(EmailSearchStrategy strategy, String value) {
    SearchTerm seekStrategy = null;//ww w . ja  va2s  .co m
    switch (strategy) {
    case SUBJECT:
        seekStrategy = new SubjectTerm(value);
        LOG.debug("Fetching emails with a subject of \"" + value + "\"");
        break;
    case TO:
        seekStrategy = new RecipientStringTerm(Message.RecipientType.TO, value);
        LOG.debug("Fetching emails sent to \"" + value + "\"");
        break;
    case FROM:
        seekStrategy = new FromStringTerm(value);
        LOG.debug("Fetching emails sent from \"" + value + "\"");
        break;
    case CC:
        seekStrategy = new RecipientStringTerm(Message.RecipientType.CC, value);
        LOG.debug("Fetching emails CC'd to \"" + value + "\"");
        break;
    case DATE_GT:
        seekStrategy = new SentDateTerm(SentDateTerm.GT, parseDate(value));
        LOG.debug("Fetching emails with a send date newer than \"" + value + "\"");
        break;
    case DATE_LT:
        seekStrategy = new SentDateTerm(SentDateTerm.LT, parseDate(value));
        LOG.debug("Fetching emails with a send date newer than \"" + value + "\"");
        break;
    case DATE_EQ:
        seekStrategy = new SentDateTerm(SentDateTerm.EQ, parseDate(value));
        LOG.debug("Fetching emails with a send date newer than \"" + value + "\"");
        break;
    case KEYWORD:
        seekStrategy = new BodyTerm(value);
        LOG.debug("Fetching emails containing the keyword \"" + value + "\"");
        break;
    case UNREAD:
        seekStrategy = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
        LOG.debug("Fetching all unread emails");
        break;
    }
    try {
        final GmailMessageList found = new GmailMessageList();
        final Store store = openGmailStore();
        final Folder folder = getFolder(this.srcFolder, store);
        folder.open(Folder.READ_ONLY);
        for (final Message msg : folder.search(seekStrategy)) {
            found.add(new JavaMailGmailMessage(msg));
        }
        LOG.debug("Found " + found.size() + " emails");
        return found;
    } catch (final Exception e) {
        throw new GmailException("Failed getting unread messages", e);
    }
}

From source file:de.kp.ames.web.function.access.imap.ImapConsumer.java

/**
 * Retrieve HTML representation of mail message
 * //from   w ww . j a v a 2 s  .c om
 * @param mid
 * @return
 */
public String getHTMLMessage(int mid) {

    String html = null;
    if (store == null)
        return html;

    try {
        /*
         * Connect to IMAP store
         */
        store.connect();

        /*
         * Retrieve & open INBOX folder
         */
        Folder folder = store.getFolder(ImapConstants.INBOX);
        folder.open(Folder.READ_ONLY);

        Message[] messages = folder.getMessages();
        Message m = null;

        for (int i = 0; i < messages.length; i++) {
            if (i == mid)
                m = messages[i];
        }

        if (m == null)
            return html;
        html = mail2HTML(m);

        folder.close(false);
        store.close();

    } catch (Exception e) {
        e.printStackTrace();

    }

    return html;

}

From source file:org.apache.syncope.core.logic.NotificationTest.java

private boolean verifyMail(final String sender, final String subject) throws Exception {
    LOG.info("Waiting for notification to be sent...");
    try {/*w  w  w.ja  v a 2  s . c o  m*/
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    }

    boolean found = false;
    Session session = Session.getDefaultInstance(System.getProperties());
    Store store = session.getStore("pop3");
    store.connect(POP3_HOST, POP3_PORT, MAIL_ADDRESS, MAIL_PASSWORD);

    Folder inbox = store.getFolder("INBOX");
    assertNotNull(inbox);
    inbox.open(Folder.READ_WRITE);

    Message[] messages = inbox.getMessages();
    for (int i = 0; i < messages.length; i++) {
        if (sender.equals(messages[i].getFrom()[0].toString()) && subject.equals(messages[i].getSubject())) {
            found = true;
            messages[i].setFlag(Flag.DELETED, true);
        }
    }

    inbox.close(true);
    store.close();
    return found;
}

From source file:org.opennms.javamail.JavaReadMailer.java

/**
 * <p>retrieveMessages</p>//from  w w  w.  j ava 2 s  .com
 *
 * @return a {@link java.util.List} object.
 * @throws org.opennms.javamail.JavaMailerException if any.
 */
public List<Message> retrieveMessages() throws JavaMailerException {
    Message[] msgs;
    Folder mailFolder = null;

    try {
        m_store = m_session.getStore(m_config.getReadmailHost().getReadmailProtocol().getTransport());
        m_store.connect(m_config.getReadmailHost().getHost(), (int) m_config.getReadmailHost().getPort(),
                m_config.getUserAuth().getUserName(), m_config.getUserAuth().getPassword());
        mailFolder = m_store.getFolder(m_config.getMailFolder());
        mailFolder.open(Folder.READ_WRITE);
        msgs = mailFolder.getMessages();
    } catch (NoSuchProviderException e) {
        throw new JavaMailerException(
                "No provider matching:" + m_config.getReadmailHost().getReadmailProtocol().getTransport()
                        + " from config:" + m_config.getName(),
                e);
    } catch (MessagingException e) {
        throw new JavaMailerException("Problem reading messages from configured mail store", e);
    }

    return new ArrayList<Message>(Arrays.asList(msgs));
}

From source file:org.opennms.javamail.JavaReadMailer.java

/**
 * <p>retrieveMessages</p>/*from  ww  w. j a  v  a2  s  .co  m*/
 *
 * @param term a {@link javax.mail.search.SearchTerm} object.
 * @return a {@link java.util.List} object.
 * @throws org.opennms.javamail.JavaMailerException if any.
 */
public List<Message> retrieveMessages(SearchTerm term) throws JavaMailerException {
    Message[] msgs;
    Folder mailFolder = null;

    try {
        Store store = m_session.getStore(m_config.getReadmailHost().getReadmailProtocol().getTransport());
        store.connect(m_config.getReadmailHost().getHost(), (int) m_config.getReadmailHost().getPort(),
                m_config.getUserAuth().getUserName(), m_config.getUserAuth().getPassword());
        mailFolder = store.getFolder(m_config.getMailFolder());
        mailFolder.open(Folder.READ_WRITE);
        msgs = mailFolder.search(term);
    } catch (NoSuchProviderException e) {
        throw new JavaMailerException(
                "No provider matching:" + m_config.getReadmailHost().getReadmailProtocol().getTransport()
                        + " from config:" + m_config.getName(),
                e);
    } catch (MessagingException e) {
        throw new JavaMailerException("Problem reading messages from configured mail store", e);
    }

    List<Message> msgList = Arrays.asList(msgs);

    return msgList;
}

From source file:de.kp.ames.web.function.access.imap.ImapConsumer.java

/**
 * Retrieve attachment from message id/*from w  w w  . j a va  2s.  c o  m*/
 * 
 * @param mid
 * @return
 */
public FileUtil getAttachment(int mid) {

    FileUtil file = null;
    if (store == null)
        return file;

    try {
        /*
         * Connect to IMAP store
         */
        store.connect();

        /*
         * Retrieve & open INBOX folder
         */
        Folder folder = store.getFolder(ImapConstants.INBOX);
        folder.open(Folder.READ_ONLY);

        Message[] messages = folder.getMessages();
        Message m = null;

        /* 
         * Message message = folder.getMessage(mid); 
         * 
         * the method 'getMessage' always returns an
         * index out of bound error, so we have to
         * introduce the hack below
         */

        for (int i = 0; i < messages.length; i++) {
            if (i == mid)
                m = messages[i];
        }

        if (m == null)
            return file;

        Object content = m.getContent();
        if (content instanceof Multipart) {
            file = handleMultipart((Multipart) content, true);

        } else {
            file = handlePart(m, true);

        }

        folder.close(false);
        store.close();

    } catch (Exception e) {
        e.printStackTrace();

    }

    return file;

}

From source file:com.googlecode.gmail4j.javamail.ImapGmailClient.java

/**
 * Mark all {@link GmailMessage}(s) as read in a folder.
 *
 * @throws GmailException if unable to mark all {@link GmailMessage} as read
 *///from w  ww . j  a v  a 2 s .  com
public void markAllAsRead() {
    Folder folder = null;

    try {
        final Store store = openGmailStore();
        folder = getFolder(this.srcFolder, store);
        folder.open(Folder.READ_WRITE);
        for (final Message message : folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false))) {
            message.setFlag(Flags.Flag.SEEN, true);
        }
    } catch (Exception e) {
        throw new GmailException("ImapGmailClient failed marking" + " all GmailMessage as read", e);
    } finally {
        closeFolder(folder);
    }
}

From source file:com.googlecode.gmail4j.javamail.ImapGmailClient.java

/**
 * Mark a given {@link GmailMessage} as read.
 *
 * @param messageNumber the message number ex:{@code gmailMessage.getMessageNumber()}
 * @throws GmailException if unable to mark {@link GmailMessage} as read
 *//*  ww w  . java  2 s  .  c o m*/
public void markAsRead(int messageNumber) {
    if (messageNumber <= 0) {
        throw new GmailException("ImapGmailClient invalid " + "GmailMessage number");
    }
    Folder folder = null;

    try {
        final Store store = openGmailStore();
        folder = getFolder(this.srcFolder, store);
        folder.open(Folder.READ_WRITE);
        Message message = folder.getMessage(messageNumber);
        if (!message.isSet(Flags.Flag.SEEN)) {
            message.setFlag(Flags.Flag.SEEN, true);
        }
    } catch (Exception e) {
        throw new GmailException("ImapGmailClient failed marking" + " GmailMessage as read : " + messageNumber,
                e);
    } finally {
        closeFolder(folder);
    }
}

From source file:com.googlecode.gmail4j.javamail.ImapGmailClient.java

/**
 * Mark a given {@link GmailMessage} as unread.
 *
 * @param messageNumber the message number ex:{@code gmailMessage.getMessageNumber()}
 * @throws GmailException if unable to mark {@link GmailMessage} as unread
 *///from  ww  w . j  a  v a2s . c  o m
public void markAsUnread(int messageNumber) {
    if (messageNumber <= 0) {
        throw new GmailException("ImapGmailClient invalid " + "GmailMessage number");
    }
    Folder folder = null;

    try {
        final Store store = openGmailStore();
        folder = getFolder(this.srcFolder, store);
        folder.open(Folder.READ_WRITE);
        Message message = folder.getMessage(messageNumber);
        if (message.isSet(Flags.Flag.SEEN)) {
            message.setFlag(Flags.Flag.SEEN, false);
        }
    } catch (Exception e) {
        throw new GmailException(
                "ImapGmailClient failed marking" + " GmailMessage as unread : " + messageNumber, e);
    } finally {
        closeFolder(folder);
    }
}