新电子邮件服务器推送通知

时间:2016-03-18 08:09:40

标签: java gmail javamail

我正在使用javax.mail API连接到gmail收件箱。我正在寻找的基本上是创建一个程序来监听java中的新电子邮件通知。我找到了一个在消息计数中添加监听器的代码。不幸的是,如果新的电子邮件到达我的Gmail帐户,它就无法正常运行并且没有任何更新。这是一个代码:

public class CheckingMails {

public static void check(String host, String storeType, String user,
                         String password) {
    try {

        //create properties field
        Properties properties = new Properties();

        properties.put("mail.pop3.host", host);
        properties.put("mail.pop3.port", "995");
        properties.put("mail.pop3.starttls.enable", "true");
        Session emailSession = Session.getDefaultInstance(properties);


        IMAPStore imapStore = (IMAPStore) emailSession.getStore("imaps");
       // imapStore.connect();

        imapStore.connect(host, user, password);

        final IMAPFolder folder = (IMAPFolder) imapStore.getFolder("Inbox");
        folder.open(IMAPFolder.READ_WRITE);

        folder.addMessageCountListener(new MessageCountListener() {

            public void messagesAdded(MessageCountEvent e) {
                System.out.println("Message Count Event Fired");
            }

            public void messagesRemoved(MessageCountEvent e) {
                System.out.println("Message Removed Event fired");
            }
        });

        folder.addMessageChangedListener(new MessageChangedListener() {

            public void messageChanged(MessageChangedEvent e) {
                System.out.println("Message Changed Event fired");
            }
        });


        // Check mail once in "freq" MILLIseconds
        int freq = 2000;
        boolean supportsIdle = false;
        try {
            if (emailFolder instanceof IMAPFolder) {
                IMAPFolder f = (IMAPFolder) emailFolder;
                f.idle();
                supportsIdle = true;
            }
        } catch (FolderClosedException fex) {
            throw fex;
        } catch (MessagingException mex) {
            supportsIdle = false;
        }
        for (; ; ) {
            if (supportsIdle && emailFolder instanceof IMAPFolder) {
                IMAPFolder f = (IMAPFolder) emailFolder;
                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.
                emailFolder.getMessageCount();
            }
        }



        /*
        // retrieve the messages from the folder in an array and print it
        Message[] messages = emailFolder.getMessages();
        System.out.println("messages.length---" + messages.length);

        for (int i = 0, n = messages.length; i < n; i++) {
            Message message = messages[i];
            System.out.println("---------------------------------");
            System.out.println("Email Number " + (i + 1));
            System.out.println("Subject: " + message.getSubject());
            System.out.println("From: " + message.getFrom()[0]);
            System.out.println("Text: " + message.getContent().toString());

        }

        */

        //close the store and folder objects
     //   emailFolder.close(false);
     //   store.close();

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

public static void main(String [] args){

    String host = "pop.gmail.com";
    String mailStoreType = "pop3";
    String username = "EmailAddress@gmail.com";
    String password = "****";

    check(host, mailStoreType, username, password);

}

}

我还添加了一个注释代码来读取文件夹中的所有电子邮件,它正在运行,但我正在寻找某种来自服务器的推送通知。 有人可以告诉我如何实现这一点。

0 个答案:

没有答案