Search code examples
springspring-integrationjakarta-mailmime-message

Spring Integration IMAP - cannot get the body of the message


Here is my context.xml - it is working as expected, I can fetch email received to my gmail account But the main problem is that I tried many solution to get the body of the email but I failed the getContent() give null pointer exception with all method found. Anyone has the issue and know how to solve it?

    <util:properties id="javaMailProperties">
        <prop key="mail.imap.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
        <prop key="mail.imap.socketFactory.fallback">false</prop>
        <prop key="mail.store.protocol">imaps</prop>
        <prop key="mail.debug">false</prop>
    </util:properties>


    <!--  In case you didn't notice: should-delete-messages="false" will make it not delete your emails from the inbox, and should-mark-messages-as-read="true" will mark them as read. -->
    <int-mail:inbound-channel-adapter
        id="imapAdapter"
        store-uri="${imap.uri}"
        channel="recieveEmailChannel"
        should-delete-messages="false" 
        should-mark-messages-as-read="true"
        auto-startup="true" 
        java-mail-properties="javaMailProperties">
        <int:poller fixed-delay="${imap.poolerSecondsDelay}" time-unit="SECONDS" />
    </int-mail:inbound-channel-adapter>

    <int:channel id="recieveEmailChannel">
        <int:interceptors>
            <int:wire-tap channel="logger" />
        </int:interceptors>
    </int:channel>

    <int:logging-channel-adapter id="logger"
        level="DEBUG" />

    <int:service-activator
        input-channel="recieveEmailChannel" ref="emailReceiverService"
        method="receive" />

    <bean id="emailReceiverService"
        class="com.dtp.integration.EmailReceiverService">
    </bean>

</beans>

Here is the code I tried

    public void receive(Message<?> message) throws MessagingException, IOException {
        MimeMessage mimeMessage = (MimeMessage) message.getPayload();
        mimeMessage.getAllHeaderLines();
        String messageContent = getTextFromMessage(mimeMessage);
    }

    private String getTextFromMessage(MimeMessage message) throws MessagingException, IOException {
        String result = "";
        if (message.isMimeType("text/plain")) {
            result = message.getContent().toString();
        } else if (message.isMimeType("multipart/*")) {
            MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();
            result = getTextFromMimeMultipart(mimeMultipart);
        }
        return result;
    }

    private String getTextFromMimeMultipart(
            MimeMultipart mimeMultipart)  throws MessagingException, IOException{
        String result = "";
        int count = mimeMultipart.getCount();
        for (int i = 0; i < count; i++) {
            BodyPart bodyPart = mimeMultipart.getBodyPart(i);
            if (bodyPart.isMimeType("text/plain")) {
                result = result + "\n" + bodyPart.getContent();
                break; // without break same text appears twice in my tests
            } else if (bodyPart.isMimeType("text/html")) {
                String html = (String) bodyPart.getContent();
                result = result + "\n" + org.jsoup.Jsoup.parse(html).text();
            } else if (bodyPart.getContent() instanceof MimeMultipart){
                result = result + getTextFromMimeMultipart((MimeMultipart)bodyPart.getContent());
            }
        }
        return result;
    }

Solution

  • thank you all for your contribution, the problem was with content of the message payload coming always null to the service activator bean, at the end I found that I should put this simple line on my inbound adapter configuration simple-content="true"