Spring 集成 IMAP - 无法获取邮件正文

Spring Integration IMAP - cannot get the body of the message

这是我的 context.xml - 它按预期工作,我可以提取收到的电子邮件到我的 gmail 帐户 但主要问题是我尝试了很多解决方案来获取电子邮件的正文,但我没有找到 getContent() 给出空指针异常,找到了所有方法。 任何人都遇到了问题并且知道如何解决它?

    <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>

这是我试过的代码

    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;
    }

感谢大家的贡献,问题是消息负载的内容对于服务激活器 bean 总是为 null,最后我发现我应该把这个简单的行放在我的入站适配器配置中 simple-content="true"