alfrescoalfresco-webscriptsalfresco-maven

How to attach a file to emails? Alfresco


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


Solution

  • 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>