sending mail using external smtp from container

I prepared an app that can send mail through smtp.gmail.com and it works well but, when I run it using docker, I get an error – as it can’t connect to smtp.gmail.com. The function sending mails is:

public void sendEmal(String body, String mailRecipient){
   Properties props = new Properties();
   props.put("mail.transport.protocol", "smtp");
   props.put("mail.smtp.auth", "true");
   props.put("mail.smtp.host", "smtp.gmail.com");
   props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
   props.put("mail.smtp.port", "465");
   try{
        Authenticator auth = new SMTPAuthenticator();
        Session mailSession = Session.getDefaultInstance(props, auth);
        mailSession.setDebug(true);
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);
        message.setContent(body, "text/plain");
        message.addRecipient(Message.RecipientType.TO,
        new InternetAddress("mailRecipient"));

        transport.connect();
        transport.sendMessage(message,
        message.getRecipients(Message.RecipientType.TO));
        transport.close();  
    }
    catch(Exception e){
        System.out.println(e);
    }
}

This is the error:

  • publish a web api based on asp.net core RC2 on docker
  • docker cp to container: file name too long
  • ADD command failing while building dockerfile using docker-build-step plugin in jenkins
  • Slow docker push since Version 17.03.0-ce-mac2
  • Issue when I try to create a docker image [duplicate]
  • Docker: save - produces no output
  • myapp_thirdInst | DEBUG SMTP: trying to connect to host
    “smtp.gmail.com”, port 465, isSSL false

    myapp_thirdInst | javax.mail.MessagingException: Unknown SMTP host:
    smtp.gmail.com;

    myapp_thirdInst | nested exception is:

    myapp_thirdInst | java.net.UnknownHostException: smtp.gmail.com

    myapp_thirdInst | error sending mail

    It’s the first time I’m playing with docker and after some tries, it continues to fail. I’m kind of lost – could anyone give me a hint to solve this issue?

  • Getting list of running containers from a cluster
  • Docker Weave and WeaveDNS issues
  • Unable to ssh localhost within a running Docker container
  • Creating a cassandra docker image with initialized KEYSPACE and schema
  • NoHostAvailableException in DBeaver when connecting to remote Cassandra docker container
  • docker - NGINX Proxy + Let's encrypt
  • One Solution collect form web for “sending mail using external smtp from container”

    The first thing you need to do is get DNS working! Test the container DNS first with a shell, get that working and then your SMTP code may work.

    Perhaps you can update your question with information about your DNS configuration

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.