/** * Wraps the input exception with a runtime {@link MailException}. The * exception message will contain the failed message (using toString). * * @param message a failed message * @param exception a MessagingException * @throws MailException a translation of the Exception * @see MailErrorHandler#handle(MailMessage, Exception) */ @Override public void handle(MailMessage message, Exception exception) throws MailException { String msg = message.toString(); throw new MailSendException("Mail server send failed: " + msg.substring(0, Math.min(maxMessageLength, msg.length())), exception); } }
@Test public void failedMailServerConnect() { 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()); } }
@Test public void failedMailServerClose() { 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()); } }
/** * @param items the items to send * @see ItemWriter#write(List) */ @Override public void write(List<? extends SimpleMailMessage> items) throws MailException { try { mailSender.send(items.toArray(new SimpleMailMessage[items.size()])); } catch (MailSendException e) { Map<Object, Exception> failedMessages = e.getFailedMessages(); for (Entry<Object, Exception> entry : failedMessages.entrySet()) { mailErrorHandler.handle((SimpleMailMessage) entry.getKey(), entry.getValue()); } } }
logger.warn(e.getMessage(), e); } catch(MailSendException e){ logger.warn(e.getMessage(), e);
@Test public void failedMimeMessage() throws MessagingException { 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()); } }
/** * @param items the items to send * @see ItemWriter#write(List) */ @Override public void write(List<? extends MimeMessage> items) throws MailException { try { mailSender.send(items.toArray(new MimeMessage[items.size()])); } catch (MailSendException e) { Map<Object, Exception> failedMessages = e.getFailedMessages(); for (Entry<Object, Exception> entry : failedMessages.entrySet()) { mailErrorHandler.handle(new MimeMailMessage((MimeMessage)entry.getKey()), entry.getValue()); } } }
@Override public void send(SimpleMailMessage... simpleMessages) throws MailException { Map<Object, Exception> failedMessages = new LinkedHashMap<>(); for (SimpleMailMessage simpleMessage : simpleMessages) { if (subjectsToFail.contains(simpleMessage.getSubject())) { failedMessages.put(simpleMessage, new MessagingException()); } else { received.add(simpleMessage); } } if (!failedMessages.isEmpty()) { throw new MailSendException(failedMessages); } }
@Test public void failedSimpleMessage() throws MessagingException { 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()); } }
/** * @param items the items to send * @see ItemWriter#write(List) */ @Override public void write(List<? extends SimpleMailMessage> items) throws MailException { try { mailSender.send(items.toArray(new SimpleMailMessage[items.size()])); } catch (MailSendException e) { Map<Object, Exception> failedMessages = e.getFailedMessages(); for (Entry<Object, Exception> entry : failedMessages.entrySet()) { mailErrorHandler.handle((SimpleMailMessage) entry.getKey(), entry.getValue()); } } }
failedMessages.put(original, ex); throw new MailSendException("Mail server connection failed", ex, failedMessages); throw new MailSendException("Failed to close server connection after message failures", ex, failedMessages); throw new MailSendException("Failed to close server connection after message sending", ex); throw new MailSendException(failedMessages);
/** * @param items the items to send * @see ItemWriter#write(List) */ @Override public void write(List<? extends MimeMessage> items) throws MailException { try { mailSender.send(items.toArray(new MimeMessage[items.size()])); } catch (MailSendException e) { Map<Object, Exception> failedMessages = e.getFailedMessages(); for (Entry<Object, Exception> entry : failedMessages.entrySet()) { mailErrorHandler.handle(new MimeMailMessage((MimeMessage)entry.getKey()), entry.getValue()); } } }
failedMessages.put(original, ex); throw new MailSendException("Mail server connection failed", ex, failedMessages); throw new MailSendException("Failed to close server connection after message failures", ex, failedMessages); throw new MailSendException("Failed to close server connection after message sending", ex); throw new MailSendException(failedMessages);
@Test(expected = MailSendException.class) public void testDefaultErrorHandler() throws Exception { MimeMessage foo = new MimeMessage(session); MimeMessage bar = new MimeMessage(session); MimeMessage[] items = new MimeMessage[] { foo, bar }; // Spring 4.1 changed the send method to be vargs instead of an array if(ReflectionUtils.findMethod(MailSender.class, "send", MimeMessage[].class) != null) { mailSender.send(aryEq(items)); } else { mailSender.send(items); } when(mailSender).thenThrow(new MailSendException(Collections.singletonMap((Object)foo, (Exception)new MessagingException("FOO")))); writer.write(Arrays.asList(items)); }
@Test(expected = MailSendException.class) public void testErrorHandler() throws Exception { SimpleMailMessageItemWriter writer = new SimpleMailMessageItemWriterBuilder().mailSender(this.mailSender) .build(); this.mailSender.send(this.foo, this.bar); when(this.mailSender) .thenThrow(new MailSendException(Collections.singletonMap(this.foo, new MessagingException("FOO")))); writer.write(Arrays.asList(this.items)); }
@Test public void testCustomErrorHandler() throws Exception { final AtomicReference<String> content = new AtomicReference<>(); writer.setMailErrorHandler(new MailErrorHandler() { @Override public void handle(MailMessage message, Exception exception) throws MailException { content.set(exception.getMessage()); } }); MimeMessage foo = new MimeMessage(session); MimeMessage bar = new MimeMessage(session); MimeMessage[] items = new MimeMessage[] { foo, bar }; // Spring 4.1 changed the send method to be vargs instead of an array if(ReflectionUtils.findMethod(MailSender.class, "send", MimeMessage[].class) != null) { mailSender.send(aryEq(items)); } else { mailSender.send(items); } when(mailSender).thenThrow(new MailSendException(Collections.singletonMap((Object)foo, (Exception)new MessagingException("FOO")))); writer.write(Arrays.asList(items)); assertEquals("FOO", content.get()); }
@Test(expected = MailSendException.class) public void testDefaultErrorHandler() throws Exception { SimpleMailMessage foo = new SimpleMailMessage(); SimpleMailMessage bar = new SimpleMailMessage(); SimpleMailMessage[] items = new SimpleMailMessage[] { foo, bar }; // Spring 4.1 changed the send method to be vargs instead of an array if(ReflectionUtils.findMethod(SimpleMailMessage.class, "send", SimpleMailMessage[].class) != null) { mailSender.send(aryEq(items)); } else { mailSender.send(items); } when(mailSender).thenThrow(new MailSendException(Collections.singletonMap((Object)foo, (Exception)new MessagingException("FOO")))); writer.write(Arrays.asList(items)); }
@Test public void testCustomErrorHandler() throws Exception { final AtomicReference<String> content = new AtomicReference<>(); SimpleMailMessageItemWriter writer = new SimpleMailMessageItemWriterBuilder() .mailErrorHandler(new MailErrorHandler() { @Override public void handle(MailMessage message, Exception exception) throws MailException { content.set(exception.getMessage()); } }).mailSender(this.mailSender).build(); this.mailSender.send(this.foo, this.bar); when(this.mailSender) .thenThrow(new MailSendException(Collections.singletonMap(this.foo, new MessagingException("FOO")))); writer.write(Arrays.asList(this.items)); assertEquals("FOO", content.get()); }
@Test public void testCustomErrorHandler() throws Exception { final AtomicReference<String> content = new AtomicReference<>(); writer.setMailErrorHandler(new MailErrorHandler() { @Override public void handle(MailMessage message, Exception exception) throws MailException { content.set(exception.getMessage()); } }); SimpleMailMessage foo = new SimpleMailMessage(); SimpleMailMessage bar = new SimpleMailMessage(); SimpleMailMessage[] items = new SimpleMailMessage[] { foo, bar }; // Spring 4.1 changed the send method to be vargs instead of an array if(ReflectionUtils.findMethod(SimpleMailMessage.class, "send", SimpleMailMessage[].class) != null) { mailSender.send(aryEq(items)); } else { mailSender.send(items); } when(mailSender).thenThrow(new MailSendException(Collections.singletonMap((Object)foo, (Exception)new MessagingException("FOO")))); writer.write(Arrays.asList(items)); assertEquals("FOO", content.get()); }
Mockito.doNothing().doThrow(new MailSendException("Test message")).when(this.mailSenderMock).send(Mockito.any(SimpleMailMessage.class));`