1 Reply Latest reply: Feb 17, 2012 2:50 PM by bshannon RSS

    JavaMail connects to server via SSL then attempts to connect to locahost:25

    917768
      Solved the problem:

      Replaced
      transport.send(msg);
      With
      transport.sendMessage(msg,msg.getAllRecipients());
      ------------------------------------------
      Hi,

      I'm attempting to send an email over SSL from a webapp and I am experiencing a connection problem. I've migrated the code to a standalone class which reproduces the problem.

      The email server is remote to both the app server and standalone class --- mail.smtps.host (a FQDN to a remote system) is clearly defined from the debug information, they're 'aware' of it. There is no email server on localhost, nor do I desire to connect to localhost ...

      I've validated the email server SSL set up outside of Java using an email client.

      Full debug output is provided (at the bottom) after the standalone code with a dump of the properties after the pound symbols. The class appears to communicate over SSL (465) without issue:
           
           <snip>
           DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc]
           DEBUG SMTP: useEhlo true, useAuth true
           DEBUG SMTP: useEhlo true, useAuth true
           DEBUG SMTP: trying to connect to host "MY_SMTP_SERVER", port 465, isSSL true
           220 MY_SMTP_SERVER ESMTP
           DEBUG SMTP: connected to host "MY_SMTP_SERVER", port: 465
           <snip>

      A connection is indicate and no email is sent and, then puzzlingly, there's an attempt to connect to port 25 on my local system.

           <snip>
           235 authenticated.
           DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
           DEBUG SMTP: useEhlo true, useAuth false
           DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false
           <snip>     

      There is no stack trace when I execute the standalone code below, however, a java.net.ConnectException occurs on the app server. This exception is due to the class attempting to connect on localhost 25 (nope, no email server there) ...
      [2/15/12 10:55:07:045 EST] 00000025 SystemOut     O DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false
      javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
        nested exception is:
              java.net.ConnectException: Connection refused: connect
              at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
              at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
              at javax.mail.Service.connect(Service.java:275)
              at javax.mail.Service.connect(Service.java:156)
              at javax.mail.Service.connect(Service.java:105)
              at javax.mail.Transport.send0(Transport.java:168)
              at javax.mail.Transport.send(Transport.java:98)
      <snip>
      Caused by:
      java.net.ConnectException: Connection refused: connect
              at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:383)
              at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:245)
              at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:232)
              at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:377)
              at java.net.Socket.connect(Socket.java:539)
              at java.net.Socket.connect(Socket.java:488)
              at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
              at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
              at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
              ... 28 more
      There's an old post <JavaMail connects to SMTP server & then tries to send to localhost I changed
      send()
      to
      sendMessage()
      , but got
      SendMyMail.java:81: sendMessage(javax.mail.Message,javax.mail.Address[]) in javax.mail.Transport cannot be applied to (javax.mail.internet.MimeMessage)
                              transport.sendMessage(msg);
                                       ^
           Any help will be greatly appreciated. Thank you for your time.

      Regards,

      Pete

      Code:
           
      import java.util.Enumeration;
      import java.util.Properties;
      import javax.mail.*; 
      import javax.mail.internet.*;
      
      public class SendMyMail
      {
      
           public static void main(String[] args) 
           {
           
                Properties mailProps = new Properties();
               mailProps.put("mail.smtp.host", "MY_SMTP_SERVER");
                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);
      
                     mailProps.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
                     mailProps.put("mail.smtp.socketFactory.fallback", "false");               
                }
                
                mailProps.put("mail.smtp.auth", "true");
                
                Session session = Session.getInstance(mailProps,
                     new javax.mail.Authenticator() {
                          protected PasswordAuthentication getPasswordAuthentication() {
                               return new PasswordAuthentication("MY_USERNAME","MY_PASSWORD");
                          }
                     });
                     
                try 
                {
                     MimeMessage msg = new MimeMessage(session);          
                     msg.setRecipients( javax.mail.Message.RecipientType.TO, "MY_USERNAME" );
                     msg.setSubject("Test") ;     
                     
                     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.send(msg);
                     transport.close();
                     
                } catch (SendFailedException sfe)
                {
                }
                catch (NoSuchProviderException nspe)
                {
                }
                catch (MessagingException me)
                {
                }
           }
      }
      Debug output:
      C:\test> MY_JAVA_JRE_DIR\jre\bin\java.exe -cp .;mail14.jar SendMyMail
      DEBUG: JavaMail version 1.4ea
      DEBUG: java.io.FileNotFoundException: MY_JAVA_JRE_DIR\jre\lib\javamail.providers (The system cannot find the file specified.)
      DEBUG: !anyLoaded
      DEBUG: not loading resource: /META-INF/javamail.providers
      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,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTranspor
      t=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsyst
      ems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail
      .imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
      DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun M
      icrosystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems
      , Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
      DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
      DEBUG: !anyLoaded
      DEBUG: not loading resource: /META-INF/javamail.address.map
      DEBUG: java.io.FileNotFoundException: MY_JAVA_JRE_DIR\jre\lib\javamail.address.map (The system cannot find the file specified.)
      ########## propName mail.smtps.socketFactory.port = 465
      ########## propName mail.smtps.ssl.enable = true
      ########## propName mail.debug = true
      ########## propName mail.transport.protocol = smtps
      ########## propName mail.smtps.socketFactory.class = javax.net.ssl.SSLSocketFactory
      ########## propName mail.smtps.socketFactory.fallback = false
      ########## propName mail.smtps.auth = true
      ########## propName mail.smtps.host = MY_SMTP_SERVER
      DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc]
      DEBUG SMTP: useEhlo true, useAuth true
      DEBUG SMTP: useEhlo true, useAuth true
      DEBUG SMTP: trying to connect to host "MY_SMTP_SERVER", port 465, isSSL true
      220 MY_SMTP_SERVER ESMTP
      DEBUG SMTP: connected to host "MY_SMTP_SERVER", port: 465
      
      EHLO MY_LOCAL_MACHINENAME
      250-MY_SMTP_SERVER
      250-SIZE 20480000
      250 AUTH LOGIN PLAIN
      DEBUG SMTP: Found extension "SIZE", arg "20480000"
      DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
      DEBUG SMTP: Attempt to authenticate
      AUTH LOGIN
      334 VXNlcm5hqWUw
      c3NsLXBldGVabWF4ZGV2Lm9yZw==
      334 UGFzc4dvcwQ6
      aGFsYmVpd2Vw
      235 authenticated.
      DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
      DEBUG SMTP: useEhlo true, useAuth false
      DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false
      Edited by: 914765 on Feb 17, 2012 9:39 AM