use of org.springframework.integration.mail.MailSendingMessageHandler in project spring-integration by spring-projects.
the class MessageWithContentTypeTests method testMessageConversionWithHtmlAndContentType.
@Test
public void testMessageConversionWithHtmlAndContentType() throws Exception {
JavaMailSender sender = mock(JavaMailSender.class);
MailSendingMessageHandler handler = new MailSendingMessageHandler(sender);
StringWriter writer = new StringWriter();
FileReader reader = new FileReader("src/test/java/org/springframework/integration/mail/config/test.html");
FileCopyUtils.copy(reader, writer);
Message<String> message = MessageBuilder.withPayload(writer.getBuffer().toString()).setHeader(MailHeaders.TO, "to").setHeader(MailHeaders.FROM, "from").setHeader(MailHeaders.CONTENT_TYPE, "text/html").build();
MimeMessage mMessage = new TestMimeMessage();
// MOCKS
when(sender.createMimeMessage()).thenReturn(mMessage);
doAnswer(invocation -> {
MimeMessage mimeMessage = invocation.getArgument(0);
assertEquals("text/html", mimeMessage.getDataHandler().getContentType());
return null;
}).when(sender).send(Mockito.any(MimeMessage.class));
// handle message
handler.handleMessage(message);
verify(sender, times(1)).send(Mockito.any(MimeMessage.class));
}
use of org.springframework.integration.mail.MailSendingMessageHandler in project spring-integration by spring-projects.
the class MailOutboundChannelAdapterParserTests method adapterWithJavaMailProperties.
@Test
public void adapterWithJavaMailProperties() {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("MailOutboundWithJavamailProperties-context.xml", this.getClass());
Object adapter = context.getBean("adapterWithHostProperty.adapter");
MailSendingMessageHandler handler = (MailSendingMessageHandler) new DirectFieldAccessor(adapter).getPropertyValue("handler");
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(handler);
MailSender mailSender = (MailSender) fieldAccessor.getPropertyValue("mailSender");
assertNotNull(mailSender);
Properties javaMailProperties = (Properties) TestUtils.getPropertyValue(mailSender, "javaMailProperties");
assertEquals(7, javaMailProperties.size());
assertNotNull(javaMailProperties);
assertEquals("true", javaMailProperties.get("mail.smtps.auth"));
context.close();
}
use of org.springframework.integration.mail.MailSendingMessageHandler in project spring-integration by spring-projects.
the class MailOutboundChannelAdapterParserTests method adapterWithHostProperty.
@Test
public void adapterWithHostProperty() {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("mailOutboundChannelAdapterParserTests.xml", this.getClass());
Object adapter = context.getBean("adapterWithHostProperty.adapter");
MailSendingMessageHandler handler = (MailSendingMessageHandler) new DirectFieldAccessor(adapter).getPropertyValue("handler");
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(handler);
MailSender mailSender = (MailSender) fieldAccessor.getPropertyValue("mailSender");
assertNotNull(mailSender);
context.close();
}
use of org.springframework.integration.mail.MailSendingMessageHandler in project spring-integration by spring-projects.
the class MailOutboundChannelAdapterParserTests method adapterWithMailSenderReference.
@Test
public void adapterWithMailSenderReference() {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("mailOutboundChannelAdapterParserTests.xml", this.getClass());
Object adapter = context.getBean("adapterWithMailSenderReference.adapter");
MailSendingMessageHandler handler = (MailSendingMessageHandler) new DirectFieldAccessor(adapter).getPropertyValue("handler");
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(handler);
MailSender mailSender = (MailSender) fieldAccessor.getPropertyValue("mailSender");
assertNotNull(mailSender);
assertEquals(23, fieldAccessor.getPropertyValue("order"));
context.close();
}
use of org.springframework.integration.mail.MailSendingMessageHandler in project spring-integration by spring-projects.
the class SmtpTests method testSmtp.
@Test
public void testSmtp() throws Exception {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("localhost");
mailSender.setPort(smtpServer.getPort());
mailSender.setUsername("user");
mailSender.setPassword("pw");
MailSendingMessageHandler handler = new MailSendingMessageHandler(mailSender);
handler.handleMessage(MessageBuilder.withPayload("foo").setHeader(MailHeaders.TO, new String[] { "bar@baz" }).setHeader(MailHeaders.FROM, "foo@bar").setHeader(MailHeaders.SUBJECT, "foo").build());
int n = 0;
while (n++ < 100 && smtpServer.getMessages().size() == 0) {
Thread.sleep(100);
}
assertTrue(smtpServer.getMessages().size() > 0);
String message = smtpServer.getMessages().get(0);
assertThat(message, endsWith("foo\n"));
assertThat(message, containsString("foo@bar"));
assertThat(message, containsString("bar@baz"));
assertThat(message, containsString("user:user"));
assertThat(message, containsString("password:pw"));
}
Aggregations