Christian Ibanibo
Christian Ibanibo

Reputation: 255

Spring integration imap cant read from sent and trash folder except inbox folder

I am new to spring integration and i need help. I can read and process mails from my inbox folder with the code below

@Bean
public IntegrationFlow imapMailFlow() throws UnsupportedEncodingException {
    return IntegrationFlows
            .from(Mail.imapInboundAdapter(imapUrl())
            .searchTermStrategy(new UnreadSearchTermStrategy())
            .userFlag("testSIUserFlag")
            .simpleContent(true)
            .javaMailProperties(p -> p.put("mail.debug", "false")),
                    e -> e.autoStartup(true)
            .poller(p -> p.fixedDelay(100)))
            .channel(MessageChannels.queue("imapInboxChannel")).get();
}
private String imapUrl() throws UnsupportedEncodingException {
    return "imaps://" + URLEncoder.encode(mailHostService.getMailHost().getEmail(), "UTF-8") + ":"
            + mailHostService.getMailHost().getPassword() + "@" + mailHostService.getMailHost().getIncominghost()
            + ":" + mailHostService.getMailHost().getIncomingport() + "/" + "INBOX";
}

@Bean
@ServiceActivator(inputChannel = "imapInboxChannel")
public MessageHandler processNewEmail() {
    MessageHandler messageHandler = new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) {
            MimeMessage mimeMessage = (MimeMessage) message.getPayload();
            processInboxMail.handleInboxMail(mimeMessage);
        }
    };
    return messageHandler;

}

But when i wrote the same code for sent folder and trash folder i cant read mails yet am not having any error. I have tested it with different mail clients, it only works with inbox folder

@Bean
public IntegrationFlow imapSentFlow() throws UnsupportedEncodingException {
    return IntegrationFlows
            .from(Mail.imapInboundAdapter(imapSentUrl())
            //.searchTermStrategy(new UnreadSearchTermStrategy())
            .simpleContent(true)
            .javaMailProperties(p -> p.put("mail.debug", "false")),
                    e -> e.autoStartup(true)
            .poller(p -> p.fixedDelay(100)))
            .log("checking in the logs")
            .channel(MessageChannels.queue("imapSentChannel")).get();
}
private String imapSentUrl() throws UnsupportedEncodingException {
    return "imaps://" + URLEncoder.encode(mailHostService.getMailHost().getEmail(), "UTF-8") + ":"
            + mailHostService.getMailHost().getPassword() + "@" + mailHostService.getMailHost().getIncominghost()
            + ":" + mailHostService.getMailHost().getIncomingport() + "/" + "INBOX.Sent";
}

@Bean
@ServiceActivator(inputChannel = "imapSentChannel")
public MessageHandler processNewSentEmail() {
    MessageHandler messageHandler = new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) {
            System.out.println("Message Receive");
            MimeMessage mimeMessage = (MimeMessage) message.getPayload();
            processInboxMail.handleInboxMail(mimeMessage);
        }
    };
    return messageHandler;

}

Upvotes: 0

Views: 432

Answers (1)

Thilo Schwarz
Thilo Schwarz

Reputation: 782

Let's move the discussion out of the comments.

Ok, you have to keep in mind, what you want to do. I recommend to use an extra imap mailbox for processing emails with spring integration. If not, you have to check every time all SEEN flagged emails to decide, if it is relevant for processing or not. That could by a lot of emails!

Anyway.

If you want to have trackable email processing, I success to have two processes:

  1. Process: Searching the Inbox for relevant emails and moving those email to a separate folder e.g. to_process.
  2. Process: Proccessing all emails in to_process. If the processing was successful, move the mail into the folder done, otherwise into process_failed.

Hope, this is what you need. If not, you should describe your email processing a bit.

Upvotes: 1

Related Questions