6 Replies Latest reply on Mar 19, 2008 4:43 PM by pmuir

    Sending Email from an EAR deployed application

      Our application is deployed as a series of WARs in an EAR.  I have a situation in which I'm wanting to send the same email message from a UI trigger that can be hit from one of two separate WAR files.  We're using Seam's email templates - and love them - but I'm not sure what the best way to proceed is.


      Is there any way to set this up so that we can continue to use the .xhtml template approach and yet render the email from Java code running in either web application?


      Additionally, we may have times when another event (MDB for example) hits and should trigger an outbound email send.  How would we set things up so that this email could also be formatted using the .xhtml templates?


      Thanks!  Any help will be greatly appreciated.

        • 1. Re: Sending Email from an EAR deployed application
          keithnaas

          If all of the jars are in the EAR, it is 100% possible to do this from anywhere in your app. 


          Try the FaceletsRenderer component which states



          Implementation of Renderer using Facelets Especially useful for sending email using Seam Mail

          @In FaceletsRenderer renderer;
          
          public void foo() {
             renderer.render("/some/location/email.xhtml"); 
          }


          • 2. Re: Sending Email from an EAR deployed application

            Additionally, we may have times when another event (MDB for example) hits and should trigger an outbound email send.  How would we set things up so that this email could also be formatted using the .xhtml templates?


            AFAIK this cannot be achieved with Seam mail, because it tightly depends on view layer presence. You'll get No application context active etc. This is a huge limitation if you want to have general mailing services for batch processing notifications etc.


            Templating is great, but providing your own thin wrapper on the MAIL API is not a big deal really and brings no such limitations.


            After few attempts I switched to Spring mail support which can be used without this limitation and supports Velocity and Freemarker templates.

            • 3. Re: Sending Email from an EAR deployed application

              Thanks for the explanation, Przemyslaw.  That's rough.  It strikes me that the only real need is so that the .xhtml file (passed as the only parameter to renderer.render()) can be located.  Is that the case?  If so, can a static (String) template be used instead?


              Templated email is pretty important, but I always hate the idea of adding more framework code to replace existing framework code that almost works...


              Any chance one of the Seam insiders can weigh in on this?

              • 4. Re: Sending Email from an EAR deployed application
                pmuir

                It should probably work - just make sure the mail exists inside a full Seam app which has a view layer. But in general I need to rewrite the the Renderer so that it can work without JSF being active as Przemyslaw says - yes, you would still need the JSF libraries present, but that would be all.

                • 5. Re: Sending Email from an EAR deployed application

                  In this particular case, though, there are two WAR files and a number of other EJB JAR files in the EAR.  I'm trying to find out how I can specify a particular WAR to reference for the email template... either when called from an MDB or when accessed through a different WAR.


                  If the latter case sounds odd, one of the scenarios that I'm dealing with involves a product with an administrative application built into one WAR, and a user-facing front-end built in a different WAR (different templates, security, pretty much everything, but the same model and some common SSBs).  The user-facing application sends out a variety of confirmation emails, and I'm now having to add an email-resend functionality in to the administrative application.  I'd prefer to do this without duplicating all of the email templates, even during the build process.


                  More specifically I'm trying to understand how the seam Renderer is accessing those templates, so I don't accidentally think I have a solution that works but is fragile and may break across deployments.


                  Thanks again!

                  • 6. Re: Sending Email from an EAR deployed application
                    pmuir

                    It loads the from the the ResourceLoader which essentially loads them from the classpath. Look at the Seam source code for exactly which classloaders it tries.