我将 SMTP 用于邮件并检索我使用 imaps (imap + ssl) 的邮件:
我做的内部连接方法:
private Session connect(SMTPTask task) {
SSLSocketFactory factory = null;
try {
SSLContext ctx;
KeyManagerFactory kmf;
KeyStore ks;
char[] passphrase = "changeit".toCharArray();
ctx = SSLContext.getInstance("TLS");
kmf = KeyManagerFactory.getInstance("SunX509");
ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream("truststore.jks"), passphrase);
kmf.init(ks, passphrase);
ctx.init(kmf.getKeyManagers(), null, null);
factory = ctx.getSocketFactory();
} catch (Exception e) {
log.error("Error with SSLFactory",e);
}
String host = getHost();
String port = String.valueOf(getPort());
Authenticator authenticator = new Authenticator(task);
Properties properties = new Properties();
properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
properties.setProperty("mail.smtp.auth", "true");
//properties.setProperty("mail.imap.auth.login.disable", "true");
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.port", port);
properties.put("mail.imaps.ssl.socketFactory", factory);
properties.setProperty("mail.imap.port", "993");
Properties systemProps = System.getProperties();
systemProps.put( "javax.net.ssl.trustStore", "truststore.jks");
systemProps.put( "javax.net.ssl.trustStorePassword", "changeit");
System.setProperties(systemProps);
return Session.getInstance(properties, authenticator);
我在其他地方做:
...
store.connect(getHost(),getUser(),getPass());
...
编辑:在这里我得到以下异常:
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
现在我想添加一个带有自己证书的自定义信任库。另一篇文章展示了如何在 java 中做到这一点:为客户端身份验证添加 trustStore
但是如何将 SSLSocketFactory 与我的商店结合起来?所以我的商店使用自定义 ssl 连接?
这样做的正确方法是什么?如果那不可能,是否有另一种方法可以将我的信任库添加到 java 中的商店?
编辑:我错过了什么,或者我在属性中做错了什么?
问候