I am running the equivalent of
docker run --name james_run -p "25:25" -p "465:465" -p "587:587" -p "143:143" -p "993:993" --volume "$PWD/conf:/root/conf/" --volume "$PWD/var:/root/var/" apache/james:latest
in TestContainers
james = new GenericContainer(DockerImageName.parse("apache/james")).withExposedPorts(25, 465, 587, 143, 993, 8000).withFileSystemBind(EmailServerIT.class.getClassLoader().getResource("conf").getPath(),"/root/conf", BindMode.READ_WRITE).withFileSystemBind(EmailServerIT.class.getClassLoader().getResource("var").getPath(),"/root/var", BindMode.READ_WRITE);
But am getting a connection refused error whenever I try to send to the SMTP port:
Caused by: javax.mail.MessagingException: Connection error (java.net.ConnectException: Connection refused (Connection refused))
I have set to try and avoid needing a keystore but am having no luck.
Please advise
Example Project Tree
Sample Code
pom.xml
SimpleJamesTest.java
testcontainers.properties
Notes
Docker image
I use docker image is this:
apache/james:demo-3.8.0
It will auto create mail domain, mail user.Mail Domain:
james.local
Mail user:
[email protected]
[email protected]
[email protected]
Config Exposed Ports
Run command:
docker inspect apache/james:demo-3.8.0
, find exposed ports.then use it in code:
.withExposedPorts(143,25,4000,465,587,80,8000,993)
Wait James Container Start OK
If we use command run docker
sudo docker run -it apache/james:demo-3.8.0
we will see the last message is :
We must wait container add user 3 times.
then use it in our code:
.waitingFor(Wait.forLogMessage(".*AddUser command executed sucessfully.*", 3));;
And TestContainers will mapped original smtp port 25 to some other port in host.
We need use code to get it:
Integer smtpPort = james.getMappedPort(25);
Then we can use it in our smtp send mail code.
props.put("mail.smtp.port", smtpPort );
Run Test
Run Test Command
Run Result - SMTP Debug Message