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: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();
    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();//  www  .  ja  v a 2s  .c o  m
        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;
}

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

public String check() throws MessagingException, IOException, SearchLibException {
    Store store = null;
    StringWriter sw = null;//from ww  w. java  2s  .  c o  m
    PrintWriter pw = null;
    try {
        sw = new StringWriter();
        pw = new PrintWriter(sw);
        pw.println();
        store = getStore();
        connect(store);
        checkFolder(store.getDefaultFolder(), pw);
        pw.println("OK");
        return sw.toString();
    } finally {
        if (store != null)
            store.close();
        IOUtils.close(pw, sw);
    }
}

From source file:de.saly.elasticsearch.imap.AbstractIMAPRiverUnitTest.java

protected void renameMailbox(final Properties props, final String folderName, final String user,
        final String password) throws MessagingException {
    final Store store = Session.getInstance(props).getStore();

    store.connect(user, password);//  w  ww. ja v  a 2s.c o  m
    checkStoreForTestConnection(store);
    final Folder f = store.getFolder(folderName);

    f.renameTo(store.getFolder("renamed_from_" + folderName));

    logger.info("Renamed " + f.getFullName());
    store.close();

}

From source file:com.yfiton.notifiers.email.EmailNotifierTest.java

private boolean checkEmailReception(String subject, String host, String username, String password)
        throws MessagingException {
    Properties properties = new Properties();
    properties.put("mail.store.protocol", "imaps");
    Session session = Session.getInstance(properties);

    Store store = null;

    try {/*from   w  w  w . j  a  v  a 2 s .  co m*/
        store = session.getStore("imaps");
        store.connect(host, username, password);

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

        Message[] messages = inbox.getMessages();

        for (Message message : messages) {
            if (message.getSubject().equals(subject)) {
                message.setFlag(Flags.Flag.DELETED, true);
                return true;
            }
        }

        inbox.close(true);
    } finally {
        try {
            if (store != null) {
                store.close();
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    return false;
}

From source file:com.alvexcore.repo.emails.impl.ExtendedEmailMessage.java

@Override
public void fetch() {
    try {/*ww  w  .j a  v a 2 s  . co m*/
        Store store = connectToStore();
        fetchFolder((IMAPFolder) store.getDefaultFolder(),
                new EmailFolderImpl(getEmailConfigNode(), nodeService, contentService), true);
        store.close();
    } catch (MessagingException e) {
        throw new AlfrescoRuntimeException("Failed to fetch emails", e);
    }
}

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

/**
 * This method read e-mails from Gmail inbox and find whether the notification of particular type is found.
 *
 * @param   notificationType    Notification types supported by publisher and store.
 * @return  whether email is found for particular type.
 * @throws  Exception/* w  ww . j  ava 2s.  c o  m*/
 */
public static boolean readGmailInboxForNotification(String notificationType) throws Exception {
    boolean isNotificationMailAvailable = false;
    long waitTime = 10000;
    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 inbox = store.getFolder("inbox");
    inbox.open(Folder.READ_WRITE);
    Thread.sleep(waitTime);

    long startTime = System.currentTimeMillis();
    long endTime = 0;
    int count = 1;
    while (endTime - startTime < 180000 && !isNotificationMailAvailable) {
        Message[] messages = inbox.getMessages();

        for (Message message : messages) {
            if (!message.isExpunged()) {
                try {
                    log.info("Mail Subject:- " + message.getSubject());

                    if (message.getSubject().contains(notificationType)) {
                        isNotificationMailAvailable = true;

                    }
                    // Optional : deleting the  mail
                    message.setFlag(Flags.Flag.DELETED, true);
                } catch (MessageRemovedException e) {
                    log.error("Could not read the message subject. Message is removed from inbox");
                }
            }

        }
        endTime = System.currentTimeMillis();
        Thread.sleep(waitTime);
        endTime += count * waitTime;
        count++;
    }
    inbox.close(true);
    store.close();
    return isNotificationMailAvailable;
}

From source file:org.wso2.es.ui.integration.util.ESUtil.java

/**
 * To check if a e-mail exists with a given subject
 *
 * @param smtpPropertyFile smtp property file path
 * @param password         password/*from  ww w  .ja  v  a 2  s  .com*/
 * @param email            email address
 * @param subject          email subject
 * @return if the mail exist
 * @throws java.io.IOException
 * @throws MessagingException
 * @throws InterruptedException
 */
public static String readEmail(String smtpPropertyFile, String password, String email, String subject)
        throws MessagingException, InterruptedException, IOException {
    Properties props = new Properties();
    String message = null;
    Folder inbox = null;
    Store store = null;
    FileInputStream inputStream = null;

    try {
        inputStream = new FileInputStream(new File(smtpPropertyFile));
        props.load(inputStream);
        Session session = Session.getDefaultInstance(props, null);
        store = session.getStore(IMAPS);
        store.connect(SMTP_GMAIL_COM, email, password);
        inbox = store.getFolder(INBOX);
        inbox.open(Folder.READ_ONLY);
        message = getMailWithSubject(inbox, subject);
    } catch (MessagingException e) {
        LOG.error(getErrorMessage(email), e);
        throw e;
    } catch (InterruptedException e) {
        LOG.error(getErrorMessage(email), e);
        throw e;
    } catch (FileNotFoundException e) {
        LOG.error(getErrorMessage(email), e);
        throw e;
    } catch (IOException e) {
        LOG.error(getErrorMessage(email), e);
        throw e;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                LOG.error("Input stream closing failed");
            }
        }
        if (inbox != null) {
            try {
                inbox.close(true);
            } catch (MessagingException e) {
                LOG.error("Inbox closing failed");
            }
        }
        if (store != null) {
            try {
                store.close();
            } catch (MessagingException e) {
                LOG.error("Message store closing failed");
            }
        }
    }
    return message;
}

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

/**
 * {@inheritDoc}// w ww .  ja v  a  2 s  . c  om
 * 
 * @see org.xwiki.contrib.mail.IMailReader#readFromStore(java.lang.String)
 */
@Override
public List<Message> read(String folder, boolean onlyUnred, int max) throws MessagingException {
    Store store = getJavamailStore();
    store.connect();
    Folder mailFolder = store.getDefaultFolder();
    if (StringUtils.isNotEmpty(folder)) {
        mailFolder = mailFolder.getFolder(folder);
    }
    mailFolder.open(Folder.READ_WRITE);
    Message[] msgsArray = mailFolder.getMessages();
    if (max > 0 && msgsArray.length > max) {
        msgsArray = (Message[]) ArrayUtils.subarray(msgsArray, 0, max);
    }
    List<Message> messages = new ArrayList<Message>(Arrays.asList(msgsArray));
    mailFolder.close(false);
    store.close();
    return messages;
}

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

/**
 * This method read verification e-mail from Gmail inbox and returns the verification URL.
 *
 * @return  verification redirection URL.
 * @throws  Exception//  w w  w . j ava2  s .c o m
 */
public static String readGmailInboxForVerification() throws Exception {
    boolean isEmailVerified = false;
    long waitTime = 10000;
    String pointBrowserURL = "";
    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 inbox = store.getFolder("inbox");
    inbox.open(Folder.READ_WRITE);
    Thread.sleep(waitTime);
    long startTime = System.currentTimeMillis();
    long endTime = 0;
    int count = 1;
    while (endTime - startTime < 180000 && !isEmailVerified) {
        Message[] messages = inbox.getMessages();

        for (Message message : messages) {
            if (!message.isExpunged()) {
                try {
                    log.info("Mail Subject:- " + message.getSubject());
                    if (message.getSubject().contains("EmailVerification")) {
                        pointBrowserURL = getBodyFromMessage(message);
                        isEmailVerified = true;
                    }

                    // Optional : deleting the mail
                    message.setFlag(Flags.Flag.DELETED, true);
                } catch (MessageRemovedException e) {
                    log.error("Could not read the message subject. Message is removed from inbox");
                }
            }
        }
        endTime = System.currentTimeMillis();
        Thread.sleep(waitTime);
        endTime += count * waitTime;
        count++;
    }
    inbox.close(true);
    store.close();
    return pointBrowserURL;
}

From source file:nl.ordina.bag.etl.mail.loader.POP3MutatiesFileLoader.java

@Override
public void processMessages() {
    try {//from  www. ja v a 2  s  .c  o m
        Session session = Session.getDefaultInstance(new Properties(), null);
        Store store = session.getStore(protocol);
        if (port == 0)
            store.connect(host, username, password);
        else
            store.connect(host, port, username, password);

        Folder folder = store.getFolder(folderName);
        if (folder == null)
            throw new RuntimeException("Folder " + folderName + " not found!");
        folder.open(Folder.READ_WRITE);

        try {
            Message[] messages = folder.getMessages();
            for (Message message : messages) {
                backup(message);
                messageHandler.handle(message);
                message.setFlags(new Flags(Flags.Flag.DELETED), true);
            }
        } finally {
            folder.close(true);
            store.close();
        }
    } catch (MessagingException | IOException | JAXBException e) {
        throw new ProcessingException(e);
    }
}