Example usage for javax.mail Folder search

List of usage examples for javax.mail Folder search

Introduction

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

Prototype

public Message[] search(SearchTerm term) throws MessagingException 

Source Link

Document

Search this Folder for messages matching the specified search criterion.

Usage

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

/**
 * Delete all unread emails from inbox/*  w  w  w.  j a v  a 2 s.c om*/
 *
 * @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.wso2.esb.integration.common.utils.servers.GreenMailServer.java

/**
 * Check mail folder for an email using subject.
 *
 * @param emailSubject Email subject/*from  w  w  w  .j  ava2  s . com*/
 * @param folder       mail folder to check for an email
 * @param protocol     protocol used to connect to the server
 * @return whether mail received or not
 * @throws MessagingException if we're unable to connect to the store
 */
private static boolean isMailReceivedBySubject(String emailSubject, String folder, String protocol,
        GreenMailUser user) throws MessagingException {
    boolean emailReceived = false;
    Folder mailFolder;
    Store store = getConnection(user, protocol);
    try {
        mailFolder = store.getFolder(folder);
        mailFolder.open(Folder.READ_WRITE);
        SearchTerm searchTerm = new AndTerm(new SubjectTerm(emailSubject), new BodyTerm(emailSubject));
        Message[] messages = mailFolder.search(searchTerm);
        for (Message message : messages) {
            if (message.getSubject().contains(emailSubject)) {
                log.info("Found the Email with Subject : " + emailSubject);
                emailReceived = true;
                break;
            }
        }
    } finally {
        if (store != null) {
            store.close();
        }
    }
    return emailReceived;
}

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

/**
 * {@inheritDoc}//  w ww  . ja v  a2  s. c  o  m
 * 
 * @throws MessagingException
 * @see org.xwiki.contrib.mail.IMailReader#readFromStore(java.lang.String)
 */
public Message read(String folder, String messageid) throws MessagingException {
    Store store = getJavamailStore();
    store.connect();

    Folder mailFolder = store.getDefaultFolder().getFolder(folder);
    mailFolder.open(Folder.READ_ONLY);
    Message[] messages = mailFolder.search(new MessageIDTerm(messageid));
    mailFolder.close(false);
    store.close();
    if (messages.length > 0) {
        return messages[0];
    }
    return null;
}

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

/**
 * {@inheritDoc}/*  w  ww. ja  v a  2s.  c  om*/
 * 
 * @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;
}

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

/**
 * {@inheritDoc}// w w  w  .  j a va2s . c  om
 * 
 * @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 Message read(final String folder, final String messageid) throws MessagingException {
    assert (getMailSource() != null);
    assert (getMailSource().getHostname() != null);

    Message message = null;
    store = null;
    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);

    // Search with message id
    Message[] messages = fldr.search(new MessageIDTerm(messageid));

    if (messages.length > 0) {
        message = messages[0];
    }

    logger.info("Found message " + message);

    return message;
}