Javamail API - MessageCountListener没有被调用 [英] Javamail API - MessageCountListener is not getting Called

查看:629
本文介绍了Javamail API - MessageCountListener没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Javamail API从我的Gmail收件箱中读取未读邮件。
这是我的代码...

I am trying to read Unread mail from my gmail Inbox using Javamail API. Here is my code ...

final Properties props = new Properties();
    props.setProperty("mail.store.protocol", "imaps");
    String attachFiles = "";
    try
    {
        final Session session = Session.getInstance(props, null);
        final Store store = session.getStore();
        store.connect("imap.gmail.com", "*********", "*********");
        final Folder inbox = store.getFolder("INBOX");
        final MailCountEventListener listener = new MailCountEventListener();
        inbox.addMessageCountListener(listener);


        inbox.open(Folder.READ_ONLY);
        final Message msg = inbox.getMessage(inbox.getMessageCount());
        final Address[] in = msg.getFrom();
        for (final Address address : in)
        {
            System.out.println("FROM:" + address.toString());
        }
        final Multipart mp = (Multipart) msg.getContent();
        final BodyPart bp = mp.getBodyPart(0);
        if (msg.getContentType().contains("multipart"))
        {
            final int numberOfParts = mp.getCount();
            for (int partCount = 0; partCount < numberOfParts; partCount++)
            {
                final MimeBodyPart part = (MimeBodyPart) mp.getBodyPart(partCount);
                if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()))
                {
                    // this part is attachment
                    final String fileName = part.getFileName();
                    attachFiles += fileName + ", ";
                    part.saveFile("E:/" + File.separator + fileName);
                }
                else
                {
                    System.out.println("MultiPart Message Content :" + part.getContent().toString());
                }
            }

            if (attachFiles.length() > 1)
            {
                attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
            }

            System.out.println("Attachments: " + attachFiles);
        }
        System.out.println("SENT DATE:" + msg.getSentDate());
        System.out.println("SUBJECT:" + msg.getSubject());
        System.out.println("CONTENT:" + bp.getContent());
    }
    catch (final Exception mex)
    {
        mex.printStackTrace();
    }

我的代码正常工作。它正在阅读附有模板的邮件正文。现在我想调用 MessageCountListener ,以便如果任何新的邮件将进入我的收件箱,监听器应该被自动调用并读取新的邮件正文。

My code is working fine. It is reading mail body with attached templates. Now I want to call MessageCountListener so that if any new Mail will come into my Inbox, Listener should be called automatically and read new mail body.

但是这里的问题是我的监听器没有调用。

But Here the problem is My listener is not calling.

public class MailCountEventListener implements MessageCountListener
{


/*
 * (non-Javadoc)
 * 
 * @see javax.mail.event.MessageCountListener#messagesAdded(javax.mail.event.MessageCountEvent)
 */
@Override
public void messagesAdded(final MessageCountEvent messagecountevent)
{

    String attachFiles = "";

    System.out.println("message listner invoked.");
    final Message[] msgs = messagecountevent.getMessages();

    System.out.println("Got " + msgs.length + " new messages");
    try
    {
        final Message msg = msgs[0];
        final Address[] in = msg.getFrom();
        for (final Address address : in)
        {
            System.out.println("FROM:" + address.toString());
        }
        final Multipart mp = (Multipart) msg.getContent();
        final BodyPart bp = mp.getBodyPart(0);
        if (msg.getContentType().contains("multipart"))
        {
            final int numberOfParts = mp.getCount();
            for (int partCount = 0; partCount < numberOfParts; partCount++)
            {
                final MimeBodyPart part = (MimeBodyPart) mp.getBodyPart(partCount);
                if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()))
                {
                    // this part is attachment
                    final String fileName = part.getFileName();
                    attachFiles += fileName + ", ";
                    part.saveFile("E:/" + File.separator + fileName);
                }
                else
                {
                    System.out.println("MultiPart Message Content :" + part.getContent().toString());
                }
            }

            if (attachFiles.length() > 1)
            {
                attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
            }

            System.out.println("Attachments: " + attachFiles);
        }
        System.out.println("SENT DATE:" + msg.getSentDate());
        System.out.println("SUBJECT:" + msg.getSubject());
        System.out.println("CONTENT:" + bp.getContent());

    }
    catch (final Exception mex)
    {
        mex.printStackTrace();
    }
}

/*
 * (non-Javadoc)
 * 
 * @see javax.mail.event.MessageCountListener#messagesRemoved(javax.mail.event.MessageCountEvent)
 */
@Override
public void messagesRemoved(final MessageCountEvent messagecountevent)
{
    // YTODO Auto-generated method stub

}

  }

添加监听器..

final MailCountEventListener listener = new MailCountEventListener();
inbox.addMessageCountListener(listener);

我无法弄明白我做错了什么。我的活动没有打电话。请帮助

I am not able to figure out where I am doing wrong. My event is not calling. Please help

推荐答案

在执行命令时,服务器只会通知客户端新消息。您需要定期执行命令,例如通过调用getMessageCount,或者您需要使用 IMAP IDLE支持等待通知。另请参阅 IdleManager ,这是新功能 JavaMail 1.5.2

The server only notifies the client of new messages as part of executing a command. You either need to execute a command periodically, e.g., by calling getMessageCount, or you need to use the IMAP IDLE support to wait for notifications. See also the IdleManager that's new in JavaMail 1.5.2.

这篇关于Javamail API - MessageCountListener没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