public String verifyMail() throws Exception { List<String> messages = smtpServer.getMessages(); int n = 0; while (n++ < 100 && messages.size() < 1) { Thread.sleep(100); } assertThat(messages).hasSize(1); return messages.get(0); }
@BeforeClass public static void setup() { // Configure the boot property to send email to the test email server. System.setProperty("spring.mail.port", Integer.toString(smtpServer.getPort())); }
@Test public void testSmtp() throws Exception { assertEquals("localhost", TestUtils.getPropertyValue(this.sendMailHandler, "mailSender.host")); Properties javaMailProperties = TestUtils.getPropertyValue(this.sendMailHandler, "mailSender.javaMailProperties", Properties.class); assertEquals("false", javaMailProperties.getProperty("mail.debug")); this.sendMailChannel.send(MessageBuilder.withPayload("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")); }
@Bean public IntegrationFlow sendMailFlow() { return IntegrationFlows.from("sendMailChannel") .enrichHeaders(Mail.headers() .subjectFunction(m -> "foo") .from("foo@bar") .toFunction(m -> new String[] { "bar@baz" })) .handle(Mail.outboundAdapter("localhost") .port(smtpServer.getPort()) .credentials("user", "pw") .protocol("smtp") .javaMailProperties(p -> p.put("mail.debug", "false")), e -> e.id("sendMailEndpoint")) .get(); }
@BeforeClass public static void setup() throws InterruptedException { int n = 0; while (n++ < 100 && (!smtpServer.isListening() || !pop3Server.isListening() || !imapServer.isListening()) || !imapIdleServer.isListening()) { Thread.sleep(100); } assertTrue(n < 100); }
@AfterClass public static void tearDown() { smtpServer.stop(); pop3Server.stop(); imapServer.stop(); imapIdleServer.stop(); }
@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")); }
@Test public void testSmtp() throws Exception { assertEquals("localhost", TestUtils.getPropertyValue(this.sendMailHandler, "mailSender.host")); Properties javaMailProperties = TestUtils.getPropertyValue(this.sendMailHandler, "mailSender.javaMailProperties", Properties.class); assertEquals("false", javaMailProperties.getProperty("mail.debug")); this.sendMailChannel.send(MessageBuilder.withPayload("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")); }
@Bean public IntegrationFlow sendMailFlow() { return IntegrationFlows.from("sendMailChannel") .enrichHeaders(Mail.headers() .subjectFunction(m -> "foo") .from("foo@bar") .toFunction(m -> new String[] { "bar@baz" })) .handle(Mail.outboundAdapter("localhost") .port(smtpServer.getPort()) .credentials("user", "pw") .protocol("smtp") .javaMailProperties(p -> p.put("mail.debug", "false")), e -> e.id("sendMailEndpoint")) .get(); }
@BeforeClass public static void setup() throws InterruptedException { int n = 0; while (n++ < 100 && (!smtpServer.isListening() || !pop3Server.isListening() || !imapServer.isListening()) || !imapIdleServer.isListening()) { Thread.sleep(100); } assertTrue(n < 100); }
@AfterClass public static void tearDown() { smtpServer.stop(); pop3Server.stop(); imapServer.stop(); imapIdleServer.stop(); }
@BeforeClass public static void setup() throws InterruptedException { int n = 0; while (n++ < 100 && (!smtpServer.isListening())) { Thread.sleep(100); } assertTrue(n < 100); }
public static SmtpServer smtp(int port) { try { return new SmtpServer(port); } catch (IOException e) { throw new RuntimeException(e); } }
@AfterClass public static void tearDown() { smtpServer.stop(); }
@Before public void beforeTest() throws IOException { smtpServer.getMessages().clear(); tearDown(); this.fileInboundChannelAdapter.start(); }