I have enother convert socket exception. Stackoverflow linked me to related answer and i have done everything people suggest there.
Here we have exception description :
Exception in thread "main" javax.mail.MessagingException: Could not convert socket to TLS; nested exception is: java.net.SocketException: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class:
sun.security.ssl.SSLContextImpl$DefaultSSLContext) at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1880) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:648) at javax.mail.Service.connect(Service.java:295) at XMailMessenger.MailMessenger.sendMail(MailMessenger.java:414) at alfa.runner.main(runner.java:38) Caused by: java.net.SocketException: java.security.NoSuchAlgorithmException: Error
constructing implementation (algorithm: Default, provider: SunJSSE, class: sun.security.ssl.SSLContextImpl$DefaultSSLContext) at javax.net.ssl.DefaultSSLSocketFactory.throwException(Unknown Source) at javax.net.ssl.DefaultSSLSocketFactory.createSocket(Unknown Source) at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:432) at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1875) ... 4 more Caused by: java.security.NoSuchAlgorithmException: Error constructing implementation
(algorithm: Default, provider: SunJSSE, class:
sun.security.ssl.SSLContextImpl$DefaultSSLContext) at java.security.Provider$Service.newInstance(Unknown Source) at sun.security.jca.GetInstance.getInstance(Unknown Source) at sun.security.jca.GetInstance.getInstance(Unknown Source) at javax.net.ssl.SSLContext.getInstance(Unknown Source) at javax.net.ssl.SSLContext.getDefault(Unknown Source) at javax.net.ssl.SSLSocketFactory.getDefault(Unknown Source) at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:427) ... 5 more Caused by: java.lang.NullPointerException at sun.security.ssl.KeyManagerFactoryImpl$SunX509.engineInit(Unknown Source) at javax.net.ssl.KeyManagerFactory.init(Unknown Source) at sun.security.ssl.SSLContextImpl$DefaultSSLContext.getDefaultKeyManager(Unknown
Source)
at sun.security.ssl.SSLContextImpl$DefaultSSLContext.(Unknown Source) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at java.lang.Class.newInstance(Unknown Source) ... 12 more
So i decide to write 2 additional classes:
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];
}
}
and the second one:
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();
}
}
Finally we have method to send mail:
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();
And after calling sendMessage i get exception :
окт 18, 2013 5:36:18 PM XMailMessenger.MailMessenger sendMail SEVERE: null javax.mail.MessagingException: Exception reading response; nested exception is: java.net.SocketTimeoutException: Read timed out
PS Google mail works properly
The exception is caused by the unsupported TLS on old Windows (XP, Vista) and thrown by the class SocketFetcher (package com.sun.mail.util;) at line
MailSSLSocketFactory msf = new MailSSLSocketFactory();
and MailSSLSocketFactory (same package) /** * Initializes a new MailSSLSocketFactory. * * @throws GeneralSecurityException */ public MailSSLSocketFactory() throws GeneralSecurityException { this("TLS"); }
/** * Initializes a new MailSSLSocketFactory. * * @throws GeneralSecurityException */ public MailSSLSocketFactory() throws GeneralSecurityException { this("TLS"); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.