Supar Man
Supar Man

Reputation: 59

Javamail API - MessageCountListener is not getting Called

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();
    }

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

}

  }

adding Listener ..

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

Upvotes: 2

Views: 3275

Answers (1)

Bill Shannon
Bill Shannon

Reputation: 29961

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.

Upvotes: 2

Related Questions