Search code examples
javaemailapache-camelpooling

Apache Camel reading unread mail from gmail account


Just tried learning Apache Camel.

I am trying to read gmail inbox unread mail.

I got the code snippet while searching but not able to get success from it.

if someone point out the mistake,

    PollingConsumer pollingConsumer = null;
    CamelContext context = new DefaultCamelContext();

   Endpoint endpoint = context.getEndpoint("imaps://imap.gmail.com?username=" + mailId + "&password=" + password + "&delete=false&peek=false&unseen=true&consumer.delay=6000&closeFolder=false&disconnect=false");


    System.out.println("end point:"+endpoint);

    pollingConsumer = endpoint.createPollingConsumer();
    System.out.println("polling consumer:"+pollingConsumer);
    pollingConsumer.start();

    pollingConsumer.getEndpoint().createExchange();
    System.out.println("Exchange is created:");
    Exchange exchange = pollingConsumer.receive();
    System.out.println("pollingConsumer.receive()");

pollingConsumer.receive(); is getting blocked, I have unread mail in my mailbox. Also I tried pollingConsumer.receive(6000); but it returns null.

I enable IMAP access in Gmail settings. is there any thing I am missing?


Solution

  • Let me write the solution, It will help someone facing similar issue .

    Actually I have added java mail jar, but imap jar was missing and it was not displaying any error for this.

    That is why I was not able to figure out the actual cause.

    After browsing the parameters of "imaps://imap.gmail.com", I came across "debugMode" parameter which by default is false. when I added that parameter with value true, then it complained of missing jar on my console. After adding that jar thinks work perfectly.

    Thanks for help.