【问题标题】:Cannot read email body with Spring: javax.mail.FolderClosedException无法使用 Spring 读取电子邮件正文:javax.mail.FolderClosedException
【发布时间】:2019-10-10 07:45:36
【问题描述】:

我正在尝试在我的 Gmail 收件箱中收听收到的邮件。每次新邮件到达时,我都想查看它的主题和内容。

到目前为止,我有这个:

import java.io.IOException;

import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.internet.ContentType;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.mail.util.MimeMessageParser;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.mail.transformer.MailToStringTransformer;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;


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){
                    MimeMessage mm = (MimeMessage) message.getPayload();
                    try {
                        System.out.println("Subject: "+mm.getSubject());
                        System.out.println("Body: "+readPlainContent(mm));
                    } 
                    catch (javax.mail.MessagingException e) {

                        System.out.println("MessagingException: "+e.getMessage());
                        e.printStackTrace();
                    }
                    catch (Exception e) {
                        System.out.println("Exception: "+e.getMessage());
                        e.printStackTrace();
                    }

            }
        });
    }

     private static String readHtmlContent(MimeMessage message) throws Exception {
            return new MimeMessageParser(message).parse().getHtmlContent();
        }

    private static String readPlainContent(MimeMessage message) throws Exception {
            return new MimeMessageParser(message).parse().getPlainContent();
        }
}

它可以正确阅读邮件主题。但是邮件正文没有运气。javax.mail.FolderClosedException打我。如何解决这个问题?

【问题讨论】:

  • 您需要出示您的gmail-imap-idle-config.xml。另外,尝试设置simple-content="true"

标签: java email spring-integration jakarta-mail


【解决方案1】:

正如加里所说:simple-content="true" 或最近autoCloseFolder = falsehttps://docs.spring.io/spring-integration/docs/5.2.0.RELEASE/reference/html/mail.html#mail-inbound

从 5.2 版开始,邮件接收器上提供了autoCloseFolder 选项。将其设置为 false 不会在获取后自动关闭文件夹,而是将IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE 标头(有关更多信息,请参阅MessageHeaderAccessor API)填充到从通道适配器发送给生产者的每条消息中。目标应用程序有责任在下游流中需要时调用此标头上的close()

【讨论】:

    猜你喜欢
    • 2019-12-16
    • 1970-01-01
    • 2017-05-13
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 2015-12-15
    相关资源
    最近更新 更多