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?