Search in sources :

Example 1 with MailSendingMessageHandler

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));
}
Also used : StringWriter(java.io.StringWriter) MimeMessage(javax.mail.internet.MimeMessage) FileReader(java.io.FileReader) MailSendingMessageHandler(org.springframework.integration.mail.MailSendingMessageHandler) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) Test(org.junit.Test)

Example 2 with MailSendingMessageHandler

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();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MailSender(org.springframework.mail.MailSender) MailSendingMessageHandler(org.springframework.integration.mail.MailSendingMessageHandler) Properties(java.util.Properties) Test(org.junit.Test)

Example 3 with MailSendingMessageHandler

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();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MailSender(org.springframework.mail.MailSender) MailSendingMessageHandler(org.springframework.integration.mail.MailSendingMessageHandler) Test(org.junit.Test)

Example 4 with MailSendingMessageHandler

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();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MailSender(org.springframework.mail.MailSender) MailSendingMessageHandler(org.springframework.integration.mail.MailSendingMessageHandler) Test(org.junit.Test)

Example 5 with MailSendingMessageHandler

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"));
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) MailSendingMessageHandler(org.springframework.integration.mail.MailSendingMessageHandler) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 MailSendingMessageHandler (org.springframework.integration.mail.MailSendingMessageHandler)5 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)3 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)3 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)3 MailSender (org.springframework.mail.MailSender)3 FileReader (java.io.FileReader)1 StringWriter (java.io.StringWriter)1 Properties (java.util.Properties)1 MimeMessage (javax.mail.internet.MimeMessage)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)1 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)1