I searched several related posts here and in other places, but none of them solved my problem. I have a program that sends emails to a set of people using "javamail API". It worked fine once. Today I needed again, but I cannot send any email... My sendEmail method is the following:
public void sendEmail(String userName, String password, String toAddress,
String subject, String message, String[] attachFiles)
throws AddressException, MessagingException {
// sets SMTP properties
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.user", userName);
properties.put("mail.password", password);
// creates a new session with an authenticator
Authenticator auth = new SMTPAuthenticator(userName, password);
Session session = Session.getInstance(properties, auth);
// creates a new e-mail message
MimeMessage msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(userName, "My name"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
InternetAddress[] toAddresses = {new InternetAddress(toAddress)};
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html");
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// adds attachments
if (attachFiles != null && attachFiles.length > 0) {
for (String filePath : attachFiles) {
addAttachment(multipart, filePath);
}
}
// sets the multi-part as e-mail's content
msg.setContent(multipart);
// sends the e-mail
Transport.send(msg);
}
So, now the attempt to call this method I got the following error (I'm using jdk 1.7.0_21):
Sending email Failed...
javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
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
at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1907)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:666)
at javax.mail.Service.connect(Service.java:367)
at javax.mail.Service.connect(Service.java:226)
at javax.mail.Service.connect(Service.java:175)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at EmailSender.sendEmail(EmailSender.java:86)
at CFP_LaWasp_EmailSender.sendCFPLaWasp(CFP_LaWasp_EmailSender.java:178)
at CFP_LaWasp_EmailSender.main(CFP_LaWasp_EmailSender.java:220)
Caused by: 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
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1886)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:276)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:270)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1341)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:153)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:868)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:804)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1016)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:528)
at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:465)
at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1902)
... 9 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
at sun.security.validator.Validator.validate(Validator.java:260)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1323)
... 19 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)
... 25 more
I don't know how to solve this issue, since it used to work before... Also, I don't know if my Gmail has something to do with this (if it blocked this access...)
I appreciate any help.
Why is my Gmail not sending or receiving emails? It may be possible that your browser's or app's cache may have got corrupted. We suggest you clear your browser cache and see. We also suggest the Gmail Storage Data and see.
Solution. Check SMTP settings, enable less secure apps, and unlock Captcha: Confirm the form's SMTP settings are correct. Enable access to Less secure apps.
Set up the app or device with the Gmail SMTP serverOn your device or in the app, enter smtp.gmail.com as the server address. In the Port field, enter one of the following numbers: If you're using SSL, enter 465. If you're using TLS, enter 587.
I had this problem with java 8. After updating this property problem solved
properties.put("mail.smtp.ssl.trust", "smtp.gmail.com")
if used spring boot in application.property
spring.mail.properties.mail.smtp.ssl.trust = smtp.gmail.com
I have been struggling with this error for several hours. I have tried to install the Server certificates using the answer provided by Bil Shannon. Nothing worked.
My problem was AVAST Antivirus. Once I have disabled Avast Mail Shield, I was able to send mails via gmail account from my web app.
The key error is this:
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
See this JavaMail FAQ entry.
Since you're connecting to Gmail, this shouldn't happen. The most likely causes are:
I've been solved my problem with:
properties.put("mail.smtp.ssl.trust", "smtp.gmail.com")
and when I'm using spring boot
spring.mail.properties.mail.smtp.ssl.trust = smtp.gmail.com
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With