Example usage for org.springframework.mail MailSendException MailSendException

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

Introduction

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

Prototype

public MailSendException(Map<Object, Exception> failedMessages) 

Source Link

Document

Constructor for registration of failed messages, with the messages that failed as keys, and the thrown exceptions as values.

Usage

From source file:com.mulodo.survey.batch.writer.TestMailSender.java

@Override
public void send(SimpleMailMessage[] simpleMessages) throws MailException {
    Map<Object, Exception> failedMessages = new LinkedHashMap<Object, Exception>();
    for (SimpleMailMessage simpleMessage : simpleMessages) {
        try {//w w  w  . j av  a  2s. c  o m
            this.mailSender.send(simpleMessage);
        } catch (MailException ex) {
            throw new MailSendException(failedMessages);

        }
    }
}

From source file:com.netflix.genie.core.services.impl.MailServiceImplUnitTests.java

/**
 * Make sure if we can't send an email an exception is thrown.
 *
 * @throws GenieException On error//from  w w  w . j av  a2 s  .  c o m
 */
@Test(expected = GenieServerException.class)
public void cantSendEmail() throws GenieException {
    final String to = UUID.randomUUID().toString();
    final String subject = UUID.randomUUID().toString();
    final String body = UUID.randomUUID().toString();

    Mockito.doThrow(new MailSendException("a")).when(this.mailSender)
            .send(Mockito.any(SimpleMailMessage.class));
    this.mailService.sendEmail(to, subject, body);
}

From source file:com.postmark.PostmarkMailSender.java

@Override
public void send(SimpleMailMessage message) throws MailException {

    HttpClient httpClient = new DefaultHttpClient();
    PostmarkResponse theResponse = new PostmarkResponse();

    try {//ww  w .ja  v a2s.  c o m

        // Create post request to Postmark API endpoint
        HttpPost method = new HttpPost("http://api.postmarkapp.com/email");

        // Add standard headers required by Postmark
        method.addHeader("Accept", "application/json");
        method.addHeader("Content-Type", "application/json; charset=utf-8");
        method.addHeader("X-Postmark-Server-Token", serverToken);
        method.addHeader("User-Agent", "Postmark-Java");

        // Convert the message into JSON content
        String messageContents = UnicodeEscapeFilterWriter.escape(gson.toJson(message));
        logger.log(Level.FINER, "Message contents: " + messageContents);

        // Add JSON as payload to post request
        StringEntity payload = new StringEntity(messageContents);
        payload.setContentEncoding(HTTP.UTF_8);
        method.setEntity(payload);

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        try {
            String response = httpClient.execute(method, responseHandler);
            logger.log(Level.FINER, "Message response: " + response);
            theResponse = gson.fromJson(response, PostmarkResponse.class);
            theResponse.status = PostmarkResponseStatus.SUCCESS;
        } catch (HttpResponseException hre) {
            switch (hre.getStatusCode()) {
            case 401:
            case 422:
                logger.log(Level.SEVERE, "There was a problem with the email: " + hre.getMessage());
                theResponse.setMessage(hre.getMessage());
                theResponse.status = PostmarkResponseStatus.USERERROR;
                throw new MailSendException("Postmark returned: " + theResponse);
            case 500:
                logger.log(Level.SEVERE, "There has been an error sending your email: " + hre.getMessage());
                theResponse.setMessage(hre.getMessage());
                theResponse.status = PostmarkResponseStatus.SERVERERROR;
                throw new MailSendException("Postmark returned: " + theResponse);
            default:
                logger.log(Level.SEVERE,
                        "There has been an unknow error sending your email: " + hre.getMessage());
                theResponse.status = PostmarkResponseStatus.UNKNOWN;
                theResponse.setMessage(hre.getMessage());
                throw new MailSendException("Postmark returned: " + theResponse);
            }
        }

    } catch (Exception e) {
        logger.log(Level.SEVERE, "There has been an error sending email: " + e.getMessage());
        throw new MailSendException("There has been an error sending email", e);

    } finally {
        httpClient.getConnectionManager().shutdown();
    }

}

From source file:cherry.foundation.mail.SendMailBatchTest.java

private SendMailBatch create(long intervalMillis, File shutdownTrigger, boolean sendMailException) {

    bizDateTime = mock(BizDateTime.class);
    mailSendHandler = mock(MailSendHandler.class);
    when(mailSendHandler.listMessage((LocalDateTime) any())).thenReturn(asList(1L, 2L, 3L));
    if (sendMailException) {
        when(mailSendHandler.sendMessage(anyLong())).thenThrow(new MailSendException("sending mail failed"));
    }//w ww  . ja  va 2  s.c o m

    SendMailBatch batch = new SendMailBatch();
    batch.setBizDateTime(bizDateTime);
    batch.setMailSendHandler(mailSendHandler);
    batch.setIntervalMillis(intervalMillis);
    batch.setShutdownTrigger(shutdownTrigger);
    return batch;
}

From source file:com.postmark.PostmarkMailSender.java

@Override
public void send(SimpleMailMessage[] simpleMessages) throws MailException {
    ///FIXME default naive, non-optimal implementation (sequentially opens simpleMessages.length HTTP connections...)
    Map<Object, Exception> failedMessages = new LinkedHashMap<Object, Exception>();
    for (SimpleMailMessage simpleMessage : simpleMessages) {
        try {//from w  w w  . jav a  2  s .c om
            send(simpleMessage);
        } catch (MailException mex) {
            failedMessages.put(simpleMessage, mex);
        }
    }
    if (!failedMessages.isEmpty())
        throw new MailSendException(failedMessages);
}