4 Replies Latest reply on Mar 27, 2009 9:15 PM by John Eckhart

    Excel attachment in email

    Zoltan Tiringer Newbie

      Hi all,


      I am trying to send emails with Excel attachment with Seam. Even though the excel file does get generated and can be dowloaded when invoked as a view, the same view doesn't work in an email. Only this much gets rendered in the attachment:



      <html xmlns="http://www.w3.org/1999/xhtml">
      </html>




      After some search in the in the forums, I suspect that this is a rendering problem with Seam Mail and Facelets. However, when I try to use FaceletsRenderer (which is supposed to resolve the problem), I get the same result.


      Is it me or Seam indeed doesn't work with Excel attachments? Is there any workaround (with or without Seam)? Any code sample would be highly appreciated!


      Thank you in advance

        • 1. Re: Excel attachment in email
          Zoltan Tiringer Newbie

          Is there any version of Seam in which mail with Facelets attachments works?

          • 2. Re: Excel attachment in email
            Nicklas Karlsson Master

            Hmm, can't remember when I last tried. Look for the mail and pdf examples and see if you can get it working with a PDF attachment - the Excel attachment is a copy&paste solution of that.

            • 3. Re: Excel attachment in email
              Zoltan Tiringer Newbie

              Hi Nicklas,


              Yes, the mail example works for me, actually that's what I used as a template...


              I even tried today to send as an attachment a PDF with various EL expressions that I display in my application, and it worked, too.


              Could it be that some Excel tags are not supported? Though I don't think so, because it should be generated when FaceletsRenderer.run is called, the same as when I just do the plain file download, and it works there...


              • 4. Re: Excel attachment in email
                John Eckhart Newbie

                Hi Niklas, Zoltan,


                   I think the problem lies in the mail attachment code actually. The code has a special case to handle pdf files in order to force the JSF to render and prevent the normal redirect (which instructs the browser to download the file).


                In src/mail/org/jboss/seam/mail/ui/UIAttachment.java


                ...
                   @Override
                   public void encodeBegin(FacesContext context) throws IOException
                   {
                      if (this.getChildCount() > 0) {
                         if (Reflections.isInstanceOf(this.getChildren().get(0).getClass(), "org.jboss.seam.pdf.ui.UIDocument")) 
                         {
                            Method method = Reflections.getSetterMethod(this.getChildren().get(0).getClass(), "sendRedirect");
                            Reflections.invokeAndWrap(method, this.getChildren().get(0), false);
                            JSF.renderChildren(context, this);
                         } else {
                            setValue(encode(context).getBytes());
                            if (getContentType() == null) {
                               // User hasn't specified content, assume html
                               setContentType("text/html");
                            }
                         }
                      }
                   }
                ...
                

                  


                when I change this to:


                ...
                   @Override
                   public void encodeBegin(FacesContext context) throws IOException
                   {
                      if (this.getChildCount() > 0) {
                         if (Reflections.isInstanceOf(this.getChildren().get(0).getClass(), "org.jboss.seam.pdf.ui.UIDocument")) 
                         {
                            Method method = Reflections.getSetterMethod(this.getChildren().get(0).getClass(), "sendRedirect");
                            Reflections.invokeAndWrap(method, this.getChildren().get(0), false);
                            JSF.renderChildren(context, this);
                         } else if (Reflections.isInstanceOf(this.getChildren().get(0).getClass(), "org.jboss.seam.excel.ui.UIWorkbook")) 
                         {
                            Method method = Reflections.getSetterMethod(this.getChildren().get(0).getClass(), "sendRedirect");
                            Reflections.invokeAndWrap(method, this.getChildren().get(0), false);
                            JSF.renderChildren(context, this);
                         } else {
                            setValue(encode(context).getBytes());
                            if (getContentType() == null) {
                               // User hasn't specified content, assume html
                               setContentType("text/html");
                            }
                         }
                      }
                   }
                ...
                



                then the email sends properly and the attachment is correct (I've tested this on 2.1.0 GA.


                Looking at the code, however, I get the sense that this is sub-optimal. It appears that you would only be able to attach either one pdf OR one spreadsheet, plus, the funny handling of the redirect seems better suited for a clause in the JSF than special handling in the code. This will also require an update if we would like to attach another new type rendered in JSF in the future (maybe a graph or an odf doc??).


                I'll open an issue in JIRA (with patch) for the immediate problem as I have tested it a bit, but I'ld like to see some discussion about the other concern and how it could be fixed correctly.