Expand my Community achievements bar.

ACS commons sends mail to multiple recipients separately causing duplicate emails

Avatar

Level 1

I'm using ACS commons to send mail like this. the EmailService is from ACS commons. Problem here is the service sends the mail to each recepient in toEmail separately while also having ccemail and bccrecpient in it. This causes mail to be tiggered to the ccemail and bccrecepient multiple times which is causing an issue since the bccrecepient triggers a request number in the backend and they are getting three different emails for the same request. Is there any way to send the email once to multiple recipients? 

 

Map<String, String> emailParams = new HashMap<>();
emailParams.put("subject", subject);
emailParams.put("ccEmail", ccEmail);
emailParams.put("bccrecipient", bccEmail);
emailParams.put("body", bodyText.toString());
Map<String, DataSource> attachments = new HashMap<>();
if (!(fileName.isEmpty() || excelString.isBlank())) {
try {
attachments.put(fileName, new ByteArrayDataSource(excelString, "text/plain"));
} catch (IOException e) {
log.info("Exception {} creating attachment", e);
}
}
List<String> failureList = emailService.sendEmail("/etc/notification/email/email-template.txt", emailParams, attachments, toEmail.split(","));
5 Replies

Avatar

Level 4

To ensure the email is sent only once to all recipients (including CC and BCC), you can consolidate the recipients into a single email send operation rather than looping through each toEmail individually. Try this 

Map<String, String> emailParams = new HashMap<>();
emailParams.put("subject", subject);
emailParams.put("body", bodyText.toString());

// Instead of adding ccEmail and bccEmail here, we will pass them directly to the email service call
Map<String, DataSource> attachments = new HashMap<>();
if (!(fileName.isEmpty() || excelString.isBlank())) {
try {
attachments.put(fileName, new ByteArrayDataSource(excelString, "text/plain"));
} catch (IOException e) {
log.info("Exception {} creating attachment", e);
}
}

// Convert comma-separated emails into a List<String> for recipients, cc, and bcc.
String[] toEmails = toEmail.split(",");
String[] ccEmails = ccEmail.isEmpty() ? new String[0] : ccEmail.split(",");
String[] bccEmails = bccEmail.isEmpty() ? new String[0] : bccEmail.split(",");

// Sending the email with EmailService to multiple recipients, cc, and bcc in a single call.
List<String> failureList = emailService.sendEmail(
"/etc/notification/email/email-template.txt",
emailParams,
attachments,
toEmails,
ccEmails,
bccEmails
);

Avatar

Level 1

unfortunately, this method

emailService.sendEmail(
"/etc/notification/email/email-template.txt",
emailParams,
attachments,
toEmails,
ccEmails,
bccEmails
);

doesn't even exist

Avatar

Community Advisor

Hi @JaiSr1 

I'm not sure if this issue is specific to your email client or if it applies to others as well.

In AEM, you can control where each recipient is placed (in CC or BCC), but ideally, the email provider should be able to detect duplicate recipients and ensure that only one email is sent to each.



Arun Patidar

Avatar

Level 1

So what would be the solution? Because the current acs email service method is flawed if it sends mails one-by-one to the recipient by copying cc and bcc. 

Avatar

Community Advisor

@JaiSr1 You could try writing a custom logic with AEM to remove duplicate recipients before including it to bcc and cc. Which SMTP services/provider are you using?