4 Replies Latest reply on Sep 11, 2009 6:20 AM by luizhenrique

    Seam Email to Multi-recipients.

    fpsdyl
      Hi Guys,

      I am having problems with sending an email to multiple users. Here is my Email Action :

      `
      @Stateful
      @Name("emailNotifier")
      @Scope(ScopeType.EVENT)
      public class EmailNotifierAction implements EmailNotifier{
          @Logger Log log;

          @In(create=true)
          private Renderer renderer;
         
          UserDAO udao = null;

          @PersistenceContext(type=PersistenceContextType.EXTENDED)
          EntityManager em;

          public void send(){
                 renderer.render("/statsEmailReport.xhtml");
          }
         
          @Remove
          public void destroy() {
          }

          public void makeAndSendJavaConStatsToAll() {
                  log.info("makeAndSendJavaConStatsToAll()");
                  renderer.render("/statsEmailReport.xhtml");
          }

         
          public List<User_E> getEmailUsers(){
          List<User_E> emailUsers=new ArrayList<User_E>();
              log.info("make email users!");
            udao = new UserDAO(em);
              List<User_E> allUsers = udao.fetchUserList();
              for(User_E user : allUsers){
                  if (user.getNotifyStats() == Boolean.TRUE){
                      log.info(new StringBuffer().append(user.getFullname()).append(" -> wants this email! -->").append(user.getEmail()).toString());
                      emailUsers.add(user);
                  }
                  else{
                      log.info(new StringBuffer().append(user.getFullname()).append(" -> doesn't wants this email!").toString());
                  }
              }
              return emailUsers;
          }
      }
      `
      Here is the message that is supposed to be sent :


      <m:message xmlns="http://www.w3.org/1999/xhtml"
          xmlns:m="http://jboss.com/products/seam/mail"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:ui="http://java.sun.com/jsf/facelets">

          <m:from name="NGNDemography" address="ngnDemography@noreply.com" />
          <ui:repeat value="#{emailNotifier.getEmailUsers()}" var="user">`
              <m:to name="#{user.getFullname()}" address="#{user.getEmail()}" />
          </ui:repeat>
          <m:subject>Seam Email</m:subject>
          <m:body>
              <p><h:outputText value="Dear Guy" /></p>
              <p>You can try out Seam by visiting<a href="http://labs.jboss.com/jbossseam">http://labs.jboss.com/jbossseam</a>.</p>
              <p>Regards,</p>
              <p>Super Dylan</p>
          </m:body>
      </m:message>


      So basically to send the email I have this on a seperate page :

      <h:commandLink value="send test mail" action="#{emailNotifier.makeAndSendJavaConStatsToAll()}"/>


      My smtp host is setup correctly:

      This is the stacktrace that I get :
      `

      Caused by: java.lang.NullPointerException
              at javax.mail.internet.InternetAddress.checkAddress(InternetAddress.java:873)
              at javax.mail.internet.InternetAddress.validate(InternetAddress.java:856)
              at org.jboss.seam.mail.ui.AddressComponent.getInternetAddress(AddressComponent.java:39)
              at org.jboss.seam.mail.ui.RecipientAddressComponent.encodeBegin(RecipientAddressComponent.java:25)
              at javax.faces.component.UIComponent.encodeAll(UIComponent.java:934)
              at com.sun.facelets.component.RepeatRenderer.encodeChildren(RepeatRenderer.java:50)
              at com.sun.facelets.component.UIRepeat.process(UIRepeat.java:357)
              at com.sun.facelets.component.UIRepeat.encodeChildren(UIRepeat.java:617)
              at org.jboss.seam.ui.util.JSF.renderChild(JSF.java:175)
              at org.jboss.seam.ui.util.JSF.renderChildren(JSF.java:163)
              at org.jboss.seam.mail.ui.UIMessage.encodeChildren(UIMessage.java:192)
              at org.jboss.seam.ui.util.JSF.renderChild(JSF.java:175)
              at org.jboss.seam.ui.util.JSF.renderChildren(JSF.java:163)
              at org.jboss.seam.ui.facelet.RendererRequest.renderFacelet(RendererRequest.java:103)
              at org.jboss.seam.ui.facelet.RendererRequest.run(RendererRequest.java:73)
              at org.jboss.seam.ui.facelet.FaceletsRenderer.render(FaceletsRenderer.java:43)
              at za.co.ngn.central.session.stats.EmailNotifierAction.makeAndSendJavaConStatsToAll(EmailNotifierAction.java:52)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:585)
              at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
              at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
              at org.jboss.seam.intercept.EJBInvocationContext.proceed(EJBInvocationContext.java:44)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
              at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:77)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.persistence.EntityManagerProxyInterceptor.aroundInvoke(EntityManagerProxyInterceptor.java:29)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.persistence.HibernateSessionProxyInterceptor.aroundInvoke(HibernateSessionProxyInterceptor.java:31)
      `

      Please do you have any help for me?

      Thank you in advance!
        • 1. Re: Seam Email to Multi-recipients.
          fpsdyl

          Ok , I found this in my log file :


          2008-12-30 22:04:51,807 INFO  [za.co.ngn.central.session.Authenticator] Dylan Janeke -> wants this email! -->dylancjaneke@gmail.com
          2008-12-30 22:04:51,807 DEBUG [org.jboss.seam.Component] instantiating Seam component: org.jboss.seam.core.interpolator
          2008-12-30 22:04:51,807 DEBUG [org.jboss.seam.Component] initializing new instance of: org.jboss.seam.core.interpolator
          2008-12-30 22:04:51,807 DEBUG [org.jboss.seam.Component] done initializing: org.jboss.seam.core.interpolator
          2008-12-30 22:04:51,807 INFO  [za.co.ngn.central.session.Authenticator] Neil Dippenaar -> wants this email! -->neil.dippenaar@ngn.co.za
          2008-12-30 22:04:51,807 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.preRemoveVariable.emailUsers
          2008-12-30 22:04:51,807 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.postRemoveVariable.emailUsers
          



          I moved the method into my authenticator class to test something this is the method :


          
          public List<User_E> getEmailUsers(){
          
          



          as listed above.


          After each log it says preremov and postremove... I don't know if this is making sense to anyone .. but this problem is really annoying me that i can't send a simple friggin email... :-(

          • 2. Re: Seam Email to Multi-recipients.
            fpsdyl

            Ok I made some progress..


            I changed the method :


            public void makeAndSendJavaConStatsToAll() {
                        log.info("makeAndSendJavaConStatsToAll()");
                        renderer.render("/statsEmailReport.xhtml");
            }
            
            


            To do this :


            public String makeAndSendJavaConStatsToAll() {
                        log.info("makeAndSendJavaConStatsToAll()");
                        return "/statsEmailReport.xhtml";
            }
            



            So it redirects and renders the page inside of the browser instead of using the injected renderer.
            And it works!
            BUT I need to be able to run this method off a cronJob in Quartz, so I need to be able to render it outside of the browser... At least this should narrow down the error. Please if any one has any ideas I would really appreciate it.

            • 3. Re: Seam Email to Multi-recipients.
              luizhenrique

              I dont know the solution yet, but the ui:repeat dont work, so the email address is null then this exception occurs.


              read the documentation at 21.1.5. Templating



              if you send your mail asynchrounously Seam Mail doesn't have access to the full JSF or Servlet context, and so doesn't know about web.xml configuration parameters).


              If you do need more configure Facelets or JSF when sending mail, you'll need to override the Renderer component and do the configuration programmatically - only for advanced users!


              • 4. Re: Seam Email to Multi-recipients.
                luizhenrique

                One solution is


                public void makeAndSendJavaConStatsToAll() {
                    for(User_E user : allUsers){
                            Contexts.getEventContext().set("user", user);
                            renderer.render("/statsEmailReport.xhtml");
                    }
                }





                the message:



                <m:message xmlns="http://www.w3.org/1999/xhtml"
                    xmlns:m="http://jboss.com/products/seam/mail"
                    xmlns:h="http://java.sun.com/jsf/html"
                    xmlns:ui="http://java.sun.com/jsf/facelets">
                
                    <m:from name="NGNDemography" address="ngnDemography@noreply.com" />
                    <m:to name="#{user.getFullname()}" address="#{user.getEmail()}" />
                    
                    <m:subject>Seam Email</m:subject>
                    <m:body>
                        <p><h:outputText value="Dear Guy" /></p>
                        <p>You can try out Seam by visiting<a href="http://labs.jboss.com/jbossseam">http://labs.jboss.com/jbossseam</a>.</p>
                        <p>Regards,</p>
                        <p>Super Dylan</p>
                    </m:body>
                </m:message>