ACS commons sends mail to multiple recipients separately causing duplicate emails | Community
Skip to main content
November 12, 2024

ACS commons sends mail to multiple recipients separately causing duplicate emails

  • November 12, 2024
  • 3 replies
  • 1104 views

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(","));
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

3 replies

Sh1ju
Level 4
November 13, 2024

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

JaiSr1Author
November 19, 2024

unfortunately, this method

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

doesn't even exist

arunpatidar
Community Advisor
Community Advisor
November 13, 2024

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
JaiSr1Author
November 19, 2024

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. 

Manu_Mathew_
Community Advisor
Community Advisor
November 22, 2024

@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?

kautuk_sahni
Community Manager
Community Manager
November 25, 2024

@jaisr1 Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!

Kautuk Sahni