Example usage for javax.mail Folder close

List of usage examples for javax.mail Folder close

Introduction

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

Prototype

public abstract void close(boolean expunge) throws MessagingException;

Source Link

Document

Close this Folder.

Usage

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

/**
 * Close the given JavaMail Folder and ignore any thrown exception. This is
 * useful for typical <code>finally</code> blocks in manual JavaMail code.
 *
 * @param folder  the JavaMail Folder to close (may be <code>null</code>)
 * @param expunge whether all deleted messages should be expunged from the folder
 *///from  www  . ja  v  a 2 s . com
public static void closeFolder(Folder folder, boolean expunge) {
    if (folder != null && folder.isOpen()) {
        try {
            folder.close(expunge);
        } catch (MessagingException ex) {
            logger.debug("Could not close JavaMail Folder", ex);
        } catch (NullPointerException ex) {
            // JavaMail prior to 1.4.1 may throw this
            logger.debug("Could not close JavaMail Folder", ex);
        }
    }
}

From source file:org.springframework.ws.transport.mail.support.MailTransportUtils.java

/**
 * Close the given JavaMail Folder and ignore any thrown exception. This is useful for typical <code>finally</code>
 * blocks in manual JavaMail code.// w  w w . j a v  a  2 s . c  o m
 *
 * @param folder  the JavaMail Folder to close (may be <code>null</code>)
 * @param expunge whether all deleted messages should be expunged from the folder
 */
public static void closeFolder(Folder folder, boolean expunge) {
    if (folder != null && folder.isOpen()) {
        try {
            folder.close(expunge);
        } catch (MessagingException ex) {
            logger.debug("Could not close JavaMail Folder", ex);
        }
    }
}

From source file:org.wso2.carbon.registry.es.utils.EmailUtil.java

/**
 * This method delete all the sent mails. Can be used after a particular test class
 *
 * @throws  MessagingException//from  w  w  w  .j  ava2s  . c o m
 * @throws  IOException
 */
public static void deleteSentMails() throws MessagingException, IOException {
    Properties props = new Properties();
    props.load(new FileInputStream(new File(TestConfigurationProvider.getResourceLocation("GREG")
            + File.separator + "axis2" + File.separator + "smtp.properties")));
    Session session = Session.getDefaultInstance(props, null);
    Store store = session.getStore("imaps");
    store.connect("smtp.gmail.com", emailAddress, java.nio.CharBuffer.wrap(emailPassword).toString());

    Folder sentMail = store.getFolder("[Gmail]/Sent Mail");
    sentMail.open(Folder.READ_WRITE);
    Message[] messages = sentMail.getMessages();
    Flags deleted = new Flags(Flags.Flag.DELETED);
    sentMail.setFlags(messages, deleted, true);
    sentMail.close(true);
    store.close();
}

From source file:com.naryx.tagfusion.cfm.mail.cfPOP3.java

private static void closeFolder(Folder popFolder) {
    try {/*ww w.ja v  a2 s . c o  m*/
        if (popFolder != null)
            popFolder.close(true);
    } catch (Exception ignoreE) {
    }
}

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

/**
 * Delete all emails in the inbox.// w w  w. j  av  a2s  .  c o m
 *
 * @param protocol protocol used to connect to the server
 * @throws MessagingException if we're unable to connect to the store
 */
public static void deleteAllEmails(String protocol, GreenMailUser user) throws MessagingException {
    Folder inbox = null;
    Store store = getConnection(user, protocol);
    try {
        inbox = store.getFolder(EMAIL_INBOX);
        inbox.open(Folder.READ_WRITE);
        Message[] messages = inbox.getMessages();

        for (Message message : messages) {
            message.setFlag(Flags.Flag.DELETED, true);
            log.info("Deleted email Subject : " + message.getSubject());
        }
    } finally {
        if (inbox != null) {
            inbox.close(true);
        }
        if (store != null) {
            store.close();
        }
    }
}

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

/**
 * Delete all unread emails from inbox/*  ww w  .jav  a  2  s.co  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.apache.hupa.server.handler.DeleteFolderHandler.java

@Override
protected GenericResult executeInternal(DeleteFolder action, ExecutionContext context) throws ActionException {
    User user = getUser();/* w  ww  .  ja v  a  2 s  .co  m*/
    IMAPFolder folder = action.getFolder();
    try {
        IMAPStore store = cache.get(user);

        Folder f = store.getFolder(folder.getFullName());

        // close the folder if its open
        if (f.isOpen()) {
            f.close(false);
        }

        // recursive delete the folder
        if (f.delete(true)) {
            logger.info("Successfully delete folder " + folder + " for user " + user);
            return new GenericResult();
        } else {
            throw new ActionException("Unable to delete folder " + folder + " for user " + user);
        }
    } catch (Exception e) {
        logger.error("Error while deleting folder " + folder + " for user " + user, e);
        throw new ActionException("Error while deleting folder " + folder + " for user " + user, e);
    }
}

From source file:edu.stanford.muse.email.EmailStore.java

/** returns # of messages in the given folder */
public int getNMessages(String fname) throws MessagingException {
    // first check if we've already cached it
    if (this.folderInfos != null)
        for (FolderInfo fi : this.folderInfos)
            if (fi.longName.equals(fname))
                return fi.messageCount;

    Store store = connect();/* w w w  .  j  ava2  s .c om*/
    Pair<Folder, Integer> pair = openFolder(store, fname);
    Folder f = pair.getFirst();
    if (f == null)
        return -1;
    int count = pair.getSecond();
    if (count != -1)
        f.close(false);
    store.close();
    return count;
}

From source file:org.openadaptor.auxil.connector.mail.MailConnection.java

/**
 * Attempts to close the supplied folder. If expunge is true then all messages
 * marked for deletion will be expunged from the folder.
 *
 * @throws MessagingException if there was a problem
 *///from  w w w.j a va 2 s  .  co  m
public void closeFolder(Folder fldr, boolean expunge) throws MessagingException {
    if (fldr == null || !fldr.isOpen())
        return;

    fldr.close(expunge);
    log.debug("Folder [" + fldr.getName() + "] closed. " + "Deleted messages will " + (expunge ? "not " : "")
            + "be expunged");
}

From source file:edu.stanford.muse.email.EmailStore.java

/** returns # of messages in each of the given folders */
public int[] getNMessages(String[] fnames) throws MessagingException {
    Store store = connect();/*  w  ww  .  j a  va2  s.co  m*/
    int x[] = new int[fnames.length];
    for (int i = 0; i < x.length; i++) {
        Pair<Folder, Integer> pair = openFolder(store, fnames[i]);
        Folder f = pair.getFirst();
        if (f == null) {
            x[i] = -1;
            continue;
        }
        int count = pair.getSecond();
        if (count != -1)
            f.close(false);
        x[i] = count;
    }
    try {
        store.close();
    } catch (Exception e) {
        log.warn("Exception in closing folder " + this + ":" + e);
    }
    return x;
}