Example usage for javax.mail Folder READ_ONLY

List of usage examples for javax.mail Folder READ_ONLY

Introduction

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

Prototype

int READ_ONLY

To view the source code for javax.mail Folder READ_ONLY.

Click Source Link

Document

The Folder is read only.

Usage

From source file:net.fenyo.mail4hotspot.service.MailManager.java

private void connectToServer(final String protocol, final String host, final int port, final String username,
        final String password) throws MessagingException {
    final Properties prop = new Properties();
    prop.setProperty("mail.mime.address.strict", "false");

    //      prop.setProperty(" mail.debug", "true");
    session = javax.mail.Session.getInstance(prop, null);

    //session.setDebugOut(System.out);
    //session.setDebug(false);
    //      session.setDebug(true);

    store = session.getStore(protocol);//w  w w.j a v  a2  s  .  c  o m
    store.connect(host, port, username, password);
    inbox = store.getDefaultFolder().getFolder("INBOX");
    inbox.open(Folder.READ_ONLY);
    messages = inbox.getMessages();
}

From source file:org.webguitoolkit.messagebox.mail.MailChannel.java

/**
 * /*ww  w.  j  a  v  a2s . c  o  m*/
 */
@Override
public List<IMessage> receive(boolean clear) {
    List<IMessage> result = new ArrayList<IMessage>();

    try {
        String user = properties.getProperty("pop3.login.user");
        String password = properties.getProperty("pop3.login.password");

        String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

        Properties pop3Props = new Properties();

        pop3Props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
        pop3Props.setProperty("mail.pop3.socketFactory.fallback", "false");
        pop3Props.setProperty("mail.pop3.port", properties.getProperty("pop3.port"));
        pop3Props.setProperty("mail.pop3.socketFactory.port", properties.getProperty("pop3.port"));

        URLName url = new URLName("pop3", properties.getProperty("pop3.host"),
                Integer.valueOf(properties.getProperty("pop3.port")), "", user, password);

        Session session = Session.getInstance(pop3Props, null);
        Store store = new POP3SSLStore(session, url);
        store.connect();

        // Open the Folder
        Folder folder = store.getDefaultFolder();
        folder = folder.getFolder("INBOX");

        if (folder == null) {
            throw new RuntimeException("Invalid folder INBOX");
        }

        // try to open read/write and if that fails try read-only
        try {
            folder.open(Folder.READ_WRITE);
        } catch (MessagingException ex) {
            folder.open(Folder.READ_ONLY);
        }

        int count = folder.getMessageCount();
        // Message numbers start at 1
        for (int i = 1; i <= count; i++) {
            // Get a message by its sequence number
            Message m = folder.getMessage(i);
            Address[] from = m.getFrom();
            String type = from[0].getType();

            IMessage message = new MailMessage(from[0].toString(), this, m.getSubject(), getContent(m));

            result.add(message);

            // delete message ?
            if (clear)
                m.setFlag(Flags.Flag.DELETED, true);
        }

        // "true" actually deletes flagged messages from folder
        folder.close(clear);
        store.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return result;
}

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

/** returns # of messages in folder. -1 if folder cannot be opened.
 * connect should already have been called */
public Folder openFolderWithoutCount(Store store, String fname) throws MessagingException {
    Folder folder = null;/*from   w  w  w .ja v a2s  .  c  o m*/

    if (fname == null)
        fname = "INBOX";

    folder = store.getDefaultFolder();
    folder = folder.getFolder(fname);

    if (folder == null)
        throw new RuntimeException("Invalid folder: " + fname);

    log.info("Opening folder " + Util.blurKeepingExtension(fname) + " in r/o mode...");
    try {
        folder.open(Folder.READ_ONLY);
    } catch (MessagingException me) {
        return null;
    }
    return folder;
}

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

protected void checkFolder(Folder folder, PrintWriter pw)
        throws MessagingException, IOException, SearchLibException {
    if (folder == null)
        return;//from   w ww  . j a  va2  s .  c om
    if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
        folder.open(Folder.READ_ONLY);
        try {
            pw.print("Folder ");
            pw.print(folder.getName());
            pw.print(": ");
            pw.print(folder.getMessageCount());
            pw.println(" msgs(s).");
        } finally {
            folder.close(false);
        }
    }
    if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) {
        Folder[] folders = folder.list();
        if (folders != null)
            for (Folder f : folders)
                checkFolder(f, pw);
    }
}

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

