我正在研究這個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”?
3 回答

蕪湖不蕪
TA貢獻1796條經驗 獲得超7個贊
嘗試用這個:
inputChannel.subscribe(new MessageHandler() {
public void handleMessage(Message<?> message) throws MessagingException {
logger.info("Message: " + ((javax.mail.internet.MimeMessage) message.getPayload()).getContent());
}
});

縹緲止盈
TA貢獻2041條經驗 獲得超4個贊
通過訪問您正在記錄的對象的文檔(Message
接口)[0],您將找到一個getPayload
方法,該方法將返回以下內容的實際負載Message
:
T getPayload()
返回消息有效負載。
該有效負載對象可能具有檢索電子郵件數據的方法。在您的情況下,有效負載是一個IntegrationMimeMessage
[1],它擴展MimeMessage
并具有一個getContent
方法 [2]。所以你應該能夠做這樣的事情:
logger.info("Message content: " + message.getPayload().getContent());
[2] https://docs.oracle.com/javaee/6/api/javax/mail/internet/MimeMessage.html#getContent()
添加回答
舉報
0/150
提交
取消