Expand my Community achievements bar.

SOLVED

When i change the metadata property of an asset the email notification workflow should trigger notify the members id administrators group with custom email template

Avatar

Level 1

Hi community, 

 

i am working on a custom process step that fetches the administrators group members emails and send the custom email template , i am able to fetching the members mails and template but getting the error org.apache.commons.mail.emailException:Invalid message supplied. The below is the process step code i am working on and the smtp configurationsScreenshot 2024-10-18 104125.pngScreenshot 2024-10-21 105305.png

public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) {
ResourceResolver resourceResolver = null;
try {
// Obtain ResourceResolver from the session
resourceResolver = getResourceResolver(workflowSession);
String payloadPath = workItem.getWorkflowData().getPayload().toString();
Resource resource = resourceResolver.getResource(payloadPath);

// The workflow is triggered due to a specific metadata change, so no need to check if the metadata changed
if (resource != null && resource.isResourceType("nt:unstructured")) {
log.info("Workflow triggered for asset: {}", payloadPath);

// Fetch email addresses of the administrators group members
String[] emails = getAdminEmails(resourceResolver);
if (emails.length > 0) {
// Parameters for the email template
Map<String, String> emailParams = new HashMap<>();
emailParams.put("senderEmail", "noreply@day.com");
emailParams.put("assetPath", payloadPath);
emailParams.put("message", "Metadata has been changed for the asset.");

// Email template path
String emailTemplatePath = "/etc/notification/email/metadata-change-template.html"; // Adjust this path as needed

// Send the email
sendEmail(resourceResolver, messageGatewayService, emailTemplatePath, emailParams, emails);
} else {
log.warn("No email addresses found for administrators.");
}
} else {
log.warn("Resource at path {} is not of type dam:Asset", payloadPath);
}
} catch (Exception e) {
log.error("Error in Metadata Change Notification Process", e);
}
}

private ResourceResolver getResourceResolver(WorkflowSession workflowSession) throws Exception {
Map<String, Object> authInfo = Collections.singletonMap(
ResourceResolverFactory.SUBSERVICE, "providence-user"
);
return resourceResolverFactory.getServiceResourceResolver(authInfo);
}
private String[] getAdminEmails(ResourceResolver resourceResolver) {
List<String> emailList = new ArrayList<>();
try {
// Fetch UserManager from ResourceResolver
UserManager userManager = resourceResolver.adaptTo(UserManager.class);
// Fetch the group using the group ID
Authorizable authorizable = userManager.getAuthorizable(ADMINISTRATORS_GROUP_ID);

if (authorizable.isGroup()) {
Group userGroup = (Group) authorizable;
Iterator<Authorizable> membersList = userGroup.getMembers();
while (membersList.hasNext()) {
Authorizable member = membersList.next();
if (member != null && !member.isGroup()) { // Ensure it's a user and not another group
javax.jcr.Value[] usrEmail = member.getProperty("./profile/email");

if (usrEmail != null && usrEmail.length > 0) {
for (javax.jcr.Value emailValue : usrEmail) {
String email = emailValue.getString();
log.info("Email for member {}: {}", member.getID(), email);
if (email != null && !email.isEmpty()) {
emailList.add(email); // Add email to the list
}
}
} else {
log.warn("No email found for member: {}", member.getID());
}
}
}
} else {
log.warn("Group not found: {}", "GroupId");
}
} catch (RepositoryException e) {
log.error("Error fetching administrator emails", e);
}

return emailList.toArray(new String[0]);
}

public static void sendEmail(ResourceResolver resourceResolver, MessageGatewayService messageGatewayService, String template, Map<String, String> parameters, String[] emails) throws IOException, MessagingException, EmailException {
MailTemplate mailTemplate = MailTemplate.create(template, resourceResolver.adaptTo(Session.class));
MessageGateway<HtmlEmail> messageGateway = messageGatewayService.getGateway(HtmlEmail.class);
HtmlEmail email;
email = mailTemplate.getEmail(StrLookup.mapLookup(parameters), HtmlEmail.class);
List<InternetAddress> emailRecipients = new ArrayList<>();
for (String addEmail : emails) {
emailRecipients.add(new InternetAddress(addEmail));
}
email.setTo(emailRecipients);
messageGateway.send(email);
}
}
1 Accepted Solution

Avatar

Correct answer by
Level 4
1 Reply

Avatar

Correct answer by
Level 4

Hi @Venkata_NarayanaBa 

Hope you have already identified the solution for the above, if not please find below the article related to the similar error if it helps

https://stackoverflow.com/questions/61271903/org-apache-commons-mail-emailexception-invalid-message-...

https://adobe-consulting-services.github.io/acs-aem-commons/features/e-mail/email-api/index.html

 

Thanks