MailClient extends AbstractEndpoint implements Producer, InitializingBean { @Override public void send(Message message, TestContext context) { if (log.isDebugEnabled()) { log.debug(String.format("Sending mail message to host: '%s: } MimeMailMessage mimeMessage = getEndpointConfiguration().getMessageConverter().convertOutbound(message, getEndpointConfiguration(), context); getEndpointConfiguration().getJavaMailSender().send(mimeMessage.getMimeMessage()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); Message mailMessage; try { mimeMessage.getMimeMessage().writeTo(bos); mailMessage = new RawMessage(bos.toString()); } catch (IOException e) { mailMessage = message; } catch (MessagingException e) { mailMessage = message; } finally { try { bos.close(); } catch (IOException e) { log.warn("Failed to close output stream", e); } } context.onOutboundMessage(mailMessage); log.info(String.format("Mail message was sent to host: '%s: } MailClient();  MailClient(MailEndpointConfiguration endpointConfiguration); @Override MailEndpointConfiguration getEndpointConfiguration(); @Override void send(Message message, TestContext context); @Override Producer createProducer(); @Override Consumer createConsumer(); @Override void afterPropertiesSet();  }
@Test public void testSendMailMessageObject() throws Exception { MailRequest mailRequest = (MailRequest) new MailMarshaller().unmarshal(new StreamSource( new ClassPathResource("text_mail.xml", MailServer.class).getInputStream())); reset(javaMailSender); when(javaMailSender.createMimeMessage()).thenReturn(new MimeMessage(Session.getDefaultInstance(new Properties()))); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { MimeMessage mimeMessage = (MimeMessage) invocation.getArguments()[0]; Assert.assertEquals(getAddresses(mimeMessage.getFrom()), "foo@mail.com"); Assert.assertEquals(getAddresses(mimeMessage.getRecipients(Message.RecipientType.TO)), "bar@mail.com,copy@mail.com"); Assert.assertEquals(getAddresses(mimeMessage.getRecipients(Message.RecipientType.CC)), "foobar@mail.com"); Assert.assertEquals(getAddresses(mimeMessage.getRecipients(Message.RecipientType.BCC)), "secret@mail.com"); Assert.assertEquals(getAddresses(mimeMessage.getReplyTo()), "foo@mail.com"); Assert.assertNotNull(mimeMessage.getSentDate()); Assert.assertEquals(mimeMessage.getSubject(), "Testmail"); Assert.assertEquals(mimeMessage.getContentType(), "text/plain"); Assert.assertEquals(mimeMessage.getContent().toString(), "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua."); return null; } }).when(javaMailSender).send(any(MimeMessage.class)); mailClient.send(new DefaultMessage(mailRequest), context); }
@Test public void testSendMultipartMailMessageObject() throws Exception { MailRequest mailRequest = (MailRequest) new MailMarshaller().unmarshal(new StreamSource( new ClassPathResource("multipart_mail.xml", MailServer.class).getInputStream())); reset(javaMailSender); when(javaMailSender.createMimeMessage()).thenReturn(new MimeMessage(Session.getDefaultInstance(new Properties()))); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { MimeMessage mimeMessage = (MimeMessage) invocation.getArguments()[0]; Assert.assertEquals(getAddresses(mimeMessage.getFrom()), "foo@mail.com"); Assert.assertEquals(getAddresses(mimeMessage.getRecipients(Message.RecipientType.TO)), "bar@mail.com"); Assert.assertEquals(getAddresses(mimeMessage.getReplyTo()), "foo@mail.com"); Assert.assertNotNull(mimeMessage.getSentDate()); Assert.assertEquals(mimeMessage.getSubject(), "Multipart Testmail"); Assert.assertEquals(mimeMessage.getContentType(), "text/plain"); Assert.assertEquals(mimeMessage.getContent().getClass(), MimeMultipart.class); MimeMultipart multipart = (MimeMultipart) mimeMessage.getContent(); Assert.assertEquals(multipart.getCount(), 2L); Assert.assertTrue(multipart.getContentType().startsWith("multipart/mixed")); Assert.assertTrue(((MimeMultipart) multipart.getBodyPart(0).getContent()).getContentType().startsWith("multipart/related")); Assert.assertEquals(((MimeMultipart) multipart.getBodyPart(0).getContent()).getCount(), 1L); Assert.assertEquals(((MimeMultipart) multipart.getBodyPart(0).getContent()).getBodyPart(0).getContent().toString(), "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua."); Assert.assertEquals(((MimeMultipart) multipart.getBodyPart(0).getContent()).getBodyPart(0).getContentType(), "text/plain"); Assert.assertEquals(StringUtils.trimAllWhitespace(multipart.getBodyPart(1).getContent().toString()), "<html><head></head><body><h1>HTMLAttachment</h1></body></html>"); Assert.assertEquals(multipart.getBodyPart(1).getFileName(), "index.html"); Assert.assertEquals(multipart.getBodyPart(1).getDisposition(), "attachment"); return null; } }).when(javaMailSender).send(any(MimeMessage.class)); mailClient.send(new DefaultMessage(mailRequest), context); }