Send Email -Workflow | Community
Skip to main content
Level 9
October 16, 2015
Solved

Send Email -Workflow

  • October 16, 2015
  • 24 replies
  • 6215 views

Hello,

I would like to send email picking the template from AEM, and also dynamically adding values to the template using the workflow code. I would like to construct the "TO" list in an email dynamically, picking it up from a group of users(group A), and would like to insert a line, "URL" dynamically in the email template .txt file.

Please let me know.

Regards,

Nicole

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by JustinEd3

The problem is that you are embedding commons-email. Don't do this. Your bundle needs to import org.apache.commons. 

24 replies

Level 9
October 16, 2015

justin_at_adobe wrote...

The problem is that you are embedding commons-email. Don't do this. Your bundle needs to import org.apache.commons. 

 

Justin,

I totally get it when you say don't embed commons-email, i must say that the workflow executes just perfect, thanks, but am wondering if i should manually install a jar- commons-email in felix console and have it being imported by the bundle?

I was wondering if i was to install the commons-email jar in felix or not, if i am doing something right?.

Regards,

Adobe Employee
October 16, 2015

I was looking for the MANIFEST.MF file from the bundle.

Let's try something else... are you using <Embed-Dependency> in your pom.xml file?

smacdonald2008
Level 10
October 16, 2015

Are you asking if this use case is out of the box functionality or if it is possible? 

Level 9
October 16, 2015

Updated: Sure, I could send the xml under OSGI-INF under scr-plugin-generated folder, I have also attached the MANIFEST.MF file contents Please let me know if anything else is needed from me. I also see the bundle being part of references in the services tab for Message Gateway and Message Gateway interface. 

<?xml version="1.0" encoding="UTF-8"?><components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0">
    <scr:component immediate="true" name="com.test.workflow.email.EmailWorkflow">
        <implementation class="com.test.workflow.email.EmailWorkflow"/>
        <service servicefactory="false">
            <provide interface="com.day.cq.workflow.exec.WorkflowProcess"/>
        </service>
        <property name="service.description" value="Test Email workflow process implementation."/>
        <property name="service.vendor" value="Adobe"/>
        <property name="process.label" value="Test Email Workflow Process"/>
        <property name="service.pid" value="com.test.workflow.email.EmailWorkflow"/>
        <reference name="messageGateway" interface="com.day.cq.mailer.MessageGateway" cardinality="1..1" policy="static" bind="bindMessageGateway" unbind="unbindMessageGateway"/>
        <reference name="messageGatewayService" interface="com.day.cq.mailer.MessageGatewayService" cardinality="1..1" policy="static" bind="bindMessageGatewayService" unbind="unbindMessageGatewayService"/>
        <reference name="mailService" interface="com.day.cq.mailer.MailService" cardinality="1..1" policy="dynamic" bind="bindMailService" unbind="unbindMailService"/>
    </scr:component>
</components>

MANIFEST.MF

Manifest-Version: 1.0
Bnd-LastModified: 1379652279235
Build-Jdk: 1.7.0_21
Built-By: XXX
Bundle-Category: cq5
Bundle-ClassPath: .,commons-email-1.3.jar
Bundle-Description: Workflow to send email
Bundle-ManifestVersion: 2
Bundle-Name: EmailProcess
Bundle-SymbolicName: com.test.workflow.email
Bundle-Version: 1.0.0
Created-By: Apache Maven Bundle Plugin
Embed-Dependency: commons-email
Embedded-Artifacts: commons-email-1.3.jar;g="org.apache.commons";a="comm
 ons-email";v="1.3"
Export-Package: com.test.workflow.email;uses:="com.day.cq.workflow.metada
 ta,com.day.cq.mailer,javax.mail.internet,com.day.cq.workflow,javax.mail
 ,com.day.cq.workflow.exec,org.osgi.service.component";version="1.0.0",c
 om.test.email.constants;version="1.0.0"
Implementation-Title: Custom Email Workflow
Implementation-Vendor: Test
Implementation-Version: 1.0.1
Import-Package: com.day.cq.mailer,com.day.cq.workflow,com.day.cq.workflo
 w.exec,com.day.cq.workflow.metadata,javax.activation,javax.mail;version
 ="[1.4,2)",javax.mail.internet;version="[1.4,2)",javax.mail.util;versio
 n="[1.4,2)",javax.naming,org.osgi.service.component;version="[1.0,2)"
Service-Component: OSGI-INF/com.test.workflow.email.EmailWorkflow.xml
Tool: Bnd-1.50.0

Adobe Employee
October 16, 2015

Can you post your bundle's MANIFEST.MF file?

Level 9
October 16, 2015

Thanks Justin, I agree with you referred to an online link http://blogs.adobe.com/learningwem/2011/11/27/cq5-4-workflow-process-to-send-an-email-using-messagegateway/.

Now i get the below error with the code implemented. Please could you guide me along right path.

@Reference(policy = ReferencePolicy.DYNAMIC)
private MailService mailService;

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

public void execute(WorkItem item, WorkflowSession session,

    MetaDataMap metaData) throws WorkflowException {         

 HtmlEmail email = new HtmlEmail();

         emailRecipients.add(new InternetAddress("test@gmail.com") );

        email.setTo( emailRecipients );
        email.setSubject( "This is subject");
        email.setHtmlMsg( "Email testing is on..");

        mailService.send( email );

        System.out.println("Done");

}

 

Error: (at line  mailService.send( email );)

Caused by: java.lang.ClassCastException: org.apache.commons.mail.HtmlEmail cannot be cast to org.apache.commons.mail.Email

 

IMO , i read the api and see, would using messageGateway.send(htmlEmail) be a right thing to do?

     
voidsendEmail(org.apache.commons.mail.Email email) 
          Deprecated. since 5.4 use MessageGateway.send(Object) instead}
Level 9
October 16, 2015

Thank you will revert back with inputs, i wanted to use HTMLEmail. Please let me know if you find anything. I know this was working in 5.4 , and now in AEM 5.6 , there seems to be issues

smacdonald2008
Level 10
October 16, 2015

What about customizing the email service -- use your own email service in your own process step- always works nicely. 

I wrote an article on this a while back. I used the Java Mail API and wrapped that API within an OSGi. You can do that invoke it from your process step. It sent email messages from CQ without any issues at all. 

See: http://scottsdigitalcommunity.blogspot.ca/2012/07/creating-custom-cq-email-services.html

In the mean time -- i am going to dig in our bug base and see if there are issues with this other service. 
HTH

Adobe Employee
October 16, 2015

I don't see the code where you actually send the email.

The correct way to do this in a workflow process is to have

@Reference(policy = ReferencePolicy.DYNAMIC) private MailService mailService;

And then in the body of the execute() method (or some other method called from execute) do:

HtmlEmail email = //build the email if (mailService != null) { mailService.send(email); } else { logger.warn("Mail Service is not available. Check log for configuration issues."); } 

Rolling your own mail service is going to end up duplicating configuration as you need to configure the CQ MailService to get default notifications working in the first place.

Regards,

Justin

Level 2
October 16, 2015

You are getting null pointer exception. Refer services :

@Reference
private MessageGateway<HtmlEmail> messageGateway;

@Reference
private MessageGatewayService messageGatewayService;

and then :

messageGateway = messageGatewayService.getGateway(HtmlEmail.class);

messageGateway.send(email);