jvamail中MessageCountListener()的getSubject()调用不了

fevone 发布于 2015/05/15 10:09
阅读 318
收藏 0

代码如下:我是希望能从messageAdded事件中获取新收到的邮件进行处理的。但是message能获取到,也能读到messagenumber,就是其他信息就拿不到了,subject, from都根本不执行。求教是什么问题?

public static void update() throws MessagingException {
        // 准备链接服务器的会话信息
        Properties props = new Properties();
        props.setProperty("mail.store.protocol", "imap");
        props.setProperty("mail.imap.host", "imap.sina.com");
        props.setProperty("mail.imap.port", "143");

        // 创建session实例对象
        Session session = Session.getInstance(props);
        session.setDebug(true);

        // 创建IMAP协议的Store对象
        Store store = session.getStore("imap");

        // 链接邮件服务器
        store.connect("××××@sina.com", "××××××");
        System.out.println("连上了");

        // 获得邮件收件箱
        Folder folder = store.getFolder("INBOX");
        // 以读写模式打开收件箱
        folder.open(Folder.READ_WRITE);

        folder.addMessageCountListener(new MessageCountListener() {
            public void messagesRemoved(MessageCountEvent messagecountevent) {
            }

            @Override
            public void messagesAdded(MessageCountEvent ev) {
                System.out.println("new message");
                Message[] msgs = ev.getMessages();
                for (int i = 0; i < msgs.length; i++) {
                    System.out.println(msgs[i].getMessageNumber());
                    try {
                        System.out.println("========");
                        System.out.println("***1"+msgs[i].toString());
                        System.out.println("***2"+(msgs[i]==null));
                        System.out.println("***3:"+msgs[i].getSubject().toString());
                        System.out.println("***4:"+msgs[i].getFrom().toString());
                    } catch (MessagingException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        boolean supportIDLE = false;
        try {
            if (folder instanceof IMAPFolder) {
                IMAPFolder f = (IMAPFolder) folder;
                f.idle();
                supportIDLE = true;
            }
        } catch (FolderClosedException fex) {
            update();
        } catch (MessagingException mex) {
            supportIDLE = false;
        }



输出的结果节选如下
========
***1com.sun.mail.imap.IMAPMessage@7b80f885
***2false
DONE
A2 OK IDLE completed
A3 FETCH 24 (ENVELOPE INTERNALDATE RFC822.SIZE)



可以看到,getSubject 和 getFrom都没有执行.
加载中
返回顶部
顶部