lrkwz
lrkwz

Reputation: 6513

spring integration mail processor

I wish to use this samples as a guide to build a sort of "mail processor" which:

  1. List item
  2. read from IMAP
  3. save attachments
  4. persist sender info and attachment path in database
  5. send a confirmation email back to the sender

I've build the beans to read mbox and save the attachments as in Gunnar Hillert intermediate email samples:

    (...)
    <int:channel id="sendChannel" />

    <int-mail:outbound-channel-adapter
        channel="sendChannel" host="${smtp.host}" username="${imap.username}"
        password="${imap.password}" />

    <int:channel id="receiveChannel" />

    <!-- replace 'userid and 'password' wit the real values -->
    <int-mail:imap-idle-channel-adapter
        id="customAdapter"
        store-uri="imap://${imap.username}:${imap.password}@${imap.host}:${imap.port}/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">true</prop>
        <prop key="mail.store.protocol">imap</prop>
        <prop key="mail.debug">${mail.debug}</prop>
    </util:properties>

    <int:chain id="transform-split" input-channel="receiveChannel"
        output-channel="outputChannel">
        <int:transformer>
            <bean class="it.lrkwz.imap.support.EmailTransformer" />
        </int:transformer>
        <int:splitter>
            <bean class="it.lrkwz.imap.support.EmailSplitter" />
        </int:splitter>
    </int:chain>

    <int:channel id="outputChannel" />

    <int-file:outbound-channel-adapter
        id="save-as-file" auto-create-directory="true" channel="outputChannel"
        directory-expression="'target/out/' + headers.directory" />
</beans>

Where (main(), EmailTransformer(), EmailSplitter(), ...) should I intercept the "no attachment" state? How do I send an email back to se sender?

@SpringBootApplication
@ImportResource("classpath:META-INF/spring/integration/aruba-imap-idle-config.xml")
public class ImapProcessorApplication implements CommandLineRunner {
    private static Logger logger = LoggerFactory.getLogger(ImapProcessorApplication.class);

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(ImapProcessorApplication.class, args);
    }

    @Autowired
    DirectChannel receiveChannel;

    @Autowired
    DirectChannel sendChannel;

    @Override
    public void run(String... arg0) throws Exception {
        receiveChannel.subscribe(new MessageHandler() {
            public void handleMessage(Message<?> message) throws MessagingException{
    ...

Thank you.

Upvotes: 1

Views: 1417

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121272

To handle "no attachment" state you should really follow with standard Spring Integration recipes and add <filter> to your flow. Looks like just after your <splitter> (if you split your message by `Multipart). Here you can find, BTW, how to split and filter: Download attachments using Java Mail.

You already have <int-mail:outbound-channel-adapter> to send emails. So, what you need just make your outputChannel as <publish-subscriber-channel> and add more subscribers (<outbound-channel-adapter>, <service-activator>, <outbound-gateway> etc.) and send confirmation email, and store the data to DB. Anything what you want and need!

Upvotes: 1

Related Questions