Spring 集成 MimeMessage gmail 文件夹未打开异常

Spring Integration MimeMessage gmail Folder is not Open exception

每当我尝试读取 MimeMessage 的内容时,我都会收到“java.lang.IllegalStateException:文件夹未打开”异常。

我有一个非常简单的服务来处理收到的消息:

@Service
public class ReceiveMailService {

    private final Logger log = LoggerFactory.getLogger(ReceiveMailService.class);

    public void handleReceivedMail(MimeMessage receivedMessage) {
        try {
            log.debug("{}", receivedMessage.getContent());

            MimeMessageParser mimeMessageParser = new MimeMessageParser(receivedMessage).parse();  // it breaks here

            doMyStuff(mimeMessageParser);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}

这是我的配置 class:

@Configuration
@EnableIntegration
public class MailReceiverConfiguration {

    private static final Logger log = LoggerFactory.getLogger(MailReceiverConfiguration.class);

    @Value("${spring.mail.pop3.host}")
    private String host;
    @Value("${spring.mail.pop3.port}")
    private Integer port;
    @Value("${spring.mail.username}")
    private String username;
    @Value("${spring.mail.password}")
    private String password;

    private final ReceiveMailService receiveMailService;

    public MailReceiverConfiguration(ReceiveMailService receiveMailService) {
        this.receiveMailService = receiveMailService;
    }

    @Bean
    public IntegrationFlow mailListener() {
        return IntegrationFlows
            .from(Mail
                    .pop3InboundAdapter(host, port, username, password)
                    .javaMailProperties(p -> {
                        p.put("mail.debug", "false");
                        p.put("mail.pop3.socketFactory.fallback", "false");
                        p.put("mail.pop3.port", port);
                        p.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                        p.put("mail.pop3.socketFactory.port", port);
                    })
                    .maxFetchSize(10)
                    .shouldDeleteMessages(false),
                e -> e.poller(Pollers.fixedRate(5000).maxMessagesPerPoll(10))
            )
            .handle(message -> receiveMailService.handleReceivedMail((MimeMessage) message.getPayload()))
            .get();
    }

}

我 运行 不知道为什么这行不通。

请参阅此选项 use-cases 喜欢你的:

/**
 * When configured to {@code false}, the folder is not closed automatically after a fetch.
 * It is the target application's responsibility to close it using the
 * {@link org.springframework.integration.IntegrationMessageHeaderAccessor#CLOSEABLE_RESOURCE} header
 * from the message produced by this channel adapter.
 * @param autoCloseFolder set to {@code false} to keep folder opened.
 * @return the spec.
 * @since 5.2
 * @see AbstractMailReceiver#setAutoCloseFolder(boolean)
 */
public S autoCloseFolder(boolean autoCloseFolder) {

文档在这里:https://docs.spring.io/spring-integration/docs/current/reference/html/mail.html#mail-inbound

Starting with version 5.2, the autoCloseFolder option is provided on the mail receiver. Setting it to false doesn’t close the folder automatically after a fetch, but instead an IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE header (see MessageHeaderAccessor API for more information) is populated into every message to producer from the channel adapter.