简体   繁体   中英

Unable to send mail: Could not convert socket to TLS;

The following code is working as Java application eclipse, But when it deployed in SAP PO server the below exception is thrown:

Note : Stand alone java file is running on my laptop(Windows) where as server is not Linux

javax.mail.MessagingException: Could not convert socket to TLS; nested exception is: java.net.SocketException: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: com.sun.net.ssl.internal.ssl.DefaultSSLContextImpl)

public static void main(String[] args) {
        PropertiesSerDto dto = new PropertiesSerDto();

        dto.setEmailAuth("true");
        dto.setEmailStarttlsEnable("true");
        dto.setEmailHost("outlook.office365.com");
        dto.setEmailPort("587");
        dto.setEmailPassword("1234");
        dto.setEmailFrom("test@mail.com");
        dto.setEmailUser("test@mail.com");
        String to="to@gmail.com";
        String subject="Mail Testing";
        String message = "Hello";
        try {
            sendHtmlEmail(dto,to,subject,message);
        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void sendHtmlEmail(PropertiesSerDto dto, String to, String subject, String message) throws AddressException, MessagingException {

        // sets SMTP server properties
        Properties properties = new Properties();
        properties.put("mail.smtp.host", dto.getEmailHost());
        properties.put("mail.smtp.port", dto.getEmailPort());
        properties.put("mail.smtp.auth", dto.getEmailAuth());
        properties.put("mail.smtp.starttls.enable", dto.getEmailStarttlsEnable());
        final String user = dto.getEmailUser();
        final String password = dto.getEmailPassword();
        // creates a new session with an authenticator
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, password);
            }
        };
        Session session = Session.getInstance(properties, auth);
        // creates a new e-mail message
        Message messageContent = new MimeMessage(session);
        messageContent.setFrom(new InternetAddress(dto.getEmailFrom()));
        InternetAddress[] iAdressArray = InternetAddress.parse(to);
        messageContent.setRecipients(Message.RecipientType.TO, iAdressArray);
        messageContent.setSubject(subject);
        messageContent.setSentDate(new Date());
        // set plain text message
        messageContent.setContent(message, "text/html");
        // sends the e-mail
        Transport.send(messageContent);

    }

For the record to be left, I was having the same problem and thought it was an issue in the server configuration. It turned out to be a firewall configuration. This was not letting out the connection requests to the smtp server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM