0

How do I attach files (for example, workflow packageItems files) to an email? I tried to use this code, but the files are not being attached. What am I doing wrong? How to attach a file to the email?enter image description here

1.SendEmailDelegate

            /*MailWithAttachmentActionExecuter*/
            ActionService actionService = getServiceRegistry().getActionService();
            Action mailAction = actionService.createAction(MailWithAttachmentActionExecuter.NAME);
            mailAction.setParameterValue(MailWithAttachmentActionExecuter.PARAM_SUBJECT, SendEmailDelegate.SUBJECT);
            mailAction.setParameterValue(MailWithAttachmentActionExecuter.PARAM_TO_MANY, recipients);
            mailAction.setParameterValue(MailWithAttachmentActionExecuter.PARAM_FROM, SendEmailDelegate.FROM_ADDRESS);
            mailAction.setParameterValue(MailWithAttachmentActionExecuter.PARAM_TEXT, sb.toString());

            List<NodeRef> attachements = new ArrayList<>();         
            NodeRef workflowPackage = ((ActivitiScriptNode) task.getVariables().get("bpm_package")).getNodeRef();
            //TODOD add noderefs to attachements list... 

            if (workflowPackage != null) {
                NodeService nodeService = getServiceRegistry().getNodeService();

                List<ChildAssociationRef> assocs = nodeService.getChildAssocs(workflowPackage);
                NodeRef[] docs = new NodeRef[assocs.size()];
                if (assocs.size() != 0) {
                    int index = 0;
                    for (ChildAssociationRef assoc : assocs) {
                        docs[index] = assoc.getChildRef();
                        attachements.add(assoc.getChildRef());
                        index++;
                        getServiceRegistry().getPermissionService().setInheritParentPermissions(assoc.getChildRef(), false);
                        getServiceRegistry().getPermissionService().setPermission(assoc.getChildRef(), PermissionService.ALL_AUTHORITIES, PermissionService.CONSUMER, true);
                        getServiceRegistry().getPermissionService().setInheritParentPermissions(assoc.getChildRef(), true);
                    }
                }
                mailAction.setParameterValue(MailWithAttachmentActionExecuter.PARAM_ATTACHMENTS, docs);

            }
            mailAction.setParameterValue(MailWithAttachmentActionExecuter.PARAM_SUBJECT, SendEmailDelegate.SUBJECT);
            mailAction.setParameterValue(MailWithAttachmentActionExecuter.PARAM_TEXT, sb.toString());
            //actionService.executeAction(mailAction, null);
            actionService.executeAction(mailAction, null, false, sendEmailAsynchronously);
            logger.debug("MailWithAttachmentActionExecuter executed");

2. MailWithAttachmentActionExecuter MailWithAttachmentActionExecuter

Gercio_J
  • 32
  • 7
  • I think you might need to override [the prepareEmail method](https://github.com/Alfresco/alfresco-repository/blob/master/src/main/java/org/alfresco/repo/action/executer/MailActionExecuter.java#L591) in a custom override bean, then in your custom code check for another param + attach that to the `MimeMessage` - can't see any extra params in the code that you can set by default – Gagravarr May 21 '20 at 02:44
  • **Hi** @Gagravarr! when I override the prepareEmail method, it presents me with the following error `ERROR [repo.action.AsynchronousActionExecutionQueueImpl] [defaultAsyncAction2] Failed to execute asynchronous action: Action[ id=7bcde69f-214b-4f61-b76a-100454c36e5a, node=null ] java.lang.NullPointerException at CustomMailActionExecuter.java:1345, CustomMailActionExecuter.java:1171,CustomMailActionExecuter.java:544,CustomMailActionExecuter.java:482` Sorry, can you please guide me more? – Gercio_J Jun 09 '20 at 16:11
  • Find what that line is in your custom code, and figure out what object is null? – Gagravarr Jun 10 '20 at 04:43
  • Thanks @Gagravarr, I solved the error. – Gercio_J Jun 10 '20 at 10:47

1 Answers1

0

You need to override Existing MailActionExecuter class with your own code where attachment will work.

Override Bean:

<bean id="custom-mail"  class="org.alfresco.MailActionExecuterWithAttachments" parent="action-executer"> 
    ......
</bean> 
Sanjay
  • 2,481
  • 1
  • 13
  • 28