猿问

如何打印 Spring Message 捕获的电子邮件正文内容

我正在研究这个Spring 邮件样本。这段代码的作用是每次有新邮件到达 Gmail 收件箱时都会打印消息。

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

            }

        });

    }

}

我发送了 2 封电子邮件,最终这些行出现在 Eclipse 控制台上:


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


现在如何检索正文内容?例如邮件正文上是“hello world 123”?


慕娘9325324
浏览 123回答 3
3回答

芜湖不芜

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

喵喵时光机

尝试寻找属性简单内容在 ImapReceiver 上并将其设置为 false。身体就会很饱满!

缥缈止盈

通过访问您正在记录的对象的文档(Message接口)[0],您将找到一个getPayload方法,该方法将返回以下内容的实际负载Message:T getPayload()返回消息有效负载。该有效负载对象可能具有检索电子邮件数据的方法。在您的情况下,有效负载是一个IntegrationMimeMessage[1],它扩展MimeMessage并具有一个getContent方法 [2]。所以你应该能够做这样的事情:logger.info("Message&nbsp;content:&nbsp;"&nbsp;+&nbsp;message.getPayload().getContent());[0]&nbsp;https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/messaging/Message.html[1]&nbsp;https://github.com/spring-projects/spring-integration/blob/master/spring-integration-mail/src/main/java/org/springframework/integration/mail/AbstractMailReceiver.java#L646[2]&nbsp;https://docs.oracle.com/javaee/6/api/javax/mail/internet/MimeMessage.html#getContent()
随时随地看视频慕课网APP

相关分类

Java
我要回答