4

Am using apache camel, With Polling consumer, when poll my mail is mark as read.

options : delete=false&peek=false&unseen=true

After polling , when i am processing the attachment, if any error occurs , i want to make the mail as "unread". So that i can pool again later.

public void process(Exchange exchange) throws Exception {
    Map<String, DataHandler> attachments = exchange.getIn().getAttachments();

    Message messageCopy = exchange.getIn().copy();

    if (messageCopy.getAttachments().size() > 0) {
        for (Map.Entry<String, DataHandler> entry : messageCopy.getAttachments().entrySet()) {

            DataHandler dHandler = entry.getValue();

            // get the file name
            String filename = dHandler.getName();

            // get the content and convert it to byte[]
            byte[] data =
                    exchange.getContext().getTypeConverter().convertTo(byte[].class, dHandler.getInputStream());

            log.info("Downloading attachment, file name : " + filename);
            InputStream fileInputStream = new ByteArrayInputStream(data);
            try {
                // Processing attachments
                // if any error occurs here, i want to make the mail mark as unread
            } catch (Exception e) {
                log.info(e.getMessage());
            }
        }
    }
}

I noticed the option peek, by setting it to true, It will not make the mail mark as read during polling, in that case is there any option to make it mark as read after processing.

Benoy Prakash
  • 436
  • 5
  • 17

2 Answers2

2

To get the result that you want you should have options

peek=true&unseen=true

The peek=true option is supposed to ensure that messages remain the exact state on the mail server as they where before polling even if there is an exception. However, currently it won't work. This is actually a bug in Camel Mail component. I've submitted a patch to https://issues.apache.org/jira/browse/CAMEL-9106 and this will probably be fixed in a future release.

As a workaround you can set mapMailMessages=false but then you will have to work with the email message content yourself. In Camel 2.15 onward you also have postProcessAction option and with that you could probably remove the SEEN flags from messages with processing errors. Still, I would recommend waiting for the fix though.

jnupponen
  • 715
  • 1
  • 5
  • 16
0

We can set the mail unread flag with the following code

public void process(Exchange exchange) throws Exception {   
  final Message mailMessage = exchange.getIn(MailMessage.class).getMessage();
  mailMessage.setFlag(Flag.SEEN, false);
}
Nandan
  • 497
  • 2
  • 7
  • 18