Search in sources :

Example 1 with MailSendException

use of org.springframework.mail.MailSendException in project spring-framework by spring-projects.

the class JavaMailSenderTests method failedMailServerConnect.

@Test
public void failedMailServerConnect() throws Exception {
    MockJavaMailSender sender = new MockJavaMailSender();
    sender.setHost(null);
    sender.setUsername("username");
    sender.setPassword("password");
    SimpleMailMessage simpleMessage1 = new SimpleMailMessage();
    try {
        sender.send(simpleMessage1);
        fail("Should have thrown MailSendException");
    } catch (MailSendException ex) {
        // expected
        ex.printStackTrace();
        assertTrue(ex.getFailedMessages() != null);
        assertEquals(1, ex.getFailedMessages().size());
        assertSame(simpleMessage1, ex.getFailedMessages().keySet().iterator().next());
        assertSame(ex.getCause(), ex.getFailedMessages().values().iterator().next());
    }
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MailSendException(org.springframework.mail.MailSendException) Test(org.junit.Test)

Example 2 with MailSendException

use of org.springframework.mail.MailSendException in project spring-framework by spring-projects.

the class JavaMailSenderImpl method doSend.

/**
	 * Actually send the given array of MimeMessages via JavaMail.
	 * @param mimeMessages MimeMessage objects to send
	 * @param originalMessages corresponding original message objects
	 * that the MimeMessages have been created from (with same array
	 * length and indices as the "mimeMessages" array), if any
	 * @throws org.springframework.mail.MailAuthenticationException
	 * in case of authentication failure
	 * @throws org.springframework.mail.MailSendException
	 * in case of failure when sending a message
	 */
protected void doSend(MimeMessage[] mimeMessages, Object[] originalMessages) throws MailException {
    Map<Object, Exception> failedMessages = new LinkedHashMap<>();
    Transport transport = null;
    try {
        for (int i = 0; i < mimeMessages.length; i++) {
            // Check transport connection first...
            if (transport == null || !transport.isConnected()) {
                if (transport != null) {
                    try {
                        transport.close();
                    } catch (Exception ex) {
                    // Ignore - we're reconnecting anyway
                    }
                    transport = null;
                }
                try {
                    transport = connectTransport();
                } catch (AuthenticationFailedException ex) {
                    throw new MailAuthenticationException(ex);
                } catch (Exception ex) {
                    // Effectively, all remaining messages failed...
                    for (int j = i; j < mimeMessages.length; j++) {
                        Object original = (originalMessages != null ? originalMessages[j] : mimeMessages[j]);
                        failedMessages.put(original, ex);
                    }
                    throw new MailSendException("Mail server connection failed", ex, failedMessages);
                }
            }
            // Send message via current transport...
            MimeMessage mimeMessage = mimeMessages[i];
            try {
                if (mimeMessage.getSentDate() == null) {
                    mimeMessage.setSentDate(new Date());
                }
                String messageId = mimeMessage.getMessageID();
                mimeMessage.saveChanges();
                if (messageId != null) {
                    // Preserve explicitly specified message id...
                    mimeMessage.setHeader(HEADER_MESSAGE_ID, messageId);
                }
                transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
            } catch (Exception ex) {
                Object original = (originalMessages != null ? originalMessages[i] : mimeMessage);
                failedMessages.put(original, ex);
            }
        }
    } finally {
        try {
            if (transport != null) {
                transport.close();
            }
        } catch (Exception ex) {
            if (!failedMessages.isEmpty()) {
                throw new MailSendException("Failed to close server connection after message failures", ex, failedMessages);
            } else {
                throw new MailSendException("Failed to close server connection after message sending", ex);
            }
        }
    }
    if (!failedMessages.isEmpty()) {
        throw new MailSendException(failedMessages);
    }
}
Also used : MailAuthenticationException(org.springframework.mail.MailAuthenticationException) MailSendException(org.springframework.mail.MailSendException) AuthenticationFailedException(javax.mail.AuthenticationFailedException) MimeMessage(javax.mail.internet.MimeMessage) Transport(javax.mail.Transport) MailParseException(org.springframework.mail.MailParseException) MessagingException(javax.mail.MessagingException) MailAuthenticationException(org.springframework.mail.MailAuthenticationException) MailSendException(org.springframework.mail.MailSendException) AuthenticationFailedException(javax.mail.AuthenticationFailedException) NoSuchProviderException(javax.mail.NoSuchProviderException) MailPreparationException(org.springframework.mail.MailPreparationException) MailException(org.springframework.mail.MailException) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with MailSendException

use of org.springframework.mail.MailSendException in project spring-framework by spring-projects.

the class JavaMailSenderTests method failedMailServerClose.

@Test
public void failedMailServerClose() throws Exception {
    MockJavaMailSender sender = new MockJavaMailSender();
    sender.setHost("");
    sender.setUsername("username");
    sender.setPassword("password");
    SimpleMailMessage simpleMessage1 = new SimpleMailMessage();
    try {
        sender.send(simpleMessage1);
        fail("Should have thrown MailSendException");
    } catch (MailSendException ex) {
        // expected
        ex.printStackTrace();
        assertTrue(ex.getFailedMessages() != null);
        assertEquals(0, ex.getFailedMessages().size());
    }
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MailSendException(org.springframework.mail.MailSendException) Test(org.junit.Test)

Example 4 with MailSendException

use of org.springframework.mail.MailSendException in project spring-framework by spring-projects.

the class JavaMailSenderTests method failedSimpleMessage.

@Test
public void failedSimpleMessage() throws Exception {
    MockJavaMailSender sender = new MockJavaMailSender();
    sender.setHost("host");
    sender.setUsername("username");
    sender.setPassword("password");
    SimpleMailMessage simpleMessage1 = new SimpleMailMessage();
    simpleMessage1.setTo("he@mail.org");
    simpleMessage1.setSubject("fail");
    SimpleMailMessage simpleMessage2 = new SimpleMailMessage();
    simpleMessage2.setTo("she@mail.org");
    try {
        sender.send(simpleMessage1, simpleMessage2);
    } catch (MailSendException ex) {
        ex.printStackTrace();
        assertEquals("host", sender.transport.getConnectedHost());
        assertEquals("username", sender.transport.getConnectedUsername());
        assertEquals("password", sender.transport.getConnectedPassword());
        assertTrue(sender.transport.isCloseCalled());
        assertEquals(1, sender.transport.getSentMessages().size());
        assertEquals(new InternetAddress("she@mail.org"), sender.transport.getSentMessage(0).getAllRecipients()[0]);
        assertEquals(1, ex.getFailedMessages().size());
        assertEquals(simpleMessage1, ex.getFailedMessages().keySet().iterator().next());
        Object subEx = ex.getFailedMessages().values().iterator().next();
        assertTrue(subEx instanceof MessagingException);
        assertEquals("failed", ((MessagingException) subEx).getMessage());
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MailSendException(org.springframework.mail.MailSendException) MessagingException(javax.mail.MessagingException) Test(org.junit.Test)

Example 5 with MailSendException

use of org.springframework.mail.MailSendException in project spring-framework by spring-projects.

the class JavaMailSenderTests method fFailedMimeMessage.

@Test
public void fFailedMimeMessage() throws Exception {
    MockJavaMailSender sender = new MockJavaMailSender();
    sender.setHost("host");
    sender.setUsername("username");
    sender.setPassword("password");
    MimeMessage mimeMessage1 = sender.createMimeMessage();
    mimeMessage1.setRecipient(Message.RecipientType.TO, new InternetAddress("he@mail.org"));
    mimeMessage1.setSubject("fail");
    MimeMessage mimeMessage2 = sender.createMimeMessage();
    mimeMessage2.setRecipient(Message.RecipientType.TO, new InternetAddress("she@mail.org"));
    try {
        sender.send(mimeMessage1, mimeMessage2);
    } catch (MailSendException ex) {
        ex.printStackTrace();
        assertEquals("host", sender.transport.getConnectedHost());
        assertEquals("username", sender.transport.getConnectedUsername());
        assertEquals("password", sender.transport.getConnectedPassword());
        assertTrue(sender.transport.isCloseCalled());
        assertEquals(1, sender.transport.getSentMessages().size());
        assertEquals(mimeMessage2, sender.transport.getSentMessage(0));
        assertEquals(1, ex.getFailedMessages().size());
        assertEquals(mimeMessage1, ex.getFailedMessages().keySet().iterator().next());
        Object subEx = ex.getFailedMessages().values().iterator().next();
        assertTrue(subEx instanceof MessagingException);
        assertEquals("failed", ((MessagingException) subEx).getMessage());
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MailSendException(org.springframework.mail.MailSendException) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Test(org.junit.Test)

Aggregations

MailSendException (org.springframework.mail.MailSendException)5 Test (org.junit.Test)4 MessagingException (javax.mail.MessagingException)3 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)3 InternetAddress (javax.mail.internet.InternetAddress)2 MimeMessage (javax.mail.internet.MimeMessage)2 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 AuthenticationFailedException (javax.mail.AuthenticationFailedException)1 NoSuchProviderException (javax.mail.NoSuchProviderException)1 Transport (javax.mail.Transport)1 MailAuthenticationException (org.springframework.mail.MailAuthenticationException)1 MailException (org.springframework.mail.MailException)1 MailParseException (org.springframework.mail.MailParseException)1 MailPreparationException (org.springframework.mail.MailPreparationException)1