我还有另一个转换套接字异常。Stackoverflow 将我链接到相关答案,我已经完成了人们在那里提出的所有建议。
这里我们有异常描述:
线程“主”javax.mail.MessagingException 中的异常:无法将套接字转换为 TLS;嵌套异常是:java.net.SocketException:java.security.NoSuchAlgorithmException:
在 com.sun.mail.smtp.SMTPTransport 构造实现时出错(算法:默认,提供者:SunJSSE,类:sun.security.ssl.SSLContextImpl$DefaultSSLContext) .startTLS(SMTPTransport.java:1880) 在 com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:648) 在 javax.mail.Service.connect(Service.java:295) 在 XMailMessenger.MailMessenger.sendMail( MailMessenger.java:414) at alfa.runner.main(runner.java:38) 原因:java.net.SocketException: java.security.NoSuchAlgorithmException: Error
在 javax.net.ssl.DefaultSSLSocketFactory.throwException(Unknown Source) at javax.net.ssl.DefaultSSLSocketFactory.createSocket(Unknown Source) 构造实现(算法:默认,提供者:SunJSSE,类:sun.security.ssl.SSLContextImpl$DefaultSSLContext) ) at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:432) at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1875) ... 4 更多原因:java.security .NoSuchAlgorithmException:构造实现时出错
(算法:默认,提供者:SunJSSE,类:
sun.security.ssl.SSLContextImpl$DefaultSSLContext) 在 java.security.Provider$Service.newInstance(Unknown Source) 在 sun.security.jca.GetInstance.getInstance(Unknown Source) 在 sun.security.jca.GetInstance.getInstance(Unknown来源)在 javax.net.ssl.SSLContext.getInstance(Unknown Source) 在 javax.net.ssl.SSLContext.getDefault(Unknown Source) 在 javax.net.ssl.SSLSocketFactory.getDefault(Unknown Source) 在 com.sun.mail .util.SocketFetcher.startTLS(SocketFetcher.java:427) ... 5 更多原因: java.lang.NullPointerException at sun.security.ssl.KeyManagerFactoryImpl$SunX509.engineInit(Unknown Source) at javax.net.ssl.KeyManagerFactory sun.security.ssl.SSLContextImpl$DefaultSSLContext.getDefaultKeyManager 的 .init(未知来源)(未知
来源)
在 sun.security.ssl.SSLContextImpl$DefaultSSLContext.(Unknown Source) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance( Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at java.lang.Class.newInstance(Unknown Source) ... 12 更多
所以我决定写另外两个类:
package XMailMessenger;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;
/**
* @author Raminderjeet Singh
*/
public class DummyTrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] cert, String authType) {
// everything is trusted
}
public void checkServerTrusted(X509Certificate[] cert, String authType) {
// everything is trusted
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
第二个:
package XMailMessenger;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import javax.net.SocketFactory;
import javax.net.ssl.*;
/**
* @author Raminderjeet Singh
*/
public class DummySSLSocketFactory extends SSLSocketFactory {
private SSLSocketFactory factory;
public DummySSLSocketFactory() {
try {
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null,
new TrustManager[] { new DummyTrustManager()},
null);
factory = (SSLSocketFactory)sslcontext.getSocketFactory();
} catch(Exception ex) {
// ignore
}
}
public static SocketFactory getDefault() {
return new DummySSLSocketFactory();
}
public Socket createSocket() throws IOException {
return factory.createSocket();
}
public Socket createSocket(Socket socket, String s, int i, boolean flag)
throws IOException {
return factory.createSocket(socket, s, i, flag);
}
public Socket createSocket(InetAddress inaddr, int i,
InetAddress inaddr1, int j) throws IOException {
return factory.createSocket(inaddr, i, inaddr1, j);
}
public Socket createSocket(InetAddress inaddr, int i)
throws IOException {
return factory.createSocket(inaddr, i);
}
public Socket createSocket(String s, int i, InetAddress inaddr, int j)
throws IOException {
return factory.createSocket(s, i, inaddr, j);
}
public Socket createSocket(String s, int i) throws IOException {
return factory.createSocket(s, i);
}
public String[] getDefaultCipherSuites() {
return factory.getDefaultCipherSuites();
}
public String[] getSupportedCipherSuites() {
return factory.getSupportedCipherSuites();
}
}
最后我们有了发送邮件的方法:
Properties props = new Properties();
props.put("mail.smtp.host", currentSettingsDocument.getSMTPHostServer());
props.put("mail.smtp.user", currentSettingsDocument.getLogin());
props.put("mail.smtp.password", currentSettingsDocument.getPassword());
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.timeout" , "3000");
props.put("mail.smtp.starttls.enable", "false");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
props.put("javax.net.debug","ssl,session");
// Required to avoid security exception.
Authenticator authentication = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(currentSettingsDocument.getLogin() ,
currentSettingsDocument.getPassword());
}
};
Session session = Session.getInstance(props, authentication);
// create some properties and get the default Session
// create a message
MimeMessage msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new
InternetAddress(currentSettingsDocument.getSenderMail());
msg.setFrom(addressFrom);
for(InternetAddress addr: recepients )
msg.addRecipient(Message.RecipientType.TO, addr );
// Optional : You can also set your custom headers in the Email if you
//
// msg.addHeader("MyHeaderName", "myHeaderValue");
// Setting the Subject and Content Type
msg.setContent("1111", "text/plain");
Transport transport = session.getTransport("smtp");
Document.getSMTPHostServer(), Document.getPassword());
transport.connect(currentSettingsDocument.getSMTPHostServer(), 465,
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
在调用sendMessage后,我得到了异常:
окт 18, 2013 5:36:18 PM XMailMessenger.MailMessenger sendMail SEVERE: null javax.mail.MessagingException: 异常读取响应;嵌套异常是:java.net.SocketTimeoutException:读取超时
PS谷歌邮件正常工作