public void computeFoldersAndCounts(String cacheDir /*unused */) throws MessagingException {
    if (store == null)
        connect();//from   w w w. j a va  2  s . c om
    if (!store.isConnected())
        connect();

    doneReadingFolderCounts = false;
    this.folderInfos = new ArrayList<FolderInfo>();
    if ("pop3".equals(connectOptions.protocol) || "pop3s".equals(connectOptions.protocol)) {
        Folder f = store.getDefaultFolder();
        f = f.getFolder("INBOX");
        f.open(Folder.READ_ONLY);
        int count = f.getMessageCount();
        f.close(false);
        this.folderInfos.add(new FolderInfo(getAccountID(), "INBOX", "INBOX", count));
    } else
        collect_folder_names(store, this.folderInfos, store.getDefaultFolder());

    folderBeingScanned = "";

    if (connectOptions.server.endsWith(".pobox.stanford.edu"))
        this.folderInfos.add(new FolderInfo(getAccountID(), "INBOX", "INBOX", 0)); // hack for stanford imap, it lists INBOX as a dir folder! TOFIX

    doneReadingFolderCounts = true;
}

From source file:org.springframework.ws.transport.mail.MailSenderConnection.java

private void openFolder() throws MessagingException {
    store = session.getStore(storeUri);//from w  w w  .  j a v  a 2s.c  o  m
    store.connect();
    folder = store.getFolder(storeUri);
    if (folder == null || !folder.exists()) {
        throw new IllegalStateException("No default folder to receive from");
    }
    if (deleteAfterReceive) {
        folder.open(Folder.READ_WRITE);
    } else {
        folder.open(Folder.READ_ONLY);
    }
}

From source file:de.kp.ames.web.function.access.imap.ImapConsumer.java

/**
 * Retrieve attachment from message id/*ww  w  .j  av a 2s.  co  m*/
 * 
 * @param mid
 * @return
 */
public FileUtil getAttachment(int mid) {

    FileUtil file = null;
    if (store == null)
        return file;

    try {
        /*
         * Connect to IMAP store
         */
        store.connect();

        /*
         * Retrieve & open INBOX folder
         */
        Folder folder = store.getFolder(ImapConstants.INBOX);
        folder.open(Folder.READ_ONLY);

        Message[] messages = folder.getMessages();
        Message m = null;

        /* 
         * Message message = folder.getMessage(mid); 
         * 
         * the method 'getMessage' always returns an
         * index out of bound error, so we have to
         * introduce the hack below
         */

        for (int i = 0; i < messages.length; i++) {
            if (i == mid)
                m = messages[i];
        }

        if (m == null)
            return file;

        Object content = m.getContent();
        if (content instanceof Multipart) {
            file = handleMultipart((Multipart) content, true);

        } else {
            file = handlePart(m, true);

        }

        folder.close(false);
        store.close();

    } catch (Exception e) {
        e.printStackTrace();

    }

    return file;

}

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

public Folder openFolderWithoutCount(Store s, String fname) throws MessagingException {
    if (fname == null)
        fname = "INBOX";

    // ignore the store coming in, we need a new session and store
    // for each folder

    Session session = Session.getInstance(mstorProps, null);
    session.setDebug(DEBUG);/*from  ww  w. java  2  s.co  m*/

    // Get a Store object
    Store store = session.getStore(new URLName("mstor:" + fname));
    store.connect();

    //This is the root folder in the namespace provided
    //see http://docs.oracle.com/javaee/5/api/javax/mail/Store.html#getDefaultFolder%28%29
    Folder folder = store.getDefaultFolder();
    if (folder == null)
        throw new RuntimeException("Invalid folder: " + fname);

    log.info("Opening folder " + Util.blurPath(fname) + " in r/o mode...");
    try {
        folder.open(Folder.READ_ONLY);
    } catch (MessagingException me) {
        folder = null;
    }

    return folder;
}

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   w  ww.ja  va2 s.c  om*/
 * @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:com.seleniumtests.connectors.mails.ImapClient.java

/**
 * Returns the number of messages on the server
 * // www.  ja  va 2 s  .  c  o m
 * @param folderName
 * @return 
 * @throws MessagingException
 */
private Integer getMessageNumber(String folderName) throws MessagingException {
    // Get folder
    Folder folder = store.getFolder(folderName);
    folder.open(Folder.READ_ONLY);

    Integer messageCount = folder.getMessageCount();

    folder.close(false);

    return messageCount;
}