0

I have create a Java application which can read mail from my gmail Inbox. My codes is good, but I get the following error on the console when I execute my code.. Can someone enlighten me on that and provide me the solution please ?

Stacktrace:

**javax.mail.MessagingException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException:** **Path does not chain with any of the trust anchors;**
  nested exception is:
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:665)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at pkg.RetrieveEmail.main(RetrieveEmail.java:21)

Code:

import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;

public class RetrieveEmail {

    public static void main(String[] args) {

       Properties props = System.getProperties();
       props.setProperty("mail.store.protocol", "imaps");

       try{
           Session session = Session.getDefaultInstance(props, null);
           Store store = session.getStore("imaps");
           store.connect("imap.gmail.com", "someone@gmail.com", "xxxx");
           System.out.println(store);

           Folder inbox = store.getFolder("Inbox");
           inbox.open(Folder.READ_ONLY);
           Message messages[] = inbox.getMessages();

           for(Message message : messages){
               System.out.println(message.getSubject());
           }
       }catch(MessagingException e){
           e.printStackTrace();
           System.exit(2);
       }catch(Exception e1){
           e1.printStackTrace();
       }
    }   
}
Sandeep Chatterjee
  • 3,220
  • 9
  • 31
  • 47
James Smith
  • 55
  • 2
  • 11

0 Answers0