首页 > 解决方案 > 带有数据库的spring boot邮件接收器

问题描述

嗨,我正在尝试使用 spring 邮件集成来读取最终用户的邮箱。我使用了以下代码,它工作正常。

@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 设置。

标签: javaspringspring-bootspring-integration

解决方案


使用 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


推荐阅读