6 Replies Latest reply on Aug 13, 2009 4:32 AM by walterjwhite

    Multiple Mail Sessions

    walterjwhite

      Hi all,


      I would like my web application to be able to send email using multiple email addresses.  For instance, I report all exceptions using my support email address and site notifications using another.  I tried placing multiple mail session definitions in my components.xml declaration, but it always ends up using one.  Even if my email says to use a certain from address, it ends up using another one, whichever mail session wins.


      Is there a way to specify which users sends the email?  Can it simply be linked to the from user?



      Thanks,
      Walter

        • 1. Re: Multiple Mail Sessions
          kapitanpetko

          Are you using the <m:message> tag to send email? If so, there is a <m:from> that should allow you to set the sender.
          As far as sessions are concerned, if you set the session you want to use in the request scope under the name session
          before you call render, you should be able to force Seam mail to use your session. You'll have to create your Session
          in code though, not in components.xml. Something like:


          // create and configure myMailSession
          Contexts.getEventContext().set("session", myMailSession);
          renderer.render("/email.xhtml");
          


          • 2. Re: Multiple Mail Sessions
            walterjwhite

            Hi Nikolay,


            Thanks for your reply.


            Yes I am using the m:message tag to send email.  I was asking if Seam was intelligent enough to see if a session was declared with that email address to automatically send email from that session, but it appears not yet.


            I will do some more reading to see if I can override how the mail session is injected and stored.


            How do most enterprise applications support sending email from multiple addresses?



            Thanks,
            Walter

            • 3. Re: Multiple Mail Sessions
              kapitanpetko

              Walter White wrote on Aug 13, 2009 00:24:


              Yes I am using the m:message tag to send email.  I was asking if Seam was intelligent enough to see if a session was declared with that email address to automatically send email from that session, but it appears not yet.


              What do you mean by 'a session declared with that email address'? The relevant code just looks for a session and creates one
              if not found. Cf. UIMessage.java


              public Session getMailSession()
              {
                  if (session == null)
                  {
                     if (getValue("session") != null)
                     {
                        session = (Session) getValue("session");
                     }
                     else
                     {
                       session = MailSession.instance();
                     }
                  }
                  return session;
              }
              




              How do most enterprise applications support sending email from multiple addresses?


              Not sure what your exact requirements are, but the sender/from are properties of the message, not the session.
              As long as you have a valid session, you can set anything as the from field of your message.


              BTW, my personal opinion is that using an JSF component to send email is a hack, and should be avoided.
              It ties you to the JSF lifecycle/libs and your will run into all sorts of problems one you try to use it
              form JMS or asynchronous methods (how emails are usually sent...). This has admittedly been fixed, but still
              not a good idea IMHO. The templating you get by using JSF tags seems nice at first, but if you need
              email templates you'd better use a template engine, or do some regex substitutions for simpler stuff.





              • 4. Re: Multiple Mail Sessions
                walterjwhite

                Hi Nikolay,


                What I am saying is that I have 2 sessions declared and want the session that gets injected to be used based on the from email address in the message.


                For instance, I have a session declared for a support user, and a session declared for the notification email that general users receive.  If I'm sending a notification email, I want it to be from the notification user, likewise, if I'm sending an error message to admins, I want it to be the support account.


                I haven't had issues sending mail asynchronously, I am using it for sending error messages to the administrators when an unexpected exception occurs and am able to retrieve the URL the end user was visiting and any other Request-scoped information.  I added another email address to send mail from so that we could better track exceptions as opposed to general site notifications.



                Walter


                • 5. Re: Multiple Mail Sessions
                  kapitanpetko

                  Walter White wrote on Aug 13, 2009 04:13:


                  What I am saying is that I have 2 sessions declared and want the session that gets injected to be used based on the from email address in the message.

                  For instance, I have a session declared for a support user, and a session declared for the notification email that general users receive.  If I'm sending a notification email, I want it to be from the notification user, likewise, if I'm sending an error message to admins, I want it to be the support account.


                  I think I get the picture, but could you show your components.xml? You have two different sessions, because you need to authenticate
                  to your mail server, right?


                  Btw, Seam injection is by name and happens before the actual method call, so to do what you are trying to do, you need two different components, I think (or inject both sessions into one, but basically the same idea). Say:


                  public class SupportEmailSender {
                   @In
                   Session supportSession;
                  
                   void sendMail() {
                   }
                  }
                  
                  public class NotificationEmailSender {
                   @In
                   Session notificationSession;
                  
                   void sendMail() {
                   }
                  }
                  



                  If you use the injected session to create/send your messages directly, there should be no problems. If you use the UIMessage tag,
                  you need to re-inject your session under the name 'session'.


                  void sendMail() {
                   Contexts.getEventContext().set("session", notificationSession);
                   renderer.render("/mymessage.xhtml");
                  }
                  


                  • 6. Re: Multiple Mail Sessions
                    walterjwhite

                    Thanks again, that is the simple way to get it up and running - I'll think about it some more to see if it would make sense to develop a factory or something with some logic to determine which one to inject.


                    Since I specify the emailTemplate I'm using through a maven property, I might as well specify one to determine which session is used.



                    Walter