-1

hi i'm trying to read the end users mailbox by using spring mail integration. I have used following code it's working fine.

@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("===================================");
                }
            });
    }

And my xml configuration

<?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>

In my application i have more than 100 users mailbox information in my database. So how i need to configure from my database. And also in runtime if user change the mail setting like password change it should be perform my mail receiver as well, I try to implement using Spring Integration Java DSL but i cant use the imap settings from user mail database.

Thilakar Raj
  • 37
  • 13

1 Answers1

0

There is really no easy way to do that with XML configuration.

Se some example here: https://github.com/spring-projects/spring-integration-samples/tree/master/advanced/dynamic-ftp

A new application context is created for every customer.

With Java DSL and its dynamic flows feature it is much easier to cook some IntegrationFlow from any arbitrary code at runtime.

You also can have them mapped to those users to be able to destroy and create a new one when properties are changed.

See more in docs: https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/dsl.html#java-dsl-runtime-flows

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Thanks for your response. Is there any sample code or detailed doc JavaDsL with IMAP mail service? – Thilakar Raj Aug 19 '20 at 20:16
  • See here in docs: https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/mail.html#java-dsl-configuration. And here as a test suite: https://github.com/spring-projects/spring-integration/blob/master/spring-integration-mail/src/test/java/org/springframework/integration/mail/dsl/MailTests.java – Artem Bilan Aug 19 '20 at 20:17
  • Also see [this answer](https://stackoverflow.com/questions/32826864/spring-multiple-imapadapter/32827733#32827733). It taks the dynamic-ftp approach for mail adapters using child contexts and Java Configuration, but I agree with Artem, dynamic DSL is the best solution now. – Gary Russell Aug 19 '20 at 21:32