Spring Mail Integration - How do i convert org.springframework.messaging.Message to javax.mail.internet.MimeMessage

Viewed 529

Im am creating a service that needs to listen & handle any new emails that are received in a mail account.

Problem: I am unable to convert the message that I receive in the handleMessageInternal(Message<?> message) method into any form of MimeMessage type that I can work with to get the to/cc addresses an/or any file attached.

My MailConfiguration Class:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.mail.dsl.Mail;
import org.springframework.integration.mail.support.DefaultMailHeaderMapper;
import org.springframework.integration.mapping.HeaderMapper;
import org.springframework.integration.scheduling.PollerMetadata;
import org.springframework.scheduling.support.PeriodicTrigger;

import javax.mail.internet.MimeMessage;

@Configuration
public class MailConfiguration {

     @Bean
    public IntegrationFlow imapMailFlow() {
        return IntegrationFlows
                .from(Mail.imapInboundAdapter("imaps://username:pass123@imap.gmail.com:" + 993 + "/INBOX")
                                .javaMailProperties(p -> p.put("mail.debug", "true"))
                                .headerMapper(mailHeaderMapper()),
                        e -> e.autoStartup(true).poller(p -> p.fixedDelay(5000)))
                .handle(new MailHandler())
                .get();
    }

    @Bean
    public HeaderMapper<MimeMessage> mailHeaderMapper() {
        return new DefaultMailHeaderMapper();
    }

    @Bean(name = PollerMetadata.DEFAULT_POLLER)
    public PollerMetadata defaultPoller() {

        PollerMetadata pollerMetadata = new PollerMetadata();
        pollerMetadata.setTrigger(new PeriodicTrigger(1000));
        return pollerMetadata;
    }
}

My MailHandler Class:

import org.springframework.integration.handler.AbstractMessageHandler;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
import javax.mail.internet.MimeMessage;

@Component
public class MailHandler extends AbstractMessageHandler {

    @Override
    protected void handleMessageInternal(Message<?> message) {
        System.out.println("RECEIVED NEW MAIL");

        //Doesnt Work
        MimeMessage mimeMessage = (MimeMessage) message.getPayload();

        //Doesnt Work
        MimeMessage mimeMessage = (MimeMessage) message;

    }
}

If it's possible to convert the message to a custom domain model that would also work.

Any help would be appreciated.

1 Answers

To get access to all the email content including attachments, you need to configure an autoCloseFolder(false) and should not have that .headerMapper(mailHeaderMapper()).

See Docs: https://docs.spring.io/spring-integration/docs/5.3.0.RC1/reference/html/mail.html#mail-mapping

When message mapping is enabled, the payload depends on the mail message and its implementation. Email contents are usually rendered by a DataHandler within the MimeMessage.

And also here: https://docs.spring.io/spring-integration/docs/5.3.0.RC1/reference/html/mail.html#mail-inbound

Keeping the folder open is useful in cases where communication with the server is needed during parsing multipart content of the email with attachments.

This way a message.getPayload() in your handler indeed can be cast to the MimeMessage.

Related