10 Replies Latest reply on Oct 11, 2010 9:14 AM by Sheldon du Trevou

    Seam email with multiple recipients and attachment

    Bram Biesbrouck Newbie
      I think I discovered a bug in the Seam Email system (checking here before creating a JIRA-issue):

      When an email with multiple recipients is sent (using the ui:repeat-approach), and you attach a file to it, the second recipient gets the attachment twice, the third recipient three times, etc

      Here's the code of the email:


      <ui:repeat value="#{emailRecipients}" var="recipient">
           
           <m:message urlBase="...">
                <m:from name="#{fromName}" address="#{fromAddress}"/>
                <m:tp name="#{recipient.name}" address="#{recipient.address}"/>
                <m:subject>#{subject}</m:subject>

                <ui:repeat value="#{attachments}" var="attachment">
                     <m:attachment value="#{attachment}"/>
                </ui:repeat>

                <m:body>
                     ...
                </m:body>
           </m:message>

      </ui:repeat>

      Is this a bug, or am I doing something wrong?

      b.
        • 1. Re: Seam email with multiple recipients and attachment
          Arbi Sookazian Master
          <m:attachment value="/WEB-INF/lib/jboss-seam-mail.jar" fileName="this-is-so-cool.jar"/>



          why are you looping for the attachment?  same attachment for all emails, right?


          how many attachment files are you attaching?  assuming more than one.  then I'd assume all recipients get all attachments.

          • 2. Re: Seam email with multiple recipients and attachment
            Bram Biesbrouck Newbie

            Yes indeed, same attachment for all emails, but sometimes there's more than one (the email-code is a template).


            But the attachments get doubled by every recipient.
            More concretely: if there's one attachment file1:


            - recipient 1 gets: file1
            - recipient 2 gets: file1, file1
            - recipeint 3 gets: file1, file1, file1


            and so on.


            Not what you'd excpect...

            • 3. Re: Seam email with multiple recipients and attachment
              Arbi Sookazian Master

              I tried this as a POC with Seam 2.0.2-FP and was not able to reproduce.


              I did run into the following problem with JavaMail and/or our exchange server:


              My Link


              So if I use my internal email for 3 test recipients, I get 3 emails to my work email with the same attachment.


              Here is the code:


              TestEmailAttachments.java


              @Name("testEmailAttachments")
              public class TestEmailAttachments implements Serializable {
                   
                   //@In(create=true)
                   //private TestRecipients recipients;
                   
                   @DataModel
                   private List<TestRecipients> emailRecipients;
                   
                   @Factory("emailRecipients")
                   public void getEmailRecipients()
                   {
                        emailRecipients = new ArrayList<TestRecipients>();
                        
                        TestRecipients r1 = new TestRecipients("Arbi_cox", "arbi.sookazian@cox.com");
                        TestRecipients r2 = new TestRecipients("Arbi_cox", "arbi.sookazian@cox.com");
                        TestRecipients r3 = new TestRecipients("Arbi_cox", "arbi.sookazian@cox.com");
                        //TestRecipients r2 = new TestRecipients("Arbi_cox", "a_sookazian@yahoo.com");
                        //TestRecipients r3 = new TestRecipients("Arbi_gmail", "asookazian@gmail.com");
                        
                        
                        emailRecipients.add(r1);
                        emailRecipients.add(r2);
                        emailRecipients.add(r3);
                   }
              }



              testEmailAttachments.xhtml:


              <ui:repeat value="#{emailRecipients}" var="recipient">
                                <m:message>
                                    <m:from name="Peter" address="arbi.sookazian@cox.com" />
                                    <m:to name="#{recipient.name}" address="#{recipient.address}"/>
                                    <m:subject>this is the subject</m:subject>
                                  
                                       <!-- 
                                    <ui:repeat value="#{attachments}" var="attachment">
                                     <m:attachment value="#{attachment}"/>
                                    </ui:repeat>
                                     -->
                                  
                                  <m:attachment value="/index.html"/>
                                  
                                    <m:body>
                                      this is the body
                                    </m:body>
                              </m:message>
                        
                        </ui:repeat>




              TestRecipients.java:


              @Name("recipients")
              public class TestRecipients {
                        private String name;
                        private String address;
                        
                        public TestRecipients(){}
                        
                        public TestRecipients(String name, String address){
                             this.name = name;
                             this.address = address;
                        }
                        
                        public String getName() {
                             return name;
                        }
                        public void setName(String name) {
                             this.name = name;
                        }
                        public String getAddress() {
                             return address;
                        }
                        public void setAddress(String address) {
                             this.address = address;
                        }
                        
                        
                   }

              • 4. Re: Seam email with multiple recipients and attachment
                Bram Biesbrouck Newbie

                Arbi,


                In addition to your TestRecipients, I have also this getter:



                public File[] getAttachments()
                {
                    return new File[] {this.xmlFile};
                }




                Which returns a reference to an xml file on disk.
                Could you please try it in your testcase (with any random file)?
                Don't forget to uncomment the <ui:repeat value="#{attachments}" var="attachment"> ... part


                Individual <m:attachment> statements did work for me too, but not dynamic ones.

                • 5. Re: Seam email with multiple recipients and attachment
                  Klaasjan te Voortwis Newbie

                  I ran into this too inside a ui:repeat, i solved it using the code below (mention the status and rendered attribs)


                  <m:attachment
                          value="#{org.jboss.seam.core.resourceLoader.getResourceAsStream('/css/img/voorjekind_emailbanner.jpg')}"
                          contentType="image/jpeg"
                          fileName="banner2.jpg"
                          status="banner"
                          disposition="inline" 
                          rendered="#{banner == null}" />



                  • 7. Re: Seam email with multiple recipients and attachment
                    Bram Biesbrouck Newbie
                    Btw, the suggested solution only works when you only send one mail per event...
                    When you send more, the attachment isn't rendered/attached.

                    "no silver bullet comes to mind" ;-)
                    • 8. Re: Seam email with multiple recipients and attachment
                      Turion Danae Newbie

                      I know this topic is quite old, but i ran into the same problem and the above posts don't seam to point out a solution ?


                      I'm using Seam 2.2.0.GA and sending mails with multiple attachments works as well as sending mails to multipe recipients. Though a combination of both does not work, recipient one gets N attachments, recipient two gets 2N attachments, recipient three gets 3N attachments and so on. My email.xhtml looks like this:




                      <ui:repeat value="#{email.receivers}" var="receiver" 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:message>
                      
                           <m:from name="#{email.sender.firstName} #{email.sender.lastName}" address="my@mail.com" />
                           <m:to name="#{receiver.firstName} #{receiver.lastName}">#{receiver.email}</m:to>
                           <m:subject>#{email.subject}</m:subject>
                      
                          <m:body>
                              <h:outputText value="This mail has the following attachments: #{email.attachmentsToString()}"
                                   rendered="#{email.attachments.size > 0}" />
                      
                           <ui:repeat value="#{email.attachments}" var="a" rendered="#{email.attachments.size > 0}">
                                <m:attachment value="#{a.data}" contentType="#{a.contentType}" fileName="#{a.name}"/>
                           </ui:repeat>
                           
                          </m:body>
                      
                      </m:message>
                      </ui:repeat>





                      While the body correctly outputs This mail has... file1.pdf, file2.pdf, etc., the last recipient gets a ton of attachments. Ist there already a solution for this problem? Otherwise I'm forced to render one mail after another, iterating over a list of Email-objects, since we definitely need to send multiple attachments per mail.


                      Thanks in advantage ;o)

                      • 10. Re: Seam email with multiple recipients and attachment
                        Sheldon du Trevou Newbie

                        Dont know if this will help anybody.  Was sending single attachment and it worked fine.  Multiple attachments would not send with the email.  If I used a ui:repeat to add multiple attachments to a mail I would have to convert the attachment data to a file first from byte array and then it would attach.