带数据库的Spring Boot邮件接收器

问题描述

嗨,我正在尝试使用Spring Mail集成读取最终用户邮箱。我使用了以下代码效果很好。

@SpringBootApplication
public class ImapTestApplication {
     public static void main(String[] args) {
          SpringApplication.run(ImapTestApplication.class,args);
              ApplicationContext ac = new ClasspathXmlApplicationContext("/meta-inf/gmail-imap-idle-config.xml");

            DirectChannel inputChannel = ac.getBean("receiveChannel",DirectChannel.class);
            inputChannel.subscribe(new MessageHandler() {
                public void handleMessage(Message<?> message) throws MessagingException {
                    System.out.println("===================================");
                    System.out.println("Message: " + message);
                    System.out.println("===================================");
                }
            });
    }

我的xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:int-mail="http://www.springframework.org/schema/integration/mail"
    xmlns:util="http://www.springframework.org/schema/util">

    <int:channel id="receiveChannel" />
    <!-- replace 'userid and 'password' with the real values -->
    <int-mail:imap-idle-channel-adapter id="customAdapter"
            store-uri="imaps://mailaddress:password@host:993/inBox"
            channel="receiveChannel"
            auto-startup="true"
            should-delete-messages="false"
            should-mark-messages-as-read="false"
            java-mail-properties="javaMailProperties"/>

    <util:properties id="javaMailProperties">
        <prop key="mail.imap.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
        <prop key="mail.imap.socketFactory.fallback">false</prop>
        <prop key="mail.store.protocol">imaps</prop>
        <prop key="mail.imaps.auth">true</prop>
        <prop key="mail.debug">false</prop>
    </util:properties>

</beans>

在我的应用程序中,我的数据库中有100多个用户的邮箱信息。因此,我需要如何从数据库进行配置。而且在运行时,如果用户更改邮件设置(例如更改密码),它也应该执行我的邮件接收器,我尝试使用Spring Integration Java DSL来实现 但是我无法使用用户邮件数据库中的imap设置。

解决方法

使用XML配置确实没有简单的方法。

此处有一些示例:https://github.com/spring-projects/spring-integration-samples/tree/master/advanced/dynamic-ftp

为每个客户创建一个新的应用程序上下文。

使用Java DSL及其动态流功能,可以在运行时从任意代码中编写一些IntegrationFlow

您还可以将它们映射到那些用户,以便在属性更改时销毁并创建一个新用户。

在文档中查看更多信息:https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/dsl.html#java-dsl-runtime-flows