【问题标题】:spring boot mail receiver with database带有数据库的spring boot邮件接收器
【发布时间】:2020-12-09 03:31:45
【问题描述】:

您好,我正在尝试使用 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 设置。

【问题讨论】:

    标签: java spring spring-boot spring-integration


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 2017-02-20
    • 2015-06-20
    • 2020-08-05
    • 2023-03-11
    • 1970-01-01
    • 2020-04-29
    • 2017-05-17
    • 2018-07-17
    • 1970-01-01
    相关资源
    最近更新 更多