I've created a docker image of a springboot maven application. This application communicate with smtp server to send email through port 587. The app works fine locally, however when I create a docker image and run it, the application breaks when it try to access a socket to send email.
This is how my Dockerfile looks like.
FROM adoptopenjdk:11-jre-hotspot
RUN mkdir /opt/app
ADD target/camunda-fire-alarm.jar camunda-fire-alarm.jar
EXPOSE 8080 587
ENTRYPOINT ["java", "-jar", "camunda-fire-alarm.jar"]
The following are the commands I use to build and run the docker image.
build command:
docker build -f Dockerfile -t camunda-fire-alarm .
run command:
docker run -p 8080:8080 -p 587:587 camunda-fire-alarm
This is the Java code I use to send email from outlook
public static void SendFromOutlook(String from, String pass, String to, String subject, String body)
{
final String username = from; // like yourname@outlook.com
final String password = pass; // password here
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp-mail.outlook.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(String.valueOf(to))); // like inzi769@gmail.com
message.setSubject(subject);
message.setText(body);
System.out.println("Sending email to : "+to + " with message < " + body + " >");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
EDIT: Stacktrace
ENGINE-16004 Exception while closing command context: ENGINE-03051 There was an exception while invoking the TaskListener. Message: 'Exception while invoking TaskListener: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp-mail.outlook.com, 587; timeout -1;
nested exception is:
java.net.UnknownHostException: smtp-mail.outlook.com'
org.camunda.bpm.engine.ProcessEngineException: ENGINE-03051 There was an exception while invoking the TaskListener. Message: 'Exception while invoking TaskListener: com.sun.mail.util.M
ailConnectException: Couldn't connect to host, port: smtp-mail.outlook.com, 587; timeout -1;
nested exception is:
java.net.UnknownHostException: smtp-mail.outlook.com'
at org.camunda.bpm.engine.impl.db.EnginePersistenceLogger.invokeTaskListenerException(EnginePersistenceLogger.java:449) ~[camunda-engine-7.12.0.jar!/:7.12.0]
Not sure why the docker breaks the application during runtime. I would appreciate any help/guidelines.
Thanks in Advance