This code works fine for PORT=465. But for PORT=587, it throws exception "Exception in thread main javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?"
package smtpClient;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import javax.naming.NamingException;
import javax.net.ssl.HandshakeCompletedEvent;
import javax.net.ssl.HandshakeCompletedListener;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
class TLS_Mime_G {
static final int PORT = 587;
static String REMOTEHOST = "smtp.gmail.com";
public static void main(String[] args)
throws IOException, NoSuchAlgorithmException, NamingException, KeyManagementException {
SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket) ssf.createSocket(REMOTEHOST, PORT);
socket.setEnabledProtocols(socket.getSupportedProtocols());
socket.setEnabledCipherSuites(socket.getSupportedCipherSuites());
socket.addHandshakeCompletedListener(new MyTLSHandshakeListener());
socket.startHandshake();//Throws Error
System.out.println("Connected to " + socket.getRemoteSocketAddress());
}
}
class MyTLSHandshakeListener implements HandshakeCompletedListener {
public void handshakeCompleted(HandshakeCompletedEvent e) {
System.out.println("Handshake succesful!");
System.out.println("Cipher suite used: " + e.getCipherSuite());
}
}
/* Supported Protocols: SSLv2Hello Supported Protocols: SSLv3 Supported Protocols: TLSv1 Supported Protocols: TLSv1.1 Supported Protocols: TLSv1.2 Enabled Protocols: TLSv1 Enabled Protocols: TLSv1.1 Enabled Protocols: TLSv1.2 */