Spring Integration: SMTP server

936 views Asked by At

I am using Spring Integration 4.1.2.

In the program flow, at the end of a process I need to send an email. I am using the following:

<int:payload-type-router input-channel="response.in">
    <int:mapping type="java.lang.String" channel="response.out"/>
    <int:mapping type="org.springframework.mail.SimpleMailMessage" channel="mail.out"/>
</int:payload-type-router>

<mail:outbound-channel-adapter channel="mail.out" mail-sender="mailSender"/>

This is working fine.

I want to also handle the situation when the mail server is down.

I've tried the following solution but it does not work.

<int:chain input-channel="errorChannel" output-channel="emailErrorChannel">       
    <int:transformer ref="errorUnwrapper" />
</int:chain>

And the unwrapper:

@MessageEndpoint
public class ErrorUnwrapper {

    @Transformer
    public Message<?> transform(final ErrorMessage errorMessage) {
        Message<?> failedMessage = ((MessagingException) errorMessage.getPayload()).getFailedMessage();
        return failedMessage;
    }
}

But this is not working. I want to catch the exception and send a meaningful response back to the user instead of a stacktrace. And I want to do this within Spring Integration. Otherwise I'll have to write a Java mail service call with a service-activator instead of the SI mail extension.

Or is it a one-way component? I've just found this blog post doing the same. I've also read this but got nowhere.

1

There are 1 answers

1
Gary Russell On BEST ANSWER

You likely need to add the errorChannel to whatever starts you flow; you need to show us the rest of your configuration.

Alternatively, you can add an ExpressionEvaluatingAdvice to the mail adapter; see this sample for an example of using the advice.