Make mail tag unread after polling - Apache Camel

I am using apache camel, with a poll consumer when my mail poll is marked as read.

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


After polling, when I process the attachment, if any error occurs, I want to make the mail "unread". So I can unite again.

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

  "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) {


I noticed that the peek parameter setting it to true will not mark the mail as read while polling, in which case is there any option to mark it as read after processing.


source to share

1 answer

To get the result you want you must have options



The peek = true parameter should ensure that messages remain in an accurate state on the mail server, as they are prior to polling, even if there is an exception. However, this will not work at this time. This is actually a bug in the Camel Mail component. I posted a patch to and this will probably be fixed in a future release.

As a workaround, you can set mapMailMessages = false , but then you have to work with the content of the email messages yourself. In Camel 2.15 onwards, you also have a postProcessAction option , and with that, you can probably remove SEEN flags from processing error messages. That said, I would recommend waiting for the fix though.



All Articles