6 Replies Latest reply on Oct 14, 2010 1:21 PM by David Neiman

    Question on Mail Attachment

    bsgcic Newbie

      I followed the directions in Samuel Mendenhall's article How To Upload and Download Files in Seam and got file upload and download to work. However, I cannot seem to actually attach the uploaded file to an m:message object. I do not appear to receive any errors either. The email is sent but without the attachment. I can display the #{attachment.contentTypeStr}  #{attachment.nameStr} #{attachment.sizeInt} and even the #{attachment.dataBytAry} one which looks like the following: [B@1e11ff8



      I would greatly appreciate any suggestions.


      plain.xhtml


      ...
      
      <m:message 
           xmlns:m="http://jboss.com/products/seam/mail" 
           charset="UTF-8"
           importance="normal" 
           precedence="normal">
      <m:from ... /><m:replyTo .../><m:to ...><m:subject>...</m:subject>
      <m:attachment value="#{attachment.dataBytAry}"
           contentType="#{attachment.contentTypeStr}"
           fileName="#{attachment.nameStr}"/>
      <m:body type="plain">...</m:body>
      </m:message>
      
      



      contact.xhtml


      <h:form id="contactFormSubmissionForm" 
           enctype="multipart/form-data">
      <fieldset>
      ...
      <s:fileUpload id="file" 
           data="#{attachment.dataBytAry}"
           contentType="#{attachment.contentTypeStr}"
           fileName="#{attachment.nameStr}"
           fileSize="#{attachment.sizeInt}"/>
      ...
      
      



      Attachment.java


      private Long id;
      private String nameStr;
      private int sizeInt;
      private String contentTypeStr;
      private byte[] dataBytAry;
      
      ...
      
      @Lob
      @Column(name="dataInt", length = 2147483647)
      @Basic(fetch = FetchType.LAZY)
      public byte[] getDataBytAry()
      { return dataBytAry; }
      
      public void setDataBytAry(byte[] dataBytAry)...
      
      ...
      



      ContactFormSubmissionAction.java


      @Stateful
      @Name("contactFormSubmission")
      @Scope(EVENT)
      @TransactionAttribute(REQUIRES_NEW)
      public class ContactFormSubmissionAction implements ContactFormSubmission
      ...
      @PersistenceContext
      private EntityManager entityManager;
      
      @In(create=true)
      private MailEngine mailEngine;
      
      @In(create=true)
      private Attachment attachment;
      
      ...
      
      public void register() {
      ...
      entityManager.persist(attachment);
      
      ...
      
      mailEngine.sendPlain();
      }
      
      



      MailEngine.java


      ...
      public void sendPlain() {
      ...
      renderer.render("/mail/plain.xhtml");
      ...
      




      Thank you


      Jeff

        • 1. Re: Question on Mail Attachment
          Thomas Küstermann Newbie

          If I remember correctly, <m:attachment> must be the first tag within <m:message> as of Seam 2.2.0.GA:


          <m:message 
               xmlns:m="http://jboss.com/products/seam/mail" 
               charset="UTF-8"
               importance="normal" 
               precedence="normal">
          <m:attachment value="#{attachment.dataBytAry}"
               contentType="#{attachment.contentTypeStr}"
               fileName="#{attachment.nameStr}"/>
          <m:from ... /><m:replyTo .../><m:to ...><m:subject>...</m:subject>
          <m:body type="plain">...</m:body>
          </m:message>
          



          Hope that helps!

          • 2. Re: Question on Mail Attachment
            bsgcic Newbie

            Thomas,


            Thank you very kindly. I tried that but it still did not work. Actually, no attachments at all seam to work. I copied and pasted in the attachment code from the mail example which I also have integrated into my site and those did not work from the page. The do work from the examples page thought. It is a bit strange.


            Pasted in from the mail example:


            <m:attachment value="/mail/jboss.jpg" status="jbossLogo" disposition="inline" /> 
            <m:attachment value="#{numbers}" />
            




            Any other thoughts or suggestions?


            Thanks,


            Jeff

            • 3. Re: Question on Mail Attachment
              Cody Lerum Apprentice

              can you paste the message source from the email client to pastbin or gist.github.com?

              • 4. Re: Question on Mail Attachment
                bsgcic Newbie

                Cody, I was not exactly sure what you meant by the source from the email client.


                I got attachments to partially work but am still not sure what the original issue was. I can now attach to email after making a copy of the attachment.xhtml file in the mail example and pasting in my modifications from my original mail template file.


                However, I am having issues with files with Japanese file names. When I attach the files, the Japanese portion of the filenames get garbled.


                For example, consider when nameStr = "日本語でのファイル.jpg".  It chokes.


                <m:attachment value="#{attachment.dataBytAry}"
                contentType="#{attachment.contentTypeStr}"
                fileName="#{attachment.nameStr}"/>
                



                I have tried adding and using the following alternative names in the attachment entity class and setting the filename to those. However, the filename is not displayed as the Japanese filename.


                this.setNameSjis(this.nameStr.getBytes("Shift_JIS"));
                this.setNameISO(new String(this.nameSjis, "ISO8859_1"));
                this.setNameUrlEncoded(java.net.URLEncoder.encode(this.nameStr, "Shift-JIS"));
                
                



                I have also tried adding the following to the message template:


                <?xml version="1.0" encoding="UTF-8"?>
                <m:message
                 ...
                 charset="UTF-8">
                



                English filenames work but not the Japanese ones.


                Any thoughts or suggestions?


                Thank you kindly,


                Jeff



                • 5. Re: Question on Mail Attachment
                  bsgcic Newbie

                  Ok folks,


                  For anyone who encounters the same difficulty with Japanese file names, this is the solution that I finally found which works:


                  On the attachment.java entity class, I added the following:


                  ...
                  import javax.mail.internet.MimeUtility;
                  ...
                  private String nameMimeUtilityEncodedStr;
                  ...
                  
                  public void setNameStr(String nameStr) {
                  ...
                  try {
                   ... 
                   this.setNameMimeUtilityEncodedStr(
                     MimeUtility.encodeText(this.nameStr, "ISO-2022-JP", "B")); }
                  catch (UnsupportedEncodingException e1) {e1.printStackTrace();}
                  ...
                  } //end of method: public void setNameStr(String nameStr)
                  
                  ...
                  @Transient
                  normal getter and setter for nameMimeUtilityEncodedStr
                  
                  



                  In the mail template, my m:attachment is as follows:


                  <m:attachment value="#{attachment.dataBytAry}"
                  contentType="#{attachment.contentTypeStr}"
                  fileName="#{attachment.nameMimeUtilityEncodedStr}"/>
                  



                  Hope that helps others.


                  Best regards,


                  Jeff

                  • 6. Re: Question on Mail Attachment
                    David Neiman Newbie

                    Thomas, after LOTS of frustration not being able to get an attachment to work, I must concur that having the <m:attachment.../> tag as the first tag within the <m:message> resulted in the attachment working. I don't believe this is stated in the Seam docs. Besides, I can find almost nothing on the usage of the attributes for the various mail tags.