<p>We are trying to migrate Java service from java 11 and SpringBoot 2.7.1 to Java 17 and SpringBoot 3.0.6.</p>
<p>we are creating the docker image and deploying to AWS kubernates (1.24). with java 11 and  SpringBoot 2.7.1, able to trigger email successfully but after upgrade, we are getting below error with the smtp hostname.</p>
<p><strong>Note: if we give the ip address of that smtp server, code works fine with java 17 and SpringBoot 3.0.6.</strong></p>
<p>in local machine, from IDE, we were able to trigger an email with hostname as well as ipaddress from both java and SpringBoot version</p>
<pre><code>12:46:48 DEBUG SMTP: trying to connect to host &quot;company smpt host&quot;, port 25, isSSL false
12:46:48 (&quot;timestamp&quot;:&quot;07-03-2823 GMT 67:14:19.672&quot;,&quot;level&quot;: &quot;ERROR&quot;, &quot;thread&quot;:&quot;http-nio-8081-exec- 6&quot;, &quot;logger&quot;:&quot;com.emailer.service.FreeMarkerEMailSevice&quot;, &quot;message&quot;:&quot;Email sending failed with execption :
{}&quot;,&quot;context&quot;:&quot;default&quot;, &quot;exception&quot;:&quot;com.sun.mail.util.MailConnectException: Couldn't connect to host, port: &lt;company.smtpserver.com&gt;,
5000\n\tat com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2259)\n\tat
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:754)\n\tat jakarta.mail.Service.connect(Service.java:342)\n\ta org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport (JavaMailSenderImpl.java:518)\n\tat
org.springframework ail.jovanail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:437)\n\tat
org.springfra ork.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:361)\n\tat org.springframework.mail.javanail. JavaMailSenderImpl.send(JavaMailSenderImpl.java:356)\n\tat
.
..
...

java.net.UnknownHostException: &lt;company smtp host&gt;\n\tat java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:567)\n\tat java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:331)\n\tat java.base/java.net.Socket.connect(Socket.java:630)\n\tat
com.sun.mail.util.WriteTimeoutSocket.connect(WriteTimeoutSocket.java:104)\n\tat
com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:355)

</code></pre>
<p>Application.yaml</p>
<pre><code>spring.mail.host = &lt;company smtpserver&gt;
spring.mail.port= 25
spring.mail.username = xxxxx
spring.mail.password = xxxx
spring.mail.properties.mail.smtp.auth = true;
spring.mail.properties.mail.smtp.starttls.enable = true
spring.mail.properties.mail.transport.protocol = smtp
spring.mail.properties.mail.smtp.timeout = 5000
</code></pre>
<p>Java code:</p>
<pre><code>@Service
public class EmailService{

 private JavaMailSender emailSender;

 public EmailService(JavaMailSender emailSender){
   this.emailSender = emailSender;
 }
 
 @Override
 public void sendEmail(MailModel mailModel) {
     MimeMessage message = emailSender.createMimeMessage();
     try{
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message,true);
       
        Template template = emailConfig.getTemplate(&quot;email4.ftl&quot;);
        String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, mailModel.getModel());
        mimeMessageHelper.setTo(mailModel.getTo());
        mimeMessageHelper.setText(html, true);
        mimeMessageHelper.setSubject(mailModel.getSubject());
        mimeMessageHelper.setFrom(mailModel.getFrom());

        emailSender.send(mimeMessageHelper.getMimeMessage());
    }catch(Exception e){
        log.error(&quot;exception occurred {}, e.getCause());
    }
 }
}
</code></pre>
<p>We tried to debug the code with inetaddress, getting error for that aswell</p>
<pre><code>InetAddress address = null;
try{
 address = InetAddress.getByName(hostname);
} catch(UnknownHostException e){
   log.error(&quot;error occurred while trying to get ipaddress : {}, hostname);
   throw new RuntimeException(e);
}
</code></pre>
<p>error stacktrace:</p>
<pre><code>12:45:48 (&quot;timestamp&quot;:&quot;87-03-2023 GHT 07:16:35.225&quot;,&quot;level&quot;:&quot;ERROR&quot;, &quot;thread&quot;:&quot;http-nio-8881-exec-
7&quot;,&quot;logger&quot;:&quot;com.emailer.service.FreeMarkerEMailSevice&quot;, &quot;message&quot;:&quot;error occurred while trying to get ipaddress &lt;company smtp server&gt; text&quot;:&quot;default&quot;)
12:46:48 {&quot;timestamp&quot;:&quot;07-03-2023 GT 07:16:35.226&quot;,&quot;level&quot;:&quot;ERROR&quot;, &quot;thread&quot;:&quot;http-nio-8881-exec-
7&quot;,&quot;logger&quot;:&quot;org.apache.catalina.core.ContainerBase. [Tomcat].[localhost].[/].[dispatcherServlet]&quot;, &quot;message&quot;:&quot;Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.RuntimeException: java.net.UnknownHostException &lt;company smtp server&gt;: Try again) with root cause&quot;, &quot;context&quot;:&quot;default&quot;, &quot;exception&quot;:&quot;java.net.UnknownHostException: &lt;company smtp server&gt;: Try again\n\ java.base/java.net.InetAddressImpl.lookupAllHostAddr(Native Method)\n\tat
java.base/java.net.InetAddress.PlatformameService.lookupAllHostAddr(InetAddress.java:932)\n\tat
java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1517)\n\tat
java.base/java.net.InetAdd eServiceAddresses.get(InetAddress.java:851)\n\tat
java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1587)\n\tat java.base/java.net.InetAddress.getAllByName(InetAddress.java:1366)\n\t
java.base/java.net.InetAddress.getAllByName(InetAddress.java:1300)\n\tat java.base/java.net.InetAddress.getByName(InetAddress.java:1250)\n\tat

</code></pre>
<p>we tried to compile it into a jar file and run it in one of the aws vm with hostname and java 17, able to trigger an email successfully.</p>

Vikneshwar Vikki
<p>You can use <code>Session</code> class and use <code>Transport.send()</code>. Below is my code for reference :</p>
<p>EmailConfig :</p>
<pre><code>@Configuration
public class EmailConfig {

    @Value(&quot;${spring.mail.username}&quot;)
    private String noReplyEmailId;

    @Value(&quot;${spring.mail.password}&quot;)
    private String noReplyEmailPassword;

    @Bean
    public Session getSession() {
        Properties props = new Properties();
        props.put(&quot;mail.smtp.starttls.enable&quot;, &quot;true&quot;);
        props.put(&quot;mail.smtp.host&quot;, &quot;smtp.gmail.com&quot;);
        props.put(&quot;mail.smtp.port&quot;, &quot;587&quot;);
        props.put(&quot;mail.smtp.auth&quot;, &quot;true&quot;);
        props.put(&quot;mail.smtp.starttls.required&quot;, &quot;true&quot;);
        props.put(&quot;mail.smtp.ssl.protocols&quot;, &quot;TLSv1.2&quot;);
        return Session.getInstance(props, new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(noReplyEmailId, noReplyEmailPassword);
            }
        });
    }
}
</code></pre>
<p>EmailService :</p>
<pre><code>@Autowired
Session session;


public void sendVerifyEmail() throws MessagingException {
    try {
    MimeMessage message = new MimeMessage(session);
    MimeMessageHelper helper = new MimeMessageHelper(message, 
    MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED, 
    StandardCharsets.UTF_8.name());
    helper.setFrom();
    helper.setTo();
    helper.setSubject();
    helper.setText();
    Transport.send(message);
    }catch (Exception e) {
         System.out.println(&quot;Problem occurred while sending email&quot; + e);
       }
    }
</code></pre>
