11 Replies Latest reply on Dec 17, 2012 4:20 AM by nickarls

    Connection refused: connect on Transport.send when trying to send a mail

    niraz

      Hi All,

       

       

      I'm new to Jboss, I have jboss 7.1.0.Final and I'm trying to send a mail with the following code:

       

      PrintWriter out=res.getWriter();

      try

      {

           MimeMessage m = new MimeMessage(mailSession);

           Address from = new InternetAddress("senderUser@middlewaremagic.com");

           Address[] to = new InternetAddress[] {new InternetAddress("recieverUser@middlewaremagic.com") };

           m.setFrom(from);

           m.setRecipients(Message.RecipientType.TO, to);

           m.setSubject("JavaMail Test - 2 ");

           m.setSentDate(new java.util.Date());

           m.setContent("Test from inside JBoss AS7 Server","text/plain");

           Transport.send(m);

           out.println("<font color=green> Mail Sent Successfully.</font>");

      }

      catch (javax.mail.MessagingException e)

      {

           e.printStackTrace();

           out.println("<font color=red>Error in Sending Mail: "+e+"</font>");

      }

       

      I'm getting the following exception:

       

      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:1934)

      at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)

      at javax.mail.Service.connect(Service.java:295)

      at javax.mail.Service.connect(Service.java:176)

      at javax.mail.Service.connect(Service.java:125)

      at javax.mail.Transport.send0(Transport.java:194)

      at javax.mail.Transport.send(Transport.java:124)

      at intermediate.EmailAlerts.doGet(EmailAlerts.java:44)

      at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)

      at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)

      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)

      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)

      at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:154)

      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)

      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)

      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)

      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)

      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)

      at java.lang.Thread.run(Thread.java:662)

       

      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 java.net.Socket.connect(Socket.java:478)

      at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288)

      at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231)

      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)

      ... 22 more

       

       

      Any idea what can be the issue?

       

      Thanks in advance,

      Nir

        • 1. Re: Connection refused: connect on Transport.send when trying to send a mail
          nickarls

          You have no SMTP listener on localhost, port 25.

          • 2. Re: Connection refused: connect on Transport.send when trying to send a mail
            niraz

            Thanks for the quick answer Nicklas.

            I will check on how to set the SMTP Listener.

             

            In addition, I have the following on deployment:

             

            JBAS014775:    New missing/unsatisfied dependencies:

                  service jboss.binding.management-http (missing) dependents: [service jboss.serverManagement.controller.management.http]

                  service jboss.binding.management-native (missing) dependents: [service jboss.remoting.server.management]

                  service jboss.outbound-socket-binding.mail-smtp (missing) dependents: [service jboss.mail-session.java:jboss/mail/Default]

             

            what seems to be the issue here, and is it related to the exception?

             

            Thanks,

            Nir

            • 3. Re: Connection refused: connect on Transport.send when trying to send a mail
              ctomc

              Hi,

               

              looks like you are missing mail-smtp outbound-socket-binding configuration.

               

               

               

              --

              tomaz

              1 of 1 people found this helpful
              • 4. Re: Connection refused: connect on Transport.send when trying to send a mail
                niraz

                Thanks Tomaz.

                 

                 

                 

                I'm trying now to send mail with the following standalone-full.xml configuration:

                 

                <subsystem xmlns="urn:jboss:domain:mail:1.0">

                             <mail-session jndi-name="java:/Mail" from="myGmailMailAddress" >

                                                                <smtp-server outbound-socket-binding-ref="mail-smtp" ssl="true">

                                                   <login name="myGmailUserName" password="myGmailPassword"/>

                                                                </smtp-server>

                                                                <pop3-server outbound-socket-binding-ref="mail-pop3"/>

                                                                <imap-server outbound-socket-binding-ref="mail-imap">

                                                          <login name="nobody" password="pass"/>

                                                                </imap-server>

                                                                      </mail-session>

                                                             <mail-session debug="true" jndi-name="java:jboss/mail/Default" >

                                                                <smtp-server outbound-socket-binding-ref="mail-smtp"/>

                                                                      </mail-session>

                        </subsystem>

                 

                 

                ...

                 

                <socket-binding-group name="standard-sockets" default-interface="public" port-offset="${jboss.socket.binding.port-offset:0}">

                        <socket-binding name="http" port="8080"/>

                        <socket-binding name="https" port="8443"/>

                        <socket-binding name="jacorb" interface="unsecure" port="3528"/>

                        <socket-binding name="jacorb-ssl" interface="unsecure" port="3529"/>

                        <socket-binding name="management-native" interface="management" port="${jboss.management.native.port:9999}"/>

                        <socket-binding name="management-http" interface="management" port="${jboss.management.http.port:9990}"/>

                        <socket-binding name="management-https" interface="management" port="${jboss.management.https.port:9443}"/>

                        <socket-binding name="messaging" port="5445"/>

                        <socket-binding name="messaging-throughput" port="5455"/>

                        <socket-binding name="osgi-http" interface="management" port="8090"/>

                        <socket-binding name="remoting" port="4447"/>

                        <socket-binding name="txn-recovery-environment" port="4712"/>

                        <socket-binding name="txn-status-manager" port="4713"/>

                        <outbound-socket-binding name="mail-pop3">

                             <remote-destination host="smtp.gmail.com" port="465"/>

                        </outbound-socket-binding>

                    </socket-binding-group>

                 

                 

                and the following code:

                PrintWriter out=res.getWriter();

                                     try   

                                     {          

                                               MimeMessage m = new MimeMessage(mailSession);

                                               Address from = new InternetAddress("senderUser@middlewaremagic.com");

                                               Address[] to = new InternetAddress[] {new InternetAddress("nir.azriel1@gmail.com") };

                                              

                                         m.setFrom(from);

                                         m.setRecipients(Message.RecipientType.TO, to);

                                         m.setSubject("JavaMail Test - 2 ");

                                         m.setSentDate(new java.util.Date());

                                         m.setContent("Test from inside JBoss AS7 Server","text/plain");

                                         Transport.send(m);

                                         out.println("<font color=green> Mail Sent Successfully.</font>");

                                     }

                                     catch (javax.mail.MessagingException e)

                                     {

                                               e.printStackTrace();

                                               out.println("<font color=red>Error in Sending Mail: "+e+"</font>");

                                     }

                 

                and still getting exception:

                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:1934)

                 

                what am I missing? why does it tries to connect to localhost port 25? where should I change it?

                (when trying to connect with telnet to port 25 I got the following:

                Connecting To localhost...Could not open connection to the host, on port 25: Connect failed)

                 

                Thanks again,

                Nir

                • 5. Re: Connection refused: connect on Transport.send when trying to send a mail
                  ctomc

                  Hi,

                   

                  your configuration is bit wrong...

                   

                  you have just one outbind socket binding called mail-pop3 but in mail subsystem you reference mail-smtp, mail-imap & mail-pop3

                   

                  you probably want something like this:

                   

                  <subsystem xmlns="urn:jboss:domain:mail:1.0">

                               <mail-session jndi-name="java:/Mail" from="myGmailMailAddress" >

                                                                  <smtp-server outbound-socket-binding-ref="mail-smtp" ssl="true">

                                                     <login name="myGmailUserName" password="myGmailPassword"/>

                                                                  </smtp-server>

                                                            <login name="nobody" password="pass"/>

                                                                  </imap-server>

                                                                        </mail-session>                                        

                          </subsystem>

                   

                  ....

                  ....

                   

                     <outbound-socket-binding name="mail-smtp">

                               <remote-destination host="smtp.gmail.com" port="465"/>

                          </outbound-socket-binding>

                  --

                  tomaz

                  • 6. Re: Connection refused: connect on Transport.send when trying to send a mail
                    niraz

                    Hi,

                     

                    I did as you suggested, but I keep on getting the exception:

                    09:15:35,685 ERROR [stderr] (http--0.0.0.0-8080-2) javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;

                    09:15:35,687 ERROR [stderr] (http--0.0.0.0-8080-2)   nested exception is:

                    09:15:35,688 ERROR [stderr] (http--0.0.0.0-8080-2)           java.net.ConnectException: Connection refused: connect

                    09:15:35,689 ERROR [stderr] (http--0.0.0.0-8080-2)           at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)

                    09:15:35,691 ERROR [stderr] (http--0.0.0.0-8080-2)           at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)

                    09:15:35,693 ERROR [stderr] (http--0.0.0.0-8080-2)           at javax.mail.Service.connect(Service.java:295)

                     

                     

                    For some reason it's still trying to connect to localhost.

                    • 7. Re: Connection refused: connect on Transport.send when trying to send a mail
                      nickarls

                      So you no longer see the missing dependencies error?

                      Tried using getProperties of the mail session and see what values are picked up?

                      • 8. Re: Connection refused: connect on Transport.send when trying to send a mail
                        niraz

                        The missing dependencies errors are gone.

                         

                        The mail session is null : / does it suppose to pick up his values from the standalone.xml/standalone-full.xml?

                        • 9. Re: Connection refused: connect on Transport.send when trying to send a mail
                          nickarls

                          How *are* you getting the session? The mail session in standalone.xml is the one you get with a @Resource injection on the mail Session.

                          • 10. Re: Connection refused: connect on Transport.send when trying to send a mail
                            niraz

                            Hi,

                             

                            I added @Resource(lookup="java:/Mail"), but it won't take it "The attribute lookup is undefined for the annotation type Resource" since I'm using JBDS 5.0.0.

                            I saw there was a bug ticket for that, so now I have a different issue to solve

                            • 11. Re: Connection refused: connect on Transport.send when trying to send a mail
                              nickarls

                              That was something about endorsed libs, right?

                               

                              Are you following http://www.mastertheboss.com/jboss-configuration/jboss-mail-service-configuration ? They appear to use the mappedName attribute there