添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

For appeals, questions and feedback about Oracle Forums, please email [email protected] . Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Oracle has updated its online Terms of Use and Community Guidelines and introduced a Community Integrity Policy . These changes document Community specific rules and Oracle’s content moderation practices including use of automated tools, appeals process, and Oracle’s contact details. If you object to any changes, you may request that your account be closed by contacting [email protected] . Your continued use of Oracle Communities means that you are consenting to the updated terms.

SSL java mail error"java.net.ConnectException: Connection refused: connect"

HaniYS Apr 9 2013 — edited Apr 10 2013
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();