Announcement Announcement Module
Collapse
No announcement yet.
int-mail:outbound-adapter and email with Attachment, is it possible? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • int-mail:outbound-adapter and email with Attachment, is it possible?

    Hi everyone!

    Simple question and i suppose the answer is NO. I couldn't find any sample, sad....

    Thanks

  • #2
    I couldn't find an out-of-the-box way to do it so wrote a transformer to create a MimeMessage with attachment:

    Code:
        <si:transformer input-channel="errorEmailHandler" output-channel="errorEmailSender"
                        ref="genericExceptionToMail"/>
    
        <bean id="genericExceptionToMail" class="com.mypackage.GenericExceptionToMail">
            <property name="mailSender" ref="mailSender"/>
            <property name="subject" value="${mail_subject}"/>
            <property name="from" value="${mail_from_address}"/>
            <property name="to">
                <list>
                    <value>[email protected]</value>
                </list>
            </property>
            <property name="cc">
                <list>
                    <value>[email protected]</value>
                </list>
            </property>
            <property name="bcc">
                <list>
                </list>
            </property>
            <property name="attachPayload" value="${mail_attach_payload}"/>
        </bean>
    
        <mail:outbound-channel-adapter channel="errorEmailSender"
                                       mail-sender="mailSender"/>
    
        <!-- mail config -->
        <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
            <property name="host" value="${mail_host}"/>
            <property name="port" value="${mail_port}"/>
        </bean>
    The transformer looks something like this (without the getters and setters of the bean):

    Code:
    /**
     * Takes a generic exception message, creates a MailMessage
     * and optionally adds the original payload as an attachment
     */
    public class GenericExceptionToMail extends AbstractTransformer {
    
        private JavaMailSender mailSender;
        private boolean attachPayload;
    
        private String subject;
        private String from;
        private List<String> to;
        private List<String> cc;
        private List<String> bcc;
    
        @Override
        protected Object doTransform(Message<?> message) throws Exception {
    
            MimeMessage mail = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mail, true);
    
            if (message.getPayload() instanceof MessagingException) {
                helper.setText(((MessagingException)message.getPayload()).getCause().getMessage());
            } else {
                helper.setText(message.getPayload().toString());
            }
            helper.setSubject(subject);
            helper.setFrom(from);
            helper.setTo(to.toArray(new String[to.size()]));
    
            if (cc != null) {
                helper.setCc(cc.toArray(new String[cc.size()]));
            }
            if (bcc != null) {
                helper.setBcc(bcc.toArray(new String[bcc.size()]));
            }
            
            if (attachPayload) {
                Object failedPayload = SpringMessageExceptionUtil.getFailedPayload(message);
                byte[] attachment;
                if (failedPayload instanceof Document) {
                    Transformer transformer = TransformerFactory.newInstance().newTransformer();
                    StreamResult result = new StreamResult(new StringWriter());
                    DOMSource source = new DOMSource((Document)failedPayload);
                    transformer.transform(source,result);
                    attachment = result.getWriter().toString().getBytes();
                } else {
                    attachment = failedPayload.toString().getBytes();
                }
                helper.addAttachment("failed-msg.txt", new ByteArrayResource(attachment));
            }
    
            return new MimeMailMessage(mail);
        }

    Comment

    Working...
    X