Example usage for javax.mail Folder getMessages

List of usage examples for javax.mail Folder getMessages

Introduction

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

Prototype

public synchronized Message[] getMessages() throws MessagingException 

Source Link

Document

Get all Message objects from this Folder.

Usage

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;// w w  w. ja v a 2  s  .  c o  m

    try {
        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:org.apache.axis2.transport.mail.MailClient.java

public int checkInbox(int mode) throws MessagingException, IOException {
    int numMessages = 0;

    if (mode == 0) {
        return 0;
    }/*from ww  w.  j  a v  a 2s  .c  o m*/

    boolean show = (mode & SHOW_MESSAGES) > 0;
    boolean clear = (mode & CLEAR_MESSAGES) > 0;
    String action = (show ? "Show" : "") + ((show && clear) ? " and " : "") + (clear ? "Clear" : "");

    log.info(action + " INBOX for " + from);

    Store store = session.getStore();

    store.connect();

    Folder root = store.getDefaultFolder();
    Folder inbox = root.getFolder("inbox");

    inbox.open(Folder.READ_WRITE);

    Message[] msgs = inbox.getMessages();

    numMessages = msgs.length;

    if ((msgs.length == 0) && show) {
        log.info("No messages in inbox");
    }

    for (int i = 0; i < msgs.length; i++) {
        MimeMessage msg = (MimeMessage) msgs[i];

        if (show) {
            log.info("    From: " + msg.getFrom()[0]);
            log.info(" Subject: " + msg.getSubject());
            log.info(" Content: " + msg.getContent());
        }

        if (clear) {
            msg.setFlag(Flags.Flag.DELETED, true);
        }
    }

    inbox.close(true);
    store.close();

    return numMessages;
}

From source file:org.apache.axis2.transport.mail.MailRequestResponseClient.java

private Message getMessage(String requestMsgId) throws Exception {
    MimeMessage response = null;//from w w  w .  j a  v a  2s .  com
    Folder folder = store.getFolder(MailConstants.DEFAULT_FOLDER);
    folder.open(Folder.READ_WRITE);
    Message[] msgs = folder.getMessages();
    log.debug(msgs.length + " messages in mailbox");
    loop: for (Message m : msgs) {
        MimeMessage mimeMessage = (MimeMessage) m;
        String[] inReplyTo = mimeMessage.getHeader(MailConstants.MAIL_HEADER_IN_REPLY_TO);
        log.debug("Found message " + mimeMessage.getMessageID() + " in reply to " + Arrays.toString(inReplyTo));
        if (inReplyTo != null && inReplyTo.length > 0) {
            for (int j = 0; j < inReplyTo.length; j++) {
                if (requestMsgId.equals(inReplyTo[j])) {
                    log.debug("Identified message " + mimeMessage.getMessageID() + " as the response to "
                            + requestMsgId + "; retrieving it from the store");
                    // We need to create a copy so that we can delete the original and close the folder
                    response = new MimeMessage(mimeMessage);
                    log.debug("Flagging message " + mimeMessage.getMessageID() + " for deletion");
                    mimeMessage.setFlag(Flags.Flag.DELETED, true);
                    break loop;
                }
            }
        }
        log.warn("Don't know what to do with message " + mimeMessage.getMessageID() + "; skipping");
    }
    folder.close(true);
    return response;
}

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 a va2s  .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);
    }
}

From source file:com.treesbearfruit.icloudnotes.NotesSaver.java

private void save(Store store, String wheretobackup, String f) throws MessagingException, IOException {

    System.out.println("opening folder " + f);
    Folder folder = store.getFolder(f);
    folder.open(Folder.READ_ONLY);//from ww  w. ja v  a 2s .c  o  m

    FileUtils.forceMkdir(new File(wheretobackup));

    // Get directory
    Message message[] = folder.getMessages();
    for (int i = 0, n = message.length; i < n; i++) {
        // String from = (message[i].getFrom()[0]).toString();
        String subj = (message[i].getSubject()).toString();
        String nota = (message[i].getContent()).toString();

        // System.out.println("from: " + from);
        System.out.println("saving: " + subj);

        // BACKUP NOTE
        generals.writeFile(wheretobackup + "/" + generals.makeFilename(subj).trim() + ".html", nota,
                message[i].getSentDate());

    }
    folder.close(false);
}

From source file:com.trivago.mail.pigeon.mail.MailFacade.java

public void readBounceAccount() throws MessagingException {
    Session session = Session.getDefaultInstance(Settings.create().getConfiguration().getProperties("mail.*"));
    Store store = session.getStore("pop3");

    store.connect();/*  w  w  w.  ja v  a 2 s  .  c o  m*/

    // Get folder
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_ONLY);

    if (folder.hasNewMessages()) {
        // Get directory
        Message message[] = folder.getMessages();
        BounceFacade bounceFacade = new BounceFacade();

        for (Message msg : message) {
            boolean isBounce = bounceFacade.processBounce(msg);
            if (isBounce) {
                msg.setFlag(Flags.Flag.SEEN, true);
                msg.saveChanges();
            }
        }
    }

}

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

@Override
public void processMessages() {
    try {//from   ww  w .  j  a v a  2  s  .  c  om
        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);
    }
}

From source file:FolderModel.java

public void setFolder(Folder what) throws MessagingException {
    if (what != null) {

        // opened if needed
        if (!what.isOpen()) {
            what.open(Folder.READ_WRITE);
        }/*from w  ww .ja  va 2 s  .c  o  m*/

        // get the messages
        messages = what.getMessages();
        cached = new String[messages.length][];
    } else {
        messages = null;
        cached = null;
    }
    // close previous folder and switch to new folder
    if (folder != null)
        folder.close(true);
    folder = what;
    fireTableDataChanged();
}

From source file:org.usergrid.rest.management.RegistrationIT.java

private Message[] getMessages(String host, String user, String password)
        throws MessagingException, IOException {

    Session session = Session.getDefaultInstance(new Properties());
    Store store = session.getStore("imap");
    store.connect(host, user, password);

    Folder folder = store.getFolder("inbox");
    folder.open(Folder.READ_ONLY);// w  w  w.  ja v  a  2  s  .com
    Message[] msgs = folder.getMessages();

    for (Message m : msgs) {
        logger.info("Subject: " + m.getSubject());
        logger.info("Body content 0 " + (String) ((MimeMultipart) m.getContent()).getBodyPart(0).getContent());
        logger.info("Body content 1 " + (String) ((MimeMultipart) m.getContent()).getBodyPart(1).getContent());
    }
    return msgs;
}

From source file:org.apache.usergrid.rest.management.RegistrationIT.java

private Message[] getMessages(String host, String user, String password)
        throws MessagingException, IOException {

    Session session = Session.getDefaultInstance(new Properties());
    Store store = session.getStore("imap");
    store.connect(host, user, password);

    Folder folder = store.getFolder("inbox");
    folder.open(Folder.READ_ONLY);//w ww  .  j ava2  s  . c om
    Message[] msgs = folder.getMessages();

    for (Message m : msgs) {
        logger.info("Subject: " + m.getSubject());
        logger.info("Body content 0 " + ((MimeMultipart) m.getContent()).getBodyPart(0).getContent());
        logger.info("Body content 1 " + ((MimeMultipart) m.getContent()).getBodyPart(1).getContent());
    }
    return msgs;
}