mr.hironobu
mr.hironobu

Reputation: 177

JavaMail POP3 over SSL Connect failed;

I would like to download mail by accessing the POP3 server SSL is configured on the local environment, but we failed.

What steps do you need to take when connecting?

Exception

javax.mail.MessagingException: Connect failed;
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

Sample code

Properties props = new Properties();
final String HOST = "host";
final String PROTOCOL = "pop3";
final String PORT = "995";
String username = "user";
String pass = "password";
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.pop3.port", PORT);
props.setProperty("mail.pop3.socketFactory.port", PORT);
URLName urln = new URLName(PROTOCOL, HOST, Integer.parseInt(PORT), null, username, pass);

Session session = Session.getInstance(props, null);
Store store = session.getStore(urln);

store.connect();

Upvotes: 3

Views: 9395

Answers (1)

user432
user432

Reputation: 3534

Set the property mail.pop3.ssl.trust

props.setProperty("mail.pop3.ssl.trust", "*"); // Trust all Servers

You should also be able to only set it to your specific server, test with * before though

props.setProperty("mail.pop3.ssl.trust", HOST);

Upvotes: 1

Related Questions