Example usage for javax.mail.search FlagTerm FlagTerm

List of usage examples for javax.mail.search FlagTerm FlagTerm

Introduction

In this page you can find the example usage for javax.mail.search FlagTerm FlagTerm.

Prototype

public FlagTerm(Flags flags, boolean set) 

Source Link

Document

Constructor.

Usage

From source file:org.pentaho.di.job.entries.getpop.MailConnection.java

public void setFlagTermNotDraft() {
    addSearchTerm(new FlagTerm(new Flags(Flags.Flag.DRAFT), false));
}

From source file:org.springframework.integration.mail.ImapMailReceiverTests.java

@Test
public void testIdleWithServerCustomSearch() throws Exception {
    ImapMailReceiver receiver = new ImapMailReceiver(
            "imap://user:pw@localhost:" + imapIdleServer.getPort() + "/INBOX");
    receiver.setSearchTermStrategy((supportedFlags, folder) -> {
        try {//  w w  w .j a v a 2s. c o m
            FromTerm fromTerm = new FromTerm(new InternetAddress("bar@baz"));
            return new AndTerm(fromTerm, new FlagTerm(new Flags(Flag.SEEN), false));
        } catch (AddressException e) {
            throw new RuntimeException(e);
        }
    });
    testIdleWithServerGuts(receiver, false);
}

From source file:org.springframework.ws.transport.mail.monitor.AbstractMonitoringStrategy.java

/**
 * Retrieves new messages from the given folder. This implementation creates a {@link SearchTerm} that searches for
 * all messages in the folder that are {@link javax.mail.Flags.Flag#RECENT RECENT}, not {@link
 * javax.mail.Flags.Flag#ANSWERED ANSWERED}, and not {@link javax.mail.Flags.Flag#DELETED DELETED}. The search term
 * is used to {@link Folder#search(SearchTerm) search} for new messages.
 *
 * @param folder the folder to retrieve new messages from
 * @return the new messages/*from   w  w  w.  j  a va  2  s .c o  m*/
 * @throws MessagingException in case of JavaMail errors
 */
protected Message[] searchForNewMessages(Folder folder) throws MessagingException {
    if (!folder.isOpen()) {
        return new Message[0];
    }
    Flags supportedFlags = folder.getPermanentFlags();
    SearchTerm searchTerm = null;
    if (supportedFlags != null) {
        if (supportedFlags.contains(Flags.Flag.RECENT)) {
            searchTerm = new FlagTerm(new Flags(Flags.Flag.RECENT), true);
        }
        if (supportedFlags.contains(Flags.Flag.ANSWERED)) {
            FlagTerm answeredTerm = new FlagTerm(new Flags(Flags.Flag.ANSWERED), false);
            if (searchTerm == null) {
                searchTerm = answeredTerm;
            } else {
                searchTerm = new AndTerm(searchTerm, answeredTerm);
            }
        }
        if (supportedFlags.contains(Flags.Flag.DELETED)) {
            FlagTerm deletedTerm = new FlagTerm(new Flags(Flags.Flag.DELETED), false);
            if (searchTerm == null) {
                searchTerm = deletedTerm;
            } else {
                searchTerm = new AndTerm(searchTerm, deletedTerm);
            }
        }
    }
    return searchTerm != null ? folder.search(searchTerm) : folder.getMessages();
}

From source file:org.wso2.esb.integration.common.utils.MailToTransportUtil.java

/**
 * Delete all unread emails from inbox//from  w  w w. j  a  va2s. c o  m
 *
 * @throws ESBMailTransportIntegrationTestException - Is thrown if an error when deleting the emails
 */
public static void deleteAllUnreadEmailsFromGmail() throws ESBMailTransportIntegrationTestException {
    Folder inbox = null;
    Store store = getConnection();
    try {

        inbox = store.getFolder(EMAIL_INBOX);
        inbox.open(Folder.READ_WRITE);
        Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));

        for (Message message : messages) {
            message.setFlag(Flags.Flag.DELETED, true);
            log.info("Deleted email Subject : " + message.getSubject());
        }

    } catch (MessagingException e) {
        log.error("Error when deleting emails from inbox", e);
        throw new ESBMailTransportIntegrationTestException("Error when deleting emails from inbox ", e);
    } finally {
        if (inbox != null) {
            try {
                inbox.close(true);
            } catch (MessagingException e) {
                log.warn("Error when closing the email folder : ", e);
            }
        }
        if (store != null) {
            try {
                store.close();
            } catch (MessagingException e) {
                log.warn("Error when closing the email store : ", e);
            }
        }
    }
}

From source file:org.xwiki.contrib.mail.internal.DefaultMailReader.java

/**
 * {@inheritDoc}//from  ww  w  . ja  v a2 s .  c  o m
 * 
 * @throws MessagingException
 * @throws
 * @throws Exception
 * @see org.xwiki.contrib.mail.IMailReader#read(java.lang.String, int, java.lang.String, java.lang.String,
 *      java.lang.String, java.lang.String, boolean, int)
 */
@Override
public List<Message> read(final String folder, final boolean onlyUnread, final int max)
        throws MessagingException {
    assert (getMailSource() != null);
    assert (getMailSource().getHostname() != null);

    store = null;
    List<Message> messages = new ArrayList<Message>();
    boolean isGmail = getMailSource().getHostname() != null
            && getMailSource().getHostname().endsWith(".gmail.com");

    logger.info("Trying to retrieve mails from server " + getMailSource().getHostname());

    this.session = createSession(getMailSource().getProtocol(), getMailSource().getAdditionalProperties(),
            isGmail, getMailSource().isAutoTrustSSLCertificates());

    // Get a Store object
    store = session.getStore();

    // Connect to the mail account
    store.connect(getMailSource().getHostname(), getMailSource().getPort(), getMailSource().getUsername(),
            getMailSource().getPassword());
    Folder fldr;
    // Specifically for GMAIL ...
    if (isGmail) {
        fldr = store.getDefaultFolder();
    }
    fldr = store.getFolder(folder);

    fldr.open(Folder.READ_WRITE);

    Message[] msgsArray;
    // Searches for mails not already read
    if (onlyUnread) {
        FlagTerm searchterms = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
        msgsArray = fldr.search(searchterms);
    } else {
        msgsArray = fldr.getMessages();
    }

    if (max > 0 && msgsArray.length > max) {
        msgsArray = (Message[]) ArrayUtils.subarray(msgsArray, 0, max);
    }
    messages = new ArrayList<Message>(Arrays.asList(msgsArray));

    logger.info("Found " + messages.size() + " messages");

    // Note: we leave the Store opened to allow reading returned Messages

    return messages;
}