12 Replies Latest reply on Dec 16, 2012 8:01 AM by lafr

    How to configuration javamail with jboss 7.1.1final

    samwun9988

      Hi, with jboss 7.1.1.final, I am not able to send email through a remote smtp server.

       

      Here is my module.xml file:

       

       

      <?xml version="1.0" encoding="UTF-8"?>

       

       

      <module xmlns="urn:jboss:module:1.0" name="javax.activation.api">

       

       

          <dependencies>

       

       

              <module name="javax.api" />

       

       

              <module name="javax.mail.api" >

       

       

                  <imports><include path="META-INF"/></imports>

       

       

              </module>

       

       

          </dependencies>

       

       

       

           <resources>

       

       

              <resource-root path="activation-1.1.1.jar"/>

       

       

              <!-- Insert resources here -->

       

       

          </resources>

       

       

      </module>

       

       

       

      My java client code:

       

       

       

       

      package LuxuryLiving.utils;

       

      import java.util.Properties;

       

      import javax.mail.Message;

      import javax.mail.MessagingException;

      import javax.mail.Session;

      import javax.mail.Transport;

      import javax.mail.Message.RecipientType;

      import javax.mail.internet.AddressException;

      import javax.mail.internet.InternetAddress;

      import javax.mail.internet.MimeMessage;

      import javax.naming.Context;

      import javax.naming.InitialContext;

      import javax.naming.NamingException;

       

      public class SendMail {

       

          private String from;

          private String to;

          private String subject;

          private String text;

       

          public SendMail(String from, String to, String subject, String text){

              this.from = from;

              this.to = to;

              this.subject = subject;

              this.text = text;

          }

       

          public void send() throws NamingException{

       

      //        Context initCtx = new InitialContext();

      //javax.mail.Session session = (Session) initCtx.lookup("java:/Mail/Default");

      //

      //String transportProtocol = session.getProperty("mail.transport.protocol");

      //String mailServer = session.getProperty("mail.smtp.host");

      //String mailUser = session.getProperty("mail.smtp.user");

       

       

              Properties props = new Properties();

              props.put("mail.smtp.host", "smtp-auth.no-ip.com");

              props.put("mail.smtp.port", "587");

               props.setProperty("mail.user", "xxx@no-ipdomain.com.au");

              props.setProperty("mail.password", "mypass");

       

              Session mailSession = Session.getDefaultInstance(props);

              Message simpleMessage = new MimeMessage(mailSession);

       

              InternetAddress fromAddress = null;

              InternetAddress toAddress = null;

              try {

                  fromAddress = new InternetAddress(from);

                  toAddress = new InternetAddress(to);

              } catch (AddressException e) {

                  // TODO Auto-generated catch block

                  throw new RuntimeException(e);

              }

       

              try {

                  simpleMessage.setFrom(fromAddress);

                  simpleMessage.setRecipient(RecipientType.TO, toAddress);

                  simpleMessage.setSubject(subject);

                  simpleMessage.setText(text);

       

                  Transport.send(simpleMessage);

              } catch (MessagingException e) {

                  // TODO Auto-generated catch block

                  throw new RuntimeException(e);

              }

          }

      }

       

       

      What I have missing ?
      Any suggestion is very appreciated.

      Thanks

      Sam

        • 1. Re: How to configuration javamail with jboss 7.1.1final
          nickarls

          What happens?

          • 2. Re: How to configuration javamail with jboss 7.1.1final
            samwun9988

            I don't receive email.

            There is no exception .

            Thanks

            Samuel

            • 3. Re: How to configuration javamail with jboss 7.1.1final
              nickarls

              And you are sure the code is executed?

              • 4. Re: How to configuration javamail with jboss 7.1.1final
                lafr

                For what you need the module.xml?

                JBoss AS contains already a mail module to configure via standalone.xml. There you can define smtp, pop3 or imap.

                 

                Then you can use injection:

                @Resource(name = "java:/Mail")
                private Session mailSession;

                to get a mail session object.

                 

                And, to make sure that you code is really executed, add some debug or trace logging to your code.

                Client code means, you standalone application connecting to a JBoss instance?

                 

                What you do with a RuntimeException or Exceptions at all?

                • 5. Re: How to configuration javamail with jboss 7.1.1final
                  samwun9988

                  Thank you for the suggestion.

                  Where can I read about how to do this in standalone.xml and a way how to use java:/Mail/Default in the code?
                  Thanks

                  Sam

                  • 6. Re: How to configuration javamail with jboss 7.1.1final
                    samwun9988

                    I think the following mail session in standalone-full.xml is not complete:

                     

                     

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

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

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

                                </mail-session>

                            </subsystem>

                     

                     

                    How to add server, user id , password etc in the mail-session?

                    Thanks

                     

                    Sam

                    • 7. Re: How to configuration javamail with jboss 7.1.1final
                      samwun9988

                      And I don't have a local  mail server to use. I have to use a remote smtp server  eg. smtp-auth.no-ip.com

                      Thanks

                      Sam

                      • 8. Re: How to configuration javamail with jboss 7.1.1final
                        samwun9988

                        I need to setup this mail session to be used by my application Contact Us form.

                        Any example of how to  setup mail session and how to use it in java code is very much appreciated.

                        Thanks

                        Sam

                        • 9. Re: How to configuration javamail with jboss 7.1.1final
                          samwun9988

                          By the way, do I need to create the following module.xml (or replace the existinig one in jboss by this):

                           

                           

                          <module xmlns="urn:jboss:module:1.1" name="org.jboss.as.mail">

                              <resources>

                                  <resource-root path="jboss-as-mail-7.1.0.Final-SNAPSHOT.jar"/>

                                  <!-- Insert resources here -->

                              </resources>

                           

                           

                              <dependencies>

                                  <module name="javax.api"/>

                                  <module name="javax.mail.api">

                                      <imports>

                                          <include path="META-INF"/>

                                      </imports>

                                  </module>

                                  <module name="org.jboss.as.network"/>

                                  <module name="org.jboss.as.naming"/>

                                  <module name="org.jboss.staxmapper"/>

                                  <module name="org.jboss.as.controller"/>

                                  <module name="org.jboss.as.server"/>

                                  <module name="org.jboss.modules"/>

                                  <module name="org.jboss.msc"/>

                                  <module name="org.jboss.logging"/>

                              </dependencies>

                          </module>

                           

                          • 10. Re: How to configuration javamail with jboss 7.1.1final
                            samwun9988

                            I got the following error exception:

                             

                             

                            er.CategoryController@7a35db6f]: java.lang.RuntimeException: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Authentication required

                             

                             

                            12:02:15,720 DEBUG [org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver] (http--192.168.1.20-8080-4) Resolving exception from handler [LuxuryLiving.controller.CategoryController@7a35db6f]: java.lang.RuntimeException: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Authentication required

                             

                             

                            12:02:15,721 DEBUG [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (http--192.168.1.20-8080-4) Resolving exception from handler [LuxuryLiving.controller.CategoryController@7a35db6f]: java.lang.RuntimeException: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Authentication required

                             

                             

                            12:02:15,722 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http--192.168.1.20-8080-4) Could not complete request: java.lang.RuntimeException: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Authentication required

                             

                             

                                    at LuxuryLiving.utils.SendMail.send(SendMail.java:72) [classes:]

                                    at LuxuryLiving.controller.CategoryController.submitCustInquiry(CategoryController.java:169) [classes:]

                                    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_07]

                                    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [rt.jar:1.6.0_07]

                                    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [rt.jar:1.6.0_07]

                                    at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_07]

                                    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176) [spring-web-3.0.7.RELEASE.jar:3.0.7.RELEASE]

                                    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436) [spring-webmvc-3.0.7.RELEASE.jar:3.0.7.RELEASE]

                                    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424) [spring-webmvc-3.0.7.RELEASE.jar:3.0.7.RELEASE]

                             

                             

                            But I have set authentication in the send() method:

                             

                             

                            Properties props = new Properties();

                                    props.put("mail.smtp.host", "smtp-auth.no-ip.com");

                                    props.put("mail.smtp.port", "587");

                                     props.setProperty("mail.user", "info@myco.com.au");

                                    props.setProperty("mail.password", "mypass");

                             

                            • 11. Re: How to configuration javamail with jboss 7.1.1final
                              nickarls

                              Authentication probably comes from you needing to use t.connect with un/pw on a Transport instance

                               

                              The XSD can be found at https://github.com/jbossas/jboss-as/blob/master/build/src/main/resources/docs/schema/jboss-as-mail_1_1.xsd

                               

                              You might want to hammer away in a Java SE test program until you get the sending to work, then switch to resource injection on the mail session

                               

                              If you @Resource inject the mail session, you shouldn't need the module

                              • 12. Re: How to configuration javamail with jboss 7.1.1final
                                lafr

                                As Nicklas said, it might be a good idea to create a standalone application first and then switch over to implement in the server environment.

                                 

                                From my standalone-full.xml:


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

                                <mail-session jndi-name="java:/Mail" from="user@domain.de" debug="false">

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

                                <login name="......" password="......"/>

                                </smtp-server>

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

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

                                <login name="....." password="......"/>

                                </imap-server>

                                </mail-session>

                                </subsystem>

                                You might want to change debug to true to see more information about what happens.

                                 

                                At the end of standalone-full.xml you have to define or modify the socket-bindings used for smtp, imap, pop3:

                                    <outbound-socket-binding name="mail-smtp">
                                        <remote-destination host="smtp.1und1.de" port="25"/>
                                    </outbound-socket-binding>
                                    <outbound-socket-binding name="mail-pop3">
                                        <remote-destination host="pop.1und1.de" port="110"/>
                                    </outbound-socket-binding>
                                    <outbound-socket-binding name="mail-imap">
                                        <remote-destination host="imap.1und1.de" port="143"/>
                                    </outbound-socket-binding>