jBPM provides a rich and customizable way of sending emails to multiple recepients.


In this blog, we'll discuss the various ways provided by jBPM to send mails, reminders and what not!


 

 

 

 

Inline mails

With inline mail sending, one can write the necessary information on the node itself.

The below example shows a mail node, on which all the parameters for sending mails are written in the process definition file itself.

 

<process name="InlineMail" xmlns="http://jbpm.org/4.4/jpdl">
  <start>
    <transition to="send birthday reminder note" />
  </start>
  <mail>
    <to addresses="jbpmLearner@some-company.com" />
    <subject>Reminder: ${person} celebrates his birthday!</subject>
    <text>Do not forget: ${date} is the birthday of ${person} </text>
    <attachments>
      <attachment resource="org/example/birthday_card.png"/>
      <attachment name="picture.jpg" expression="${picture}" mime-type="image/jpeg"/>
    </attachments>
    <transition to="end" />
  </mail>
  <state/>

 

Notice that mail details are written on the node itself.


Disadvantages: Any minor change in mail parameters require redeployment of the process definition, flexibility to change mail information is reduced.

 


Mail templates

To define mail as a template, we can use a mail node and configure it as:


<mail name="rectify" template="rectify-template />


And this template can be defined in a mail.templates.xml file (you can use any name for the templates file).

To specify that the above mail template should be used, the same should be imported in jbpm.cfg.xml file as:


<import resource="com/jbpm/demo/resources/mail.templates.xml" />


The templates file would be like:


<jbpm-configuration>
   
 <process-engine-context>

     <mail-template name="rectify-template">
               <to addresses="jbpmLearner@some-company.com" />
               <subject>Reminder: ${person} celebrates his birthday!</subject>
               <text>Do not forget: ${date} is the birthday of ${person} </text>
     <attachments>
           <attachment resource="org/example/birthday_card.png"/>
           <attachment name="picture.jpg" expression="${picture}" mime-type="image/jpeg"/>
     </attachments>
        
     </mail-template>

 </process-engine-context> 


To specify smtp details, a jbpm.mail.properties file is maintained. The file can contain parameters as below:


mail.smtp.host     gmail.com
mail.smtp.port     25
mail.from          demo-noreply@gmail.com

 


Sending mail using event listeners

To send mail at any point of time (on transition, nodes etc.), one can leverage event listeners provided by jBPM.

A sample example of a simple event listener for mail execution is as below:



import javax.mail.Message;
import org.jbpm.api.Execution;
import org.jbpm.pvm.internal.email.impl.MailProducerImpl;
import org.jbpm.pvm.internal.email.impl.MailTemplate;
import org.jbpm.pvm.internal.email.impl.MailTemplateRegistry;
import org.jbpm.pvm.internal.email.spi.MailProducer;
import org.jbpm.pvm.internal.email.spi.MailSession;
import org.jbpm.pvm.internal.env.EnvironmentImpl;

import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;



public class MyEventListener implements EventListener
{
        public void notify(EventListenerExecution execution) throws Exception
     {
         sendMail("my-email-templete", execution);
     }

     private void sendMail(String templateName, Execution execution)
     {
         MailTemplateRegistry mailTemplateRegistry = EnvironmentImpl.getFromCurrent(MailTemplateRegistry.class);
         MailSession mailSession = EnvironmentImpl.getFromCurrent(MailSession.class);
         MailTemplate template = templateRegistry.getTemplate(templateName);


         MailProducerImpl mailProducer = new MailProducerImpl();
         mailProducer.setTemplate(template);
         Collection<Message> emails = mailProducer.produce(execution);
        
         mailSession.send(emails);



Mails on transition

Even on transitions, mails can be sent as:


<transition to="Approve">
      <mail template="notify-birthday" />
</transition>

 


Up Ahead, Advanced mail support, and customizing address resolvers.

Happy learning !