Java maill SSL peer shut down incorrectly

Viewed 16

I'm working on an old Java 1.8 application which needs TLS to be configured for the mail sending part using java.mail-api of the javax-mail version 1.6.2. I added these properties to the properties map (pls notice the comment):

    Properties smtpProps = System.getProperties();
    smtpProps.put("mail.smtp.auth", config.getProperty("mail.smtpauth"));
    smtpProps.put("mail.smtp.host", config.getProperty("mail.smtpserver"));
    smtpProps.put("mail.smtp.port", config.getProperty("mail.smtpport"));
    smtpProps.put("mail.transport.protocol", "smtp");
    smtpProps.put("mail.smtp.connectiontimeout", config.getProperty("mail.timeout"));
    smtpProps.put("mail.debug", config.getProperty("mail.debug"));
    // Following block is new
    smtpProps.put("mail.smtp.starttls.enable", config.getProperty("mail.starttls"));
    smtpProps.put("mail.smtp.ssl.trust", config.getProperty("mail.ssltrust"));
    smtpProps.put("mail.smtp.ssl.protocols", config.getProperty("mail.sslprotocols"));
    smtpProps.put("mail.smtp.ssl.enable", config.getProperty("mail.ssl"));

Which will added to the session by overriding the PasswordAuthentication like this:

    session = Session.getDefaultInstance(smtpProps,
        new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, pwd);
            }
    });

At last, there are some couple of lines with the message itself and following block to send the mail:

        Transport t = session.getTransport();
    try {
        if (!user.equals(""))
            t.connect(user, pwd);
        else
            t.connect();
        msg.saveChanges();
        t.sendMessage(msg, msg.getAllRecipients());
    } catch (MessagingException e) {
        log.error("Error while sending the mail: " + e.getMessage());
        throw e;
    } finally {
        t.close();
        t = null;
    }

Unfortunately, this doesn't work. I'm getting an error which looks like this:

Caused by: java.io.EOFException: SSL peer shut down incorrectly

This exception points to the t.connect(user,pwd); line but not with authentication failed or so.

0 Answers
Related