[CQ5.4] Workflow process to send an email using MessageGateway

Bit of background:

If you are upgrading from CQ5.3 to CQ5.4, and you have a workflow process to send an email, you may have noticed that MailService class used in CQ5.3 has been deprecated since 5.4 and now we are supposed to use MessageGateway.  Below, is the code snippet which shows on how to use MessageGateway to send an email.

Know your basics:

Create an OSGI Bundle as described here:

Refer CQ5 docs on defining a process step using Java class here:

Custom workflow process step to send an email using MessageGateway:

import java.util.ArrayList;

import javax.mail.internet.InternetAddress;

import org.apache.commons.mail.HtmlEmail;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.mailer.MessageGateway;
import com.day.cq.mailer.MessageGatewayService;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.WorkflowProcess;
import com.day.cq.workflow.metadata.MetaDataMap;

 /**
 * @scr.component metatype="false" immediate="true"
 * @scr.service interface="com.day.cq.workflow.exec.WorkflowProcess"
 *
 * @scr.property name="service.description" value=
 *               "This is the workflow step for sending email notification"
 * @scr.property name="service.vendor" value="MyApp"
 * @scr.property name="process.label"
 *               value="MyApp Send Email Notification"
 */

public class MySendEmailWorkflowProcess implements WorkflowProcess {

    private static final Logger log = LoggerFactory
            .getLogger(MySendEmailWorkflowProcess.class);

     ArrayList<InternetAddress> emailRecipients = new ArrayList<InternetAddress>();

    // ----< public configuration values >--------------------------------------

 	/** @scr.reference policy="dynamic" */
 	private MessageGateway<HtmlEmail> messageGateway;

 	/** @scr.reference policy="static" */
 	private MessageGatewayService messageGatewayService;

    public void execute(WorkItem item, WorkflowSession session,
			MetaDataMap metaData) throws WorkflowException {

    	HtmlEmail email = new HtmlEmail();

         try{
              emailRecipients.add(new InternetAddress("my_email_id@mycorp.com") );
              email.setTo( emailRecipients );
              email.setSubject( "This is subject");
              email.setHtmlMsg( "Email testing is on..");

              //Check the logs to see that messageGatewayService is not null
              log.info("messageGatewayService : " + messageGatewayService);

              messageGateway = messageGatewayService.getGateway(HtmlEmail.class);

              //Check the logs to see that messageGateway is not null
              log.info("messageGateway : " + messageGateway);

              messageGateway.send( email );
             }
          catch ( Exception e ) {
              e.printStackTrace();
              log.error( "Fatal error while sending email in workflow", e );
          }

     }  

    }

Before you test the above code, make sure you have configured SMTP as described below:
1. Go to Felix console –> Configuration Or the following URL (with correct host and port information)

http://<host>:<port>/system/console/configMgr

2. Edit the following:

‘Day CQ Mail Service’

3. Fill-in appropriate SMTP values.

(You may use Gmail as SMTP server with the following configuration):

 That’s it

Now, this workflow process should appear in the list of custom workflow steps inside the ‘Process Step’ component as shown below and you should be able to send your emails.

 

Troubleshooting:

  1. messageGatewayService is null
  2. SMTP configuration is missing
  3. OSGI bundle is missing/outdated in Felix console