EmailBean from Tony McGuckin not receiving attachments after saving and reopening document

I am using emailBean from Tony McGuckin to send an HTML email.

It works great until you save and open the document; at this point the bean finds no attachment.

Consider this scenario:

  • Create a document and save it as a draft
  • Open the document again and click Submit Action, where it calls the logic for the emailBean

Running the bean on debugmode I can see it printing Adding attachments ...

But he doesn't get the app here EmbeddedObject eo = this.getDocument().getDocument().getAttachment(persistentName);

If I try to print persistentName

I get null

This is emailBean:

            // add attachments....
            final List<FileRowData> attachments = this.getDocument().getAttachmentList(this.getFieldName());
            if(null != attachments && !attachments.isEmpty()){
              if(this.isDebugMode()){
                System.out.println("Adding Attachments...");
              }
                for(FileRowData attachment : attachments) {
                emailRootChild = emailRoot.createChildEntity();
                if(null != emailRootChild && attachment instanceof AttachmentValueHolder){
                  InputStream is = null;
                  try {                   

                    String persistentName = ((AttachmentValueHolder)attachment).getPersistentName();
                    String cid = ((AttachmentValueHolder)attachment).getCID();

                    //Here is printing null for persistentName
                    System.out.println("Attachment: " + persistentName);

                    EmbeddedObject eo = this.getDocument().getDocument().getAttachment(persistentName);
                    if(null != eo){
                      emailHeader = emailRootChild.createHeader("Content-Disposition");
                      emailHeader.setHeaderVal("attachment; filename=\"" + persistentName + "\"");
                      emailHeader = emailRootChild.createHeader("Content-ID");
                      emailHeader.setHeaderVal("<" + cid + ">");
                      is = eo.getInputStream();
                      Stream stream = session.createStream();
                      stream.setContents(is);
                      emailRootChild.setContentFromBytes(stream, attachment.getType(), MIMEEntity.ENC_IDENTITY_BINARY);
                      if(this.isDebugMode()){
                        System.out.println("Added Attachment : " + persistentName);
                      }
                    }
                  } catch (Exception e) {
                    if(this.isDebugMode()){
                      System.out.println("Adding Attachment failed : " + e.getMessage());
                    }
                    throw e;
                  } finally {
                    if(null != is){
                      is.close();
                      is = null;
                    }
                  }
                }
                }
              if(this.isDebugMode()){
                System.out.println("Completed Adding Attachments");
              }
            }

      

+3


source to share


1 answer


Attachments in the AttachmentValueHolder field are either persistent or not. A new app that hasn't been saved yet will have persistentName

. Otherwise he will have name

. You can add the following line to determine that:

if(StringUtil.isEmpty(persistentName)) {
    persistentName=((AttachmentValueHolder)attachment).getName();
}

      



PS. Therefore, if persistentName is NULL, it will no longer be a persistent name. I didn't change the variable name so as not to ruin your code :)

+1


source







All Articles