eclipse-ee4j / glassfish

Eclipse GlassFish
https://eclipse-ee4j.github.io/glassfish/
377 stars 143 forks source link

Got an exception "Could not convert socket to TLS" when sending an email #24669

Open hhallenyang opened 10 months ago

hhallenyang commented 10 months ago

The Glassfish Server generate an exception when the system try to send email out.

Environment Details


Problem Description

I got the "Could not convert socket to TLS" exception when I try to send an email out. I could send it out successfully with the same code and configuration from Glassfish 4 installed in the same computer.

This problem is easily reproducible.

expected resource not found: /META-INF/javamail.default.address.map|#] jakarta.mail.MessagingException: Could not convert socket to TLS; nested exception is: javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target|#]

Steps to reproduce

Start the Glassfish server, configure the email connection, enter the email content and send the email out.

The same method as shown below is called from Glassfish 4 and Glassfish 7. Glassfish 4 sent the email successfully but Glassfish 7 got the exception above.

public void sendOneEmail(String toAddr, String emailContent) {
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");

    Session emailSession = Session.getInstance(props, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("MY EMAIL ADDRESS", "MY PASSWORD");
        }});

    try {
        MimeMessage message = new MimeMessage(emailSession);
        message.setFrom(new InternetAddress("MY EMAIL ADDRESS", "Tester"));
        message.setSubject("Testing");
        message.setContent(emailContent, "text/plain; charset=UTF-8");
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddr.replaceAll("\\s+", "")));
        try {
            Transport.send(message);
            Thread.sleep(2000L);
        } catch (MessagingException e) {
            System.out.println(e.toString());
        }
    } catch (MessagingException | UnsupportedEncodingException | InterruptedException e) {
    }
}

Glassfish 4: Java EE Web API Library Glassfish 7: includes

jakarta.mail jakarta.mail-api 2.1.1 provided

Impact of Issue

Sending email is very important for the project.

High

OndroMih commented 10 months ago

This is probably because in GlassFish 7 you don’t have the certificate from smtp.google.com in the trust store file cacerts.jks. You need to download it and add it there. Or copy it from the same file in GlassFish 4, where you probably installed it in the past.