cancel
Showing results for 
Search instead for 
Did you mean: 

How to send mail with template from Java

dranakan
Champ on-the-rise
Champ on-the-rise
Hello,

I would like to send a mail from Java and using a template. How can I do this ?
There is code using a template in MailActionExecuter.java. How can I call it from a java class ?

Thank you.

Support
* Send a simple message : http://blog.algoworks.com/archives/255
* Send mail with template from javascript : https://forums.alfresco.com/en/viewtopic.php?f=53&t=42238&p=123441#p123441
6 REPLIES 6

jpotts
World-Class Innovator
World-Class Innovator
dranakan,

Actions can be invoked from Java code by leveraging the ActionService. Here is a snippet from an Activiti task listener that sends an email using the out-of-the-box mail action:
ActionService actionService = getServiceRegistry().getActionService();
Action mailAction = actionService.createAction(MailActionExecuter.NAME);
mailAction.setParameterValue(MailActionExecuter.PARAM_SUBJECT, ExternalReviewNotification.SUBJECT);       
mailAction.setParameterValue(MailActionExecuter.PARAM_TO, recipient);
mailAction.setParameterValue(MailActionExecuter.PARAM_FROM, ExternalReviewNotification.FROM_ADDRESS);
mailAction.setParameterValue(MailActionExecuter.PARAM_TEXT, sb.toString());
actionService.executeAction(mailAction, null);
If you want to use an FTL template for the email body, set PARAM_TEMPLATE to the node reference of the FTL template.

Jeff

karthickmani
Champ in-the-making
Champ in-the-making
Hi Jeff,

I have tried the above in version 4.1.8 and I am getting a null pointer exception. My part of code is below, Can you advise what is wrong here.

public class ValidateTMSDocuments
        implements NodeServicePolicies.OnUpdateNodePolicy
{
    private PolicyComponent policyComponent;
    private NodeService nodeService;
    private Behaviour onUpdateNode;
    private SearchService searchService;
    private FileFolderService fileFolderService;
    private SiteService siteService;
    private ServiceRegistry serviceRegistry;   
    public static final String MODEL_URI = "http://www.lateralminds.com/model/tmsDocs/1.0";
    public static final String CUSTOM_MODEL_URI = "http://www.alfresco.org/model/content/1.0";
    public void init()
    {
        this.onUpdateNode = new JavaBehaviour(this,"onUpdateNode",NotificationFrequency.TRANSACTION_COMMIT);
        this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI,"onUpdateNode"),
                QName.createQName(MODEL_URI,"docs"),this.onUpdateNode);
    }

    public void onUpdateNode(NodeRef nodeRef)
    {       
        ActionService actionService = getServiceRegistry().getActionService();             
        Action mailAction = actionService.createAction(MailActionExecuter.NAME);       
        mailAction.setParameterValue(MailActionExecuter.PARAM_SUBJECT, "Call doesn't exist");
        mailAction.setParameterValue(MailActionExecuter.PARAM_TO, "kmani@bsa.com.au");
        mailAction.setParameterValue(MailActionExecuter.PARAM_FROM, "alfresco@bsa.com.au");
        mailAction.setParameterValue(MailActionExecuter.PARAM_TEXT, "Call doesn't exist in Pronto");
        actionService.executeAction(mailAction, null);       
    }

    public PolicyComponent getPolicyComponent() {
        return policyComponent;
    }

    public void setPolicyComponent(PolicyComponent policyComponent) {
        this.policyComponent = policyComponent;
    }

    public NodeService getNodeService() {
        return nodeService;
    }

    public void setNodeService(NodeService nodeService) {
        this.nodeService = nodeService;
    }

    public SiteService getSiteService() {
        return siteService;
    }

    public void setSiteService(SiteService siteService) {
        this.siteService = siteService;
    }

    public SearchService getSearchService() {
        return searchService;
    }

    public void setSearchService(SearchService searchService) {
        this.searchService = searchService;
    }

    public FileFolderService getFileFolderService() {
        return fileFolderService;
    }

    public void setFileFolderService(FileFolderService fileFolderService) {
        this.fileFolderService = fileFolderService;
    }

    public ServiceRegistry getServiceRegistry() {
        return serviceRegistry;
    }
    public void setServiceRegistry(ServiceRegistry serviceRegistry) {
        this.serviceRegistry = serviceRegistry;
    }
}

This is the message in the logs,

12:16:33,802 ERROR [org.springframework.extensions.webscripts.AbstractRuntime] Exception from executeScript - redirecting to status template error: 04150535 Failed to execute transaction-level behaviour public abstract void org.alfresco.repo.node.NodeServicePolicies$OnUpdateNodePolicy.onUpdateNode(org.alfresco.service.cmr.repository.NodeRef) in transaction 613276c4-81d0-4462-8444-6b4703d474d6

dranakan
Champ on-the-rise
Champ on-the-rise
Thank you Jeff,

Here your sample with parameters for template.


      ActionService actionService = serviceRegistry.getActionService();
      Action mailAction = actionService.createAction(MailActionExecuter.NAME);
      mailAction.setParameterValue(MailActionExecuter.PARAM_SUBJECT, subject);      
      mailAction.setParameterValue(MailActionExecuter.PARAM_TO, to);
      mailAction.setParameterValue(MailActionExecuter.PARAM_FROM, from);
      mailAction.setParameterValue(MailActionExecuter.PARAM_TEXT, body);
      
      // Define Model
      String templatePATH = "PATH:\"/app:company_home/app:dictionary/app:email_templates/cm:workflownotification/cm:wf-custom-remind.ftl\"";
      ResultSet resultSet = serviceRegistry.getSearchService().query(new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore"), SearchService.LANGUAGE_LUCENE, templatePATH);
      if (resultSet.length()==0){
         logger.error("Template "+ templatePATH+" not found.");
         return;
      }
      NodeRef template = resultSet.getNodeRef(0);
      mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE, template);
      // Define parameters for the model (set fields in the ftl like : args.workflowTitle)
      Map<String, Serializable> templateArgs = new HashMap<String, Serializable>();
      templateArgs.put("workflowTitle","a");
      templateArgs.put("workflowPooled", false);
      templateArgs.put("workflowPriority", 1);
      templateArgs.put("workflowDescription", "d");
      templateArgs.put("workflowId",1);
      Map<String, Serializable> templateModel = new HashMap<String, Serializable>();
      templateModel.put("args",(Serializable)templateArgs);
      mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE_MODEL,(Serializable)templateModel);
      
      actionService.executeAction(mailAction, null);

krups
Champ in-the-making
Champ in-the-making
Thanks
This has helped me.

dzdarek
Champ in-the-making
Champ in-the-making
Thank you Jeff and dranakan. Very useful sample.

tgebeyehu
Champ in-the-making
Champ in-the-making
i was following this and it works fine. but in my work i need to attach document with the email i am sending how can i do that.
for example the document can be a file that is uploaded to the site