无法发送邮件:无法将套接字转换为TLS;

时间:2016-03-09 15:19:15

标签: java email outlook javamail office365

以下代码用作Java应用程序eclipse,但当它部署在SAP PO服务器中时,抛出以下异常:

注意:我的笔记本电脑(Windows)上运行独立的java文件,而服务器不是Linux

  

javax.mail.MessagingException:无法将套接字转换为TLS;   嵌套异常是:java.net.SocketException:   java.security.NoSuchAlgorithmException:构造错误   实现(算法:默认,提供者:SunJSSE,类:   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);

    }

1 个答案:

答案 0 :(得分:0)

对于要留下的记录,我遇到了同样的问题,并认为这是服务器配置中的问题。 结果证明是防火墙配置。这并没有将连接请求泄露给smtp服务器。