亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何打印 Spring Message 捕獲的電子郵件正文內容

如何打印 Spring Message 捕獲的電子郵件正文內容

慕娘9325324 2023-10-12 17:14:08
我正在研究這個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());

     }

});


查看完整回答
反對 回復 2023-10-12
?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

嘗試尋找屬性

簡單內容

在 ImapReceiver 上并將其設置為 false。

身體就會很飽滿!


查看完整回答
反對 回復 2023-10-12
?
縹緲止盈

TA貢獻2041條經驗 獲得超4個贊

通過訪問您正在記錄的對象的文檔(Message接口)[0],您將找到一個getPayload方法,該方法將返回以下內容的實際負載Message

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


查看完整回答
反對 回復 2023-10-12
  • 3 回答
  • 0 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號