Hi
I am try to send mail with SSL auth to exchange server, this error appear when i try to do it *"java.net.ConnectException: Connection refused: connect"* and i try with keystore and without it
DEBUG: JavaMail version 1.4.7
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
########## propName mail.smtps.socketFactory.fallback = false
########## propName mail.smtps.ssl.checkserveridentity = true
########## propName mail.smtps.ssl.socketFactory = com.sun.mail.util.MailSSLSocketFactory@d02b51
########## propName mail.transport.protocol = smtps
########## propName mail.debug = true
########## propName mail.smtps.host = ****
########## propName mail.smtps.ssl.enable = true
########## propName mail.smtps.socketFactory.class = javax.net.ssl.SSLSocketFactory
########## propName mail.smtps.auth = true
########## propName mail.smtps.socketFactory.port = 465
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "****", port 465, isSSL true
javax.mail.MessagingException: Could not connect to SMTP host: ****, port: 465;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at com.test.SendMyMail.main(SendMyMail.java:76)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:559)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:321)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:211)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
The code:
import com.sun.mail.util.MailSSLSocketFactory;
import java.security.GeneralSecurityException;
import java.util.Enumeration;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class SendMyMail {
public static void main(String[] args) throws GeneralSecurityException {
Properties mailProps = new Properties();
mailProps.put("mail.smtps.host", "****");
boolean smtpSslEnable = true;
mailProps.put("mail.smtp.ssl.enable", smtpSslEnable);
mailProps.put("mail.debug", "true");
if (smtpSslEnable) {
mailProps.put("mail.transport.protocol", "smtps");
mailProps.put("mail.smtp.ssl.enable", smtpSslEnable);
mailProps.put("mail.smtp.socketFactory.port", 465); //465
mailProps.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
mailProps.put("mail.smtp.socketFactory.fallback", "false");
//Try with keystore and without it
mailProps.put("javax.net.ssl.trustStore","mailKeyStore.kjs");
mailProps.put("javax.net.ssl.trustStorePassword","A123456");
mailProps.put("mail.smtp.auth", "true");
Session session = Session.getInstance(mailProps, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("USERNAME", "PASSWORD");
try {
MimeMessage msg = new MimeMessage(session);
msg.setRecipients(javax.mail.Message.RecipientType.TO, "EMAIL");
msg.setSubject("AA Subject");
msg.setText("AA Body");
Enumeration enum1 = mailProps.propertyNames();
if (smtpSslEnable) {
for (; enum1.hasMoreElements(); ) {
String propName = (String)enum1.nextElement();
if (propName.startsWith("mail.smtp.")) {
mailProps.put(propName.replaceFirst("smtp", "smtps"), mailProps.get(propName));
mailProps.remove(propName);
Enumeration enum2 = mailProps.propertyNames();
for (; enum2.hasMoreElements(); ) {
String propName = (String)enum2.nextElement();
if (propName.startsWith("mail")) {
System.out.println("########## propName " + propName + " = " + mailProps.get(propName));
Transport transport = session.getTransport("smtps");
transport.connect();
transport.sendMessage(msg,msg.getAllRecipients());
transport.close();
} catch (SendFailedException sfe) {
sfe.printStackTrace();
} catch (NoSuchProviderException nspe) {
nspe.printStackTrace();
} catch (MessagingException me) {
me.printStackTrace();