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

时间:2019-10-09 09:16:41

标签: java spring gmail

我正在研究Spring mail sample。这段代码的作用是,每当新邮件到达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,   标头= {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,   timestamp = 1570612171063}]

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

3 个答案:

答案 0 :(得分:0)

通过访问要记录的对象的文档(Message接口)[0],您会发现一个getPayload方法,该方法将返回Message的实际有效载荷:< / p>

  

T getPayload()

     

返回消息有效负载。

此有效负载对象可能将具有检索电子邮件数据的方法。在您的情况下,有效载荷是一个IntegrationMimeMessage [1],它扩展了MimeMessage并具有一个getContent方法[2]。因此,您应该可以执行以下操作:

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

答案 1 :(得分:0)

尝试一下:

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

答案 2 :(得分:0)

尝试查找属性

  

simpleContent

在ImapReceiver上并将其设置为false。

身体将充满!

相关问题