Send Email -Workflow | Community
Skip to main content
New Participant
October 16, 2015
Solved

Send Email -Workflow

  • October 16, 2015
  • 24 replies
  • 6211 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

smacdonald2008
New Participant
October 16, 2015

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

New Participant
October 16, 2015

Justin, 

I wonder if this has been missed out in Documentation , but if one needs to send out email from AEM, on start up -Djava.net.preferIPv4Stack=true needs to be added.

Wish it was known earlier, Would be helpful for others trying to crack this issue. Thanks for help and patience , 

New Participant
October 16, 2015

No Scott, I am trying to create a workflow step mocking the Send Email functionality, Since not all my requirements might be met by OOTB component. 

smacdonald2008
New Participant
October 16, 2015

If OOTB does not address your needs -- then yes - you are correct - a custom workflow step is the way to go. Remember that a custom workflow step is just like any other OSGi bundle which is Java. So there is not much you cannot accomplish. If you want to get CQ users for you custom workflow step -- you can use the CQ com.day.cq.security.UserManager. API. To learn how to work with this API, see http://scottsdigitalcommunity.blogspot.ca/2013/07/using-ajax-requests-to-display-adobe-cq.html

New Participant
October 16, 2015

Scott, 

I tried using message gateway service to send out an email, in my custom workflow process step, but keep getting the error, a NPE, i have keyed in smtp port and username etc in CQ mail service configuration in system console. I see people posting issues on the same, please help me with this.

Code:

@Reference
private MessageGatewayService messageGatewayService;

     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("email") );
      email.setTo( emailRecipients );
      email.setSubject( "This is subject");
      email.setHtmlMsg( "Email testing");

Error:

messageGatewayService : com.day.cq.mailer.impl.CqMailingService@c14701
messageGateway : null
Fatal error while sending email in workflowjava.lang.NullPointerException

New Participant
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);

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

smacdonald2008
New Participant
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

New Participant
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

New Participant
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}