Example usage for javax.mail Folder addMessageCountListener

List of usage examples for javax.mail Folder addMessageCountListener

Introduction

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

Prototype

public synchronized void addMessageCountListener(MessageCountListener l) 

Source Link

Document

Add a listener for MessageCount events on this Folder.

Usage

From source file:monitor.java

public static void main(String argv[]) {
    if (argv.length != 5) {
        System.out.println("Usage: monitor <host> <user> <password> <mbox> <freq>");
        System.exit(1);/*from  w  w w.  j  a v a2 s  . c  o m*/
    }
    System.out.println("\nTesting monitor\n");

    try {
        Properties props = System.getProperties();

        // Get a Session object
        Session session = Session.getInstance(props, null);
        // session.setDebug(true);

        // Get a Store object
        Store store = session.getStore("imap");

        // Connect
        store.connect(argv[0], argv[1], argv[2]);

        // Open a Folder
        Folder folder = store.getFolder(argv[3]);
        if (folder == null || !folder.exists()) {
            System.out.println("Invalid folder");
            System.exit(1);
        }

        folder.open(Folder.READ_WRITE);

        // Add messageCountListener to listen for new messages
        folder.addMessageCountListener(new MessageCountAdapter() {
            public void messagesAdded(MessageCountEvent ev) {
                Message[] msgs = ev.getMessages();
                System.out.println("Got " + msgs.length + " new messages");

                // Just dump out the new messages
                for (int i = 0; i < msgs.length; i++) {
                    try {
                        System.out.println("-----");
                        System.out.println("Message " + msgs[i].getMessageNumber() + ":");
                        msgs[i].writeTo(System.out);
                    } catch (IOException ioex) {
                        ioex.printStackTrace();
                    } catch (MessagingException mex) {
                        mex.printStackTrace();
                    }
                }
            }
        });

        // Check mail once in "freq" MILLIseconds
        int freq = Integer.parseInt(argv[4]);
        boolean supportsIdle = false;
        try {
            if (folder instanceof IMAPFolder) {
                IMAPFolder f = (IMAPFolder) folder;
                f.idle();
                supportsIdle = true;
            }
        } catch (FolderClosedException fex) {
            throw fex;
        } catch (MessagingException mex) {
            supportsIdle = false;
        }
        for (;;) {
            if (supportsIdle && folder instanceof IMAPFolder) {
                IMAPFolder f = (IMAPFolder) folder;
                f.idle();
                System.out.println("IDLE done");
            } else {
                Thread.sleep(freq); // sleep for freq milliseconds

                // This is to force the IMAP server to send us
                // EXISTS notifications. 
                folder.getMessageCount();
            }
        }

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

From source file:jp.mamesoft.mailsocketchat.Mail.java

public void run() {
    while (true) {
        try {// w ww  .  j  av  a2s  .  c  om
            System.out.println("????");
            Properties props = System.getProperties();

            // Get a Session object
            Session session = Session.getInstance(props, null);
            // session.setDebug(true);

            // Get a Store object
            Store store = session.getStore("imaps");

            // Connect
            store.connect("imap.gmail.com", 993, Mailsocketchat.mail_user, Mailsocketchat.mail_pass);
            System.out.println("??????");

            // Open a Folder
            Folder folder = store.getFolder("INBOX");
            if (folder == null || !folder.exists()) {
                System.out.println("IMAP??????");
                System.exit(1);
            }

            folder.open(Folder.READ_WRITE);

            // Add messageCountListener to listen for new messages
            folder.addMessageCountListener(new MessageCountAdapter() {
                public void messagesAdded(MessageCountEvent ev) {
                    Message[] msgs = ev.getMessages();

                    // Just dump out the new messages
                    for (int i = 0; i < msgs.length; i++) {
                        try {
                            System.out.println("?????");
                            InternetAddress addrfrom = (InternetAddress) msgs[i].getFrom()[0];
                            String subject = msgs[i].getSubject();
                            if (subject == null) {
                                subject = "";
                            }

                            Pattern hashtag_p = Pattern.compile("#(.+)");
                            Matcher hashtag_m = hashtag_p.matcher(subject);

                            if (subject.equals("#")) {
                                hashtag = null;
                            } else if (hashtag_m.find()) {
                                hashtag = hashtag_m.group(1);
                            }

                            String comment = msgs[i].getContent().toString().replaceAll("\r\n", " ");
                            comment = comment.replaceAll("\n", " ");
                            comment = comment.replaceAll("\r", " ");
                            JSONObject data = new JSONObject();
                            data.put("comment", comment);
                            if (hashtag != null) {
                                data.put("channel", hashtag);
                            }
                            if (!comment.equals("") && !comment.equals(" ") && !comment.equals("  ")) {
                                Mailsocketchat.socket.emit("say", data);
                                System.out.println("????");
                            }

                            //
                            if (subject.equals("push") || subject.equals("Push")
                                    || subject.equals("")) {
                                Send(addrfrom.getAddress(), 2);
                                Mailsocketchat.push = true;
                                Mailsocketchat.repeat = false;
                                Mailsocketchat.address = addrfrom.getAddress();
                                repeatthread.cancel();
                                repeatthread = null;
                                System.out.println("?????");
                            } else if (subject.equals("fetch") || subject.equals("Fetch")
                                    || subject.equals("?")) {
                                Send(addrfrom.getAddress(), 3);
                                Mailsocketchat.push = false;
                                Mailsocketchat.repeat = false;
                                repeatthread.cancel();
                                repeatthread = null;
                                System.out.println("??????");
                            } else if (subject.equals("repeat") || subject.equals("Repeat")
                                    || subject.equals("")) {
                                Send(addrfrom.getAddress(), 7);
                                Mailsocketchat.push = false;
                                Mailsocketchat.repeat = true;
                                Mailsocketchat.address = addrfrom.getAddress();
                                if (repeatthread == null) {
                                    repeatthread = new Repeat();
                                    repeat = new Timer();
                                }
                                repeat.schedule(repeatthread, 0, 30 * 1000);
                                System.out.println("?????");
                            } else if (subject.equals("list") || subject.equals("List")
                                    || subject.equals("")) {
                                Send(addrfrom.getAddress(), 4);
                            } else if (subject.equals("help") || subject.equals("Help")
                                    || subject.equals("")) {
                                Send(addrfrom.getAddress(), 5);
                            } else {
                                if (!Mailsocketchat.push && !Mailsocketchat.repeat) {
                                    Send(addrfrom.getAddress(), 0);
                                } else if (comment.equals("") || comment.equals(" ") || comment.equals("  ")) {
                                    Send(addrfrom.getAddress(), 5);
                                }
                            }
                        } catch (IOException ioex) {
                            System.out.println(
                                    "??????????");
                        } catch (MessagingException mex) {
                            System.out.println(
                                    "??????????");
                        }
                    }
                }
            });

            // Check mail once in "freq" MILLIseconds
            int freq = 1000; //??
            boolean supportsIdle = false;
            try {
                if (folder instanceof IMAPFolder) {
                    IMAPFolder f = (IMAPFolder) folder;
                    f.idle();
                    supportsIdle = true;
                }
            } catch (FolderClosedException fex) {
                throw fex;
            } catch (MessagingException mex) {
                supportsIdle = false;
            }
            for (;;) {
                if (supportsIdle && folder instanceof IMAPFolder) {
                    IMAPFolder f = (IMAPFolder) folder;
                    f.idle();
                } else {
                    Thread.sleep(freq); // sleep for freq milliseconds

                    // This is to force the IMAP server to send us
                    // EXISTS notifications. 
                    folder.getMessageCount();
                }
            }

        } catch (Exception ex) {
            System.out.println("??????????");
        }
    }
}

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

public void deleteMessages(long folderId, long[] messageIds) throws PortalException {

    Folder jxFolder = null;

    try {/*  w w  w .  j a v a2s .c o  m*/
        jxFolder = openFolder(folderId);

        jxFolder.addMessageCountListener(new IMAPMessageCountListener(_user, _account, _password));

        List<Message> jxMessages = getMessages(jxFolder, messageIds, true);

        for (Message jxMessage : jxMessages) {
            jxMessage.setFlag(Flags.Flag.DELETED, true);
        }
    } catch (MessagingException me) {
        throw new MailException(me);
    } finally {
        closeFolder(jxFolder, true);
    }
}

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

public void moveMessages(long sourceFolderId, long destinationFolderId, long[] messageIds,
        boolean deleteMissingMessages) throws PortalException {

    Folder sourceJxFolder = null;
    Folder destinationJxFolder = null;// w w  w. ja va2 s.c  o m

    try {
        sourceJxFolder = openFolder(sourceFolderId);

        sourceJxFolder.addMessageCountListener(new IMAPMessageCountListener(_user, _account, _password));

        destinationJxFolder = openFolder(destinationFolderId);

        destinationJxFolder.addMessageCountListener(new IMAPMessageCountListener(_user, _account, _password));

        List<Message> jxMessages = getMessages(sourceJxFolder, messageIds, deleteMissingMessages);

        for (Message jxMessage : jxMessages) {
            destinationJxFolder.appendMessages(new Message[] { jxMessage });

            jxMessage.setFlag(Flags.Flag.DELETED, true);
        }
    } catch (MessagingException me) {
        throw new MailException(me);
    } finally {
        closeFolder(sourceJxFolder, true);
        closeFolder(destinationJxFolder, false);
    }
}

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

public void sendMessage(String personalName, String sender, Address[] to, Address[] cc, Address[] bcc,
        String subject, String body, List<MailFile> mailFiles) throws PortalException {

    Folder jxFolder = null;

    try {/*from w w w.j  a v a2 s .com*/
        jxFolder = openFolder(_account.getSentFolderId());

        Message jxMessage = createMessage(personalName, sender, to, cc, bcc, subject, body, mailFiles);

        Transport transport = _imapConnection.getTransport();

        transport.sendMessage(jxMessage, jxMessage.getAllRecipients());

        transport.close();

        jxFolder.addMessageCountListener(new IMAPMessageCountListener(_user, _account, _password));

        jxFolder.appendMessages(new Message[] { jxMessage });
    } catch (MessagingException me) {
        throw new MailException(me);
    } catch (UnsupportedEncodingException uee) {
        throw new MailException(uee);
    } finally {
        closeFolder(jxFolder, false);
    }
}