How to print the email body content captured with Spring Message

Viewed 1091

I'm studying this Spring mail sample. What this code does is it will print message every time a new mail arrives in the Gmail inbox.

package org.springframework.integration.samples.mail.imapidle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;


/**
 * @author Oleg Zhurakousky
 * @author Gary Russell
 *
 */
public class GmailInboundImapIdleAdapterTestApp {
    private static Log logger = LogFactory.getLog(GmailInboundImapIdleAdapterTestApp.class);


    public static void main (String[] args) throws Exception {
        @SuppressWarnings("resource")
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(
                "/META-INF/spring/integration/gmail-imap-idle-config.xml");
        DirectChannel inputChannel = ac.getBean("receiveChannel", DirectChannel.class);
        inputChannel.subscribe(new MessageHandler() {
            public void handleMessage(Message<?> message) throws MessagingException {
                logger.info("Message: " + message);
            }
        });
    }
}

I sent 2 emails, and eventually these lines appear on Eclipse console:

16:04:52.851 INFO [pool-2-thread-1][org.springframework.integration.samples.mail.imapidle.GmailInboundImapIdleAdapterTestApp] Message: GenericMessage [payload=org.springframework.integration.mail.AbstractMailReceiver$IntegrationMimeMessage@4ac650aa, headers={id=869e46a9-8fd0-4351-4f1e-bb181286b05f, timestamp=1570611892844}] 16:09:31.063 INFO [pool-2-thread-1][org.springframework.integration.samples.mail.imapidle.GmailInboundImapIdleAdapterTestApp] Message: GenericMessage [payload=org.springframework.integration.mail.AbstractMailReceiver$IntegrationMimeMessage@76114690, headers={id=6c791751-668e-69c5-3e05-1ae1ec72f853, timestamp=1570612171063}]

Now how to retrieve the body content? E.g on the mail body is "hello world 123"?

3 Answers

By visiting the documentation of the object you are logging (Message interface)[0], you will find a getPayload method which will return the actual payload of the Message:

T getPayload()

Return the message payload.

This payload object will probably have methods to retrieve data of your email message. In your case, the payload is a IntegrationMimeMessage[1] which extends MimeMessage and has a getContent method[2]. So you should be able to do something like this:

logger.info("Message content: " + message.getPayload().getContent());

[0] https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/messaging/Message.html

[1] https://github.com/spring-projects/spring-integration/blob/master/spring-integration-mail/src/main/java/org/springframework/integration/mail/AbstractMailReceiver.java#L646

[2] https://docs.oracle.com/javaee/6/api/javax/mail/internet/MimeMessage.html#getContent()

Try with this :

inputChannel.subscribe(new MessageHandler() {
     public void handleMessage(Message<?> message) throws MessagingException {
        logger.info("Message: " + ((javax.mail.internet.MimeMessage) message.getPayload()).getContent());
     }
});

Try to find properties

simpleContent

on ImapReceiver and set it to false.

The body will be full!

Related