Example usage for org.springframework.mail MailSendException getFailedMessages

List of usage examples for org.springframework.mail MailSendException getFailedMessages

Introduction

In this page you can find the example usage for org.springframework.mail MailSendException getFailedMessages.

Prototype

public final Map<Object, Exception> getFailedMessages() 

Source Link

Document

Return a Map with the failed messages as keys, and the thrown exceptions as values.

Usage

From source file:dk.teachus.backend.bean.impl.SpringMailBean.java

public void sendMail(final InternetAddress sender, final InternetAddress recipient, final String subject,
        final String body, final Type mailType) throws MailException {
    try {//from  w  w  w. j  av a 2 s.c o  m
        mailSender.send(new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage, false, "UTF-8");

                message.setFrom(sender);
                message.addTo(recipient);
                message.setSubject(subject);

                switch (mailType) {
                case HTML:
                    message.setText(body, true);
                    break;
                default:
                    message.setText(body);
                    break;
                }
            }
        });
    } catch (MailSendException e) {
        Map<?, ?> failedMessages = e.getFailedMessages();

        if (failedMessages != null && failedMessages.isEmpty() == false) {
            Object object = failedMessages.values().iterator().next();
            if (object != null) {
                Exception mailException = (Exception) object;
                if (mailException.getCause() instanceof SMTPAddressFailedException) {
                    throw new RecipientErrorMailException(e);
                }
            }
        }

        throw new MailException(e);
    }
}