0

I'm trying to send Mail using SMTP, which set as default smtp server with 25,587 ports and TLS. MS Exchange 2010. All auth data is correct, because i can log in via web-interface of mail. Code below does work with gmail smtp server, but when i use my own smtp server it throws me error with cert paths:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

public static void SendMailTLS(String to, String subject, String body) throws Exception{

    try {
                java.util.Properties props =  new java.util.Properties();
                props.put("mail.smtp.host", "sample");
                props.put("mail.smtp.port", "587");
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.connectiontimeout", "10000");    
                final String EmailUser = "sample";
                final String EmailPassword = "sample";    
                Session session = Session.getInstance(props, new javax.mail.Authenticator() {
                          protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(
                                EmailUser,EmailPassword);
                             }
                       });    session.setDebug(true);    
                InternetAddress fromAddress = new InternetAddress("sample",
                                        "sample");
                InternetAddress toAddress = new InternetAddress("sample",
                                        "sample");
                Message msg = new MimeMessage(session);
                msg.setFrom(fromAddress);
                msg.addRecipient(Message.RecipientType.TO,toAddress);
                msg.setSubject(subject);
                msg.setText(body);    
                Transport transport = session.getTransport("smtp");
                transport.connect();
                transport.sendMessage(msg, msg.getAllRecipients());
                } catch (MessagingException e) {e.printStackTrace();}
    }

Am I doing something wrong?

Panda
  • 27
  • 1
  • 8

1 Answers1

1

This error message means that your server named "sample" (the MS Exchange 2010 server) is using a certificate that is not trusted by your java runtime. You may probably be using a self-signed certificate. So you need to import this certificate into the Java Runtime certificate store, with keytool.

Alexandre Fenyo
  • 4,526
  • 1
  • 17
  • 24