Expand my Community achievements bar.

Adobe Summit 2025: AEM Session Recordings Are Live! Missed a session or want to revisit your favorites? Watch the latest recordings now.
SOLVED

Facing issue in sending email via AEM Workflow Process Step

Avatar

Level 1
/*
* ***********************************************************************
* BOUNTEOUS CONFIDENTIAL
* ___________________
*
* Copyright 2021 Bounteous
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains the property
* of Bounteous and its suppliers, if any. The intellectual and
* technical concepts contained herein are proprietary to Bounteous
* and its suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Bounteous.
* ***********************************************************************
*/
package com.grantthornton.aem.base.core.workflow;

import com.adobe.acs.commons.email.EmailService;
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.day.cq.mailer.MessageGatewayService;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.ReplicationStatus;
import com.day.cq.replication.Replicator;
//import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.mailer.MessageGatewayService;

import javax.jcr.Session;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//import org.apache.commons.mail.HtmlEmail;



@Component(
service = WorkflowProcess.class,
property = {
"process.label" + "=Page Published",
Constants.SERVICE_DESCRIPTION + "=Sends a user new page published notification.",
Constants.SERVICE_VENDOR + "=Grant Thornton"
}
)
public class EmailNotification implements WorkflowProcess {
private static final Logger log = LoggerFactory.getLogger(EmailNotification.class);

@Reference
private MessageGatewayService messageGatewayService;

@Reference
private EmailService emailService;

@Reference
private Replicator replicator;

String emailTemplate = "/conf/global/settings/workflow/notification/email/default/pagepublishedemail.txt";

@Override
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
throws WorkflowException {

log.info("in the email notification");
ResourceResolver resolver = workflowSession.adaptTo(ResourceResolver.class);
Session session = workflowSession.adaptTo(Session.class);
String payloadPath = workItem.getWorkflowData().getPayload().toString();
//Resource payloadPage = resolver.getResource(payloadPath + "/jcr:content");
final Map<String, String> parameters = new HashMap<>();
parameters.put("subject", workItem.getNode().getDescription());
parameters.put("event.TimeStamp", workItem.getTimeStarted().toString());
parameters.put("item.data.comment", workItem.getMetaDataMap().get("comment", String.class));
parameters.put("item.node.title", workItem.getNode().getTitle());
parameters.put("model.title", workItem.getWorkflow().getWorkflowModel().getTitle());
parameters.put("payload.path.open" , workItem.getWorkflowData().getPayload().toString());

for (Map.Entry<String, String> entry : parameters.entrySet()) {
log.info("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
//Set the dynamic variables of your email template
Map<String, String> emailParams = new HashMap<String, String>();
emailParams.put("body", "hello there");

// Customize the sender email address - if required
emailParams.put("senderEmailAddress", "svc_aem@us.gt.com");
emailParams.put("senderName", "David Smith");

// Array of email recipients
String[] recipients = { "Neeraj.Chaudhary@us.gt.com"};

List<String> failureList = emailService.sendEmail(emailTemplate, emailParams, recipients);
if (failureList.isEmpty()) {
log.info("Email sent successfully to the recipients");
} else {
log.info("Email sent failed");
}
/*if (payloadPage == null) {
throw new WorkflowException("Unable to get the Resource for path" + payloadPath);
}*/
try {
replicator.replicate(session, ReplicationActionType.ACTIVATE, payloadPath);
ReplicationStatus status = replicator.getReplicationStatus(session, payloadPath);
if (status != null && status.isActivated()) {
//payloadPage.getValueMap().get('');
//status.

}
} catch (ReplicationException e) {
throw new WorkflowException("Error occured during replication", e);
}


}

}


Getting below error
2025-02-07 01:07:25.813 INFO [com.grantthornton.aem.base.core.workflow.EmailNotification] Key: model.title, Value: Publish Content New
2025-02-07 01:07:25.813 INFO [com.grantthornton.aem.base.core.workflow.EmailNotification] Key: payload.path.open, Value: /content/grantthornton/language-masters/en/test-we-speak
2025-02-07 01:07:25.813 INFO [com.grantthornton.aem.base.core.workflow.EmailNotification] Key: payload.path.open, Value: /content/grantthornton/language-masters/en/test-we-speak
07.02.2025 01:07:25.815 *WARN* [JobHandler: /var/workflow/instances/server0/2025-02-06_1/publish-content-new_45:/content/grantthornton/language-masters/en/test-we-speak] com.day.cq.commons.mail.MailTemplate Depcrecated method com.day.cq.commons.mail.MailTemplate#getEmail(StrLookup, Class) used. Please use getEmail(Map, Class) instead.
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @neera ,

You can try to use "ACS AEM Commons - E-mail Service" class with below function:

sendEmail(final String templatePath, final Map<String, String> emailParams,final InternetAddress... recipients) 
                                           

 

For more details on it you can take reference from below ACS commons class:
https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/main/java/com/ad...

Also in your implementation "getEmail" function shouldn't be deprecated according to document:-
https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/commons/ma...
Having said that if you want to go ahead with your approach then raise support ticket to understand why its saying as deprecated.

Hope it helps!


-Tarun

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @neera ,

You can try to use "ACS AEM Commons - E-mail Service" class with below function:

sendEmail(final String templatePath, final Map<String, String> emailParams,final InternetAddress... recipients) 
                                           

 

For more details on it you can take reference from below ACS commons class:
https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/main/java/com/ad...

Also in your implementation "getEmail" function shouldn't be deprecated according to document:-
https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/commons/ma...
Having said that if you want to go ahead with your approach then raise support ticket to understand why its saying as deprecated.

Hope it helps!


-Tarun

Avatar

Administrator

@neera Did you find the suggestion helpful? Please let us know if you need more information. If a response worked, kindly mark it as correct for posterity; alternatively, if you found a solution yourself, we’d appreciate it if you could share it with the community. Thank you!



Kautuk Sahni