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());
}
}
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);
}
}
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());
}
}
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());
}
}
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());
}
}
Aggregations