Spring JavaMailSender and Testcontainers Mailpit doesn't work together

1 day ago 1
ARTICLE AD BOX

For testing I have a Mailpit Testcontainers instance running:

@Container static MailpitContainer mailpitContainer = new MailpitContainer() .withNetwork(TESTCONTAINERS_NETWORK);

The service under test it itself deployed in a Testcontainers instance:

public class MyServiceUnderTestContainer extends GenericContainer<ArgusBatchContainer> private static final String IMAGE_NAME... static { Testcontainers.exposeHostPorts(1025); } public MyServiceUnderTestContainer(Map<String, String> environmentVariables) { super(IMAGE_NAME); this.withAccessToHost(true); this.withNetwork(TESTCONTAINERS_NETWORK); } }

My Spring JavaMailSender is configured like this:

@Bean public JavaMailSenderImpl createJavaMailSenderBean() { final JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setHost("host.testcontainers.internal"); mailSender.setPort(1025); mailSender.setUsername(""); mailSender.setPassword(null); mailSender.setProtocol("smtp"); mailSender.setJavaMailProperties(createJavaMailProperties()); return mailSender; } private Properties createJavaMailProperties() { final Properties properties = new Properties(); properties.put("mail.smtp.auth", false); properties.put("mail.smtp.starttls.enable", false); properties.put("mail.smtp.connectiontimeout", 180000); properties.put("mail.smtp.timeout", 180000); return properties; }

I get the following error message:

org.springframework.mail.MailSendException: Mail server connection failed. Failed messages: jakarta.mail.MessagingException: Got bad greeting from SMTP host: host.testcontainers.internal, port: 1025, response: [EOF]

Does anyone have an idea why this comes?

Read Entire Article