1 Reply Latest reply on Mar 11, 2014 3:52 AM by mreis

    Asynchronous Mail with attachments

    mattdavies

      I'm trying to send an asychronous email with Seam and am running into problems when I try to use the attachment feature.


      I am generating a PDF using iText and creating a byte array.  I put the array into the event context, and when I try to print the array in the email I get the array reference so it appears it is in the event context.


      So, the problem seems to be with the Mime attachment.  Here's what I have.


      Method for sending email


      @Asynchronous
          public void sendEmailGiftCertificate(@Duration long delay, EmailMessageStruct emailMessageStruct) {
             try {
                  GiftCertificatesToPDF g = new GiftCertificatesToPDF();
                     GiftCertInfoStruct params = new GiftCertInfoStruct();
                     params.setEmail("matt@test.com");
                     g.setParams(params);
                     ByteArrayOutputStream baos = g.generatePDF();
                     byte[] contents = baos.toByteArray();
                     System.out.println("Size: " + contents.length );
                     Contexts.getEventContext().set("attachment",contents);
                     
                     baos.close();
                     String orderId = emailMessageStruct.getData();
                    AccountingTransactions xact = em.find(AccountingTransactions.class, orderId);
                     Contexts.getEventContext().set("struct", emailMessageStruct);
                    Contexts.getEventContext().set("xact", xact);
                       
                     Renderer.instance().render("/shop/checkout/includes/emailGiftCertificate.xhtml");
             } catch (Exception e) {
                e.printStackTrace();
             }
          }



      Email snip



      <m:attachment value="#{attachment}" />



      And a portion of the stack trace



      17:06:20,437 ERROR [STDERR] java.lang.NullPointerException
      17:06:20,439 ERROR [STDERR]     at javax.mail.internet.MimeUtility.quote(MimeUtility.java:852)
      17:06:20,439 ERROR [STDERR]     at javax.mail.internet.ParameterList.quote(ParameterList.java:334)
      17:06:20,439 ERROR [STDERR]     at javax.mail.internet.ParameterList.toString(ParameterList.java:304)
      17:06:20,439 ERROR [STDERR]     at javax.mail.internet.ContentDisposition.toString(ContentDisposition.java:176)
      17:06:20,439 ERROR [STDERR]     at javax.mail.internet.MimeBodyPart.setFileName(MimeBodyPart.java:1137)
      17:06:20,439 ERROR [STDERR]     at javax.mail.internet.MimeBodyPart.setFileName(MimeBodyPart.java:534)
      17:06:20,439 ERROR [STDERR]     at org.jboss.seam.mail.ui.UIAttachment.encodeEnd(UIAttachment.java:168)
      17:06:20,439 ERROR [STDERR]     at org.jboss.seam.ui.util.JSF.renderChild(JSF.java:181)
      17:06:20,439 ERROR [STDERR]     at org.jboss.seam.ui.util.JSF.renderChildren(JSF.java:163)
      17:06:20,439 ERROR [STDERR]     at org.jboss.seam.mail.ui.MailComponent.encode(MailComponent.java:85)
      17:06:20,439 ERROR [STDERR]     at org.jboss.seam.mail.ui.MailComponent.encode(MailComponent.java:59)
      17:06:20,439 ERROR [STDERR]     at org.jboss.seam.mail.ui.UIBody.encodeChildren(UIBody.java:44)
      17:06:20,439 ERROR [STDERR]     at org.jboss.seam.ui.util.JSF.renderChild(JSF.java:175)
      17:06:20,439 ERROR [STDERR]     at org.jboss.seam.ui.util.JSF.renderChildren(JSF.java:163)
      17:06:20,439 ERROR [STDERR]     at org.jboss.seam.mail.ui.UIMessage.encodeChildren(UIMessage.java:192)
      17:06:20,439 ERROR [STDERR]     at org.jboss.seam.ui.util.JSF.renderChild(JSF.java:175)
      17:06:20,439 ERROR [STDERR]     at org.jboss.seam.ui.util.JSF.renderChildren(JSF.java:163)
      17:06:20,439 ERROR [STDERR]     at org.jboss.seam.ui.facelet.RendererRequest.renderFacelet(RendererRequest.java:103)
      17:06:20,439 ERROR [STDERR]     at org.jboss.seam.ui.facelet.RendererRequest.run(RendererRequest.java:73)
      17:06:20,439 ERROR [STDERR]     at org.jboss.seam.ui.facelet.FaceletsRenderer.render(FaceletsRenderer.java:43)
      



      Anyone know how to do an async email with a byte array for an attachment? Is there a better way?



      Thanks!