Home > OS >  How to reach previously read emails by using Spring Integration?
How to reach previously read emails by using Spring Integration?

Time:01-07

I am developing a dynamic and multiple IMAP channel listener application. For the purpose of effectiveness, I am not downloading the attachments inside mails, just getting the texts inside them. Also I am developing an endpoint to access that previously arrived mails and download & return that attachment in order not to download every attachment. So basically I am trying to download attachments only if there is a demand.

I am using ImapIdleChannelAdapter to listen mails inside integration flow. Here is my flow,

public ImapIdleChannelAdapter mailAdapter(ImapMailReceiver receiver) {
        ImapIdleChannelAdapter imapAdapter = new ImapIdleChannelAdapter(receiver);
        imapAdapter.setAutoStartup(true);
        return imapAdapter;
    }
public IntegrationFlow createMailFlow(GmailRecieverRequirements requirements, String clientID) {
        return IntegrationFlow.from(
                mailAdapter(gmailMailReceiver(requirements)))
                .handle(getMailHandler())
                .get();
    }

My question is, how can I access those previously read mails in different time? I know Java Mail has Folder - UID structure to access mails via UIDs. Here is the link. However, I do not want to use javaMail inside my flow to save the UID. Is there any chance that I could reach UID of the mail inside the flow by Spring Integration? I am open to any other solution.

Thanks in advance

CodePudding user response:

This is indeed low-level mail API functionality. Not sure what you mean with your do not want to use javaMail inside my flow, but we only can do that on demand fetching if we know the folder and message UID and with that respective API of the IMAPFolder:

/**
 * Get the Message corresponding to the given UID. If no such 
 * message exists, <code>null</code> is returned.
 *
 * @param uid   UID for the desired message
 * @return      the Message object. <code>null</code> is returned
 *          if no message corresponding to this UID is obtained.
 * @exception   MessagingException for failures
 */
public Message getMessageByUID(long uid) throws MessagingException;

The ImapIdleChannelAdapter can produce raw jakarta.mail.Message to let you to obtain its UID via UIDFolder.getUID(Message message) API.

  • Related