Example usage for javax.mail Store close

List of usage examples for javax.mail Store close

Introduction

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

Prototype

public synchronized void close() throws MessagingException 

Source Link

Document

Close this service and terminate its connection.

Usage

From source file:com.liferay.mail.imap.IMAPConnection.java

protected void testIncomingConnection() throws MailException {
    StopWatch stopWatch = new StopWatch();

    stopWatch.start();//from  w  ww  .  jav  a  2  s.c om

    try {
        Store store = getStore(false);

        store.close();
    } catch (Exception e) {
        throw new MailException(MailException.ACCOUNT_INCOMING_CONNECTION_FAILED, e);
    } finally {
        if (_log.isDebugEnabled()) {
            stopWatch.stop();

            _log.debug("Testing incoming connection completed in " + stopWatch.getTime() + " ms");
        }
    }
}

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

/**
 * Check a particular email has received to a given email folder by email subject.
 *
 * @param emailSubject - Email emailSubject to find email is in inbox or not
 * @return - found the email or not/*from  w w w . j a  va2s. co  m*/
 * @throws ESBMailTransportIntegrationTestException - Is thrown if an error occurred while reading the emails
 */
public static boolean isMailReceivedBySubject(String emailSubject, String folder)
        throws ESBMailTransportIntegrationTestException {
    boolean emailReceived = false;
    Folder mailFolder;
    Store store = getConnection();
    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 emailSubject : " + emailSubject);
                emailReceived = true;
                break;
            }
        }
        return emailReceived;
    } catch (MessagingException ex) {
        log.error("Error when getting mail count ", ex);
        throw new ESBMailTransportIntegrationTestException("Error when getting mail count ", ex);
    } finally {
        if (store != null) {
            try {
                store.close();
            } catch (MessagingException e) {
                log.warn("Error when closing the store ", e);
            }
        }
    }
}

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

/**
 * Delete all unread emails from inbox/*ww w.  java2 s  . 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:com.agiletec.plugins.jpwebmail.aps.system.services.webmail.WebMailManager.java

@Override
public void closeConnection(Store store) {
    try {//  w w  w. j a v a2  s  .c  o m
        if (null != store)
            store.close();
    } catch (Throwable t) {
        _logger.error("Error closing connection", t);
        ApsSystemUtils.logThrowable(t, this, "closeConnection", "Error closing connection");
    }
}

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();
    Pair<Folder, Integer> pair = openFolder(store, fname);
    Folder f = pair.getFirst();//w  ww. ja  va  2s .co m
    if (f == null)
        return -1;
    int count = pair.getSecond();
    if (count != -1)
        f.close(false);
    store.close();
    return count;
}

From source file:com.jaeksoft.searchlib.crawler.mailbox.crawler.MailboxAbstractCrawler.java

public void read() throws MessagingException, IOException, SearchLibException {
    Store store = null;
    try {//from  w  w  w .j  a  v  a2 s  .com
        store = getStore();
        connect(store);
        readFolder(store.getDefaultFolder());
    } finally {
        if (store != null)
            store.close();
    }
}

From source file:com.hs.mail.web.controller.FetchAccountFormController.java

@Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command,
        BindException errors) throws Exception {
    FetchAccount fetch = (FetchAccount) command;
    String userName = RequestUtils.getParameter(request, "destUserName");
    String password = RequestUtils.getParameter(request, "destPassword");
    Store store = null;
    try {/*from   w w w. j ava  2  s.  c om*/
        store = connect(userName, password);
        FetchMailer mailer = new FetchMailer(fetch, store.getFolder("INBOX"));
        mailer.fetch();
        return new ModelAndView(getSuccessView(), errors.getModel());
    } catch (Exception e) {
        return showForm(request, response, errors);
    } finally {
        if (store != null) {
            store.close();
        }
    }
}

From source file:com.liferay.mail.imap.IMAPConnection.java

public Store getStore(boolean useOldStores) throws MailException {
    Store store = null;

    try {/*from   w  w w.  j  av  a2 s . c  o  m*/
        String storeKey = _incomingHostName.concat(_outgoingHostName).concat(_login);

        if (useOldStores) {
            store = _allStores.get(storeKey);

            if ((store != null) && !store.isConnected()) {
                store.close();

                store = null;
            }
        }

        if (store == null) {
            Session session = getSession();

            if (_incomingSecure) {
                store = session.getStore("imaps");
            } else {
                store = session.getStore("imap");
            }

            store.connect(_incomingHostName, _incomingPort, _login, _password);

            if (useOldStores) {
                _allStores.put(storeKey, store);
            }
        }

        return store;
    } catch (MessagingException me) {
        throw new MailException(MailException.ACCOUNT_INCOMING_CONNECTION_FAILED, me);
    }
}

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

/**
 * {@inheritDoc}//from  www .  j  av  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:com.google.code.rptm.mailarchive.DefaultMailingListArchive.java

public void retrieveMessages(String mailingList, YearMonth month, MimeMessageProcessor processor,
        MailingListArchiveEventListener eventListener) throws MailingListArchiveException {
    Session session = Session.getDefaultInstance(new Properties());
    try {/*  w  w  w.  j ava 2 s.  c om*/
        Store store = session.getStore(new URLName("mstor:" + getMboxFile(mailingList, month, eventListener)));
        store.connect();
        try {
            Folder folder = store.getDefaultFolder();
            folder.open(Folder.READ_ONLY);
            for (Message msg : folder.getMessages()) {
                if (!processor.processMessage((MimeMessage) msg)) {
                    break;
                }
            }
        } finally {
            store.close();
        }
    } catch (MessagingException ex) {
        throw new MailingListArchiveException("JavaMail exception: " + ex.getMessage(), ex);
    } catch (IOException ex) {
        throw new MailingListArchiveException("I/O exception: " + ex.getMessage(), ex);
    }
}