How to send an email with attachments? | Community
Skip to main content
Level 2
February 4, 2025
Solved

How to send an email with attachments?

  • February 4, 2025
  • 1 reply
  • 752 views

Together with other input text, from the form, I have to send an email and add the possibility of attaching a file coming from the DAM. How can I do it?

Best answer by konstantyn_diachenko

Hi @vodjakxa ,

 

Using ACS AEM Commons EmailService, we can simplify the email-sending process in AEM as a Cloud Service (AEMaaCS) while maintaining best practices. The EmailService provided by ACS Commons allows us to send emails with dynamic variables and attachments easily. Read more: https://adobe-consulting-services.github.io/acs-aem-commons/features/e-mail/email-api/index.html

 

The sendMail(..) method now supports the following signature:

public List<String> sendEmail(String templatePath, Map<String, String> emailParams, Map<String, DataSource> attachments, String... recipients) public List<InternetAddress> sendEmail(String templatePath, Map<String, String> emailParams, Map<String, DataSource> attachments, InternetAddress... recipients)


E-mail attachments can be passed in via the attachments parameter.

 

... Map<String, DataSource> attachments = new HashMap<>(); String attachmentContentsAsString = "This text should be in the attache txt file." attachments.put("attachment1.txt", new ByteArrayDataSource(attachmentContentsAsString, "text/plain")); ... List<String> participantList = emailService.sendEmail(htmlEmailTemplatePath, emailParams, attachments, recipients);

 

Best regards,

Kostiantyn Diachenko.

1 reply

konstantyn_diachenko
Community Advisor
konstantyn_diachenkoCommunity AdvisorAccepted solution
Community Advisor
February 4, 2025

Hi @vodjakxa ,

 

Using ACS AEM Commons EmailService, we can simplify the email-sending process in AEM as a Cloud Service (AEMaaCS) while maintaining best practices. The EmailService provided by ACS Commons allows us to send emails with dynamic variables and attachments easily. Read more: https://adobe-consulting-services.github.io/acs-aem-commons/features/e-mail/email-api/index.html

 

The sendMail(..) method now supports the following signature:

public List<String> sendEmail(String templatePath, Map<String, String> emailParams, Map<String, DataSource> attachments, String... recipients) public List<InternetAddress> sendEmail(String templatePath, Map<String, String> emailParams, Map<String, DataSource> attachments, InternetAddress... recipients)


E-mail attachments can be passed in via the attachments parameter.

 

... Map<String, DataSource> attachments = new HashMap<>(); String attachmentContentsAsString = "This text should be in the attache txt file." attachments.put("attachment1.txt", new ByteArrayDataSource(attachmentContentsAsString, "text/plain")); ... List<String> participantList = emailService.sendEmail(htmlEmailTemplatePath, emailParams, attachments, recipients);

 

Best regards,

Kostiantyn Diachenko.

Kostiantyn Diachenko, Community Advisor, Certified Senior AEM Developer, creator of free AEM VLT Tool, maintainer of AEM Tools plugin.
konstantyn_diachenko
Community Advisor
Community Advisor
February 4, 2025

Asset data you can take using Asset API: 

Asset asset = damResource.adaptTo(Asset.class); InputStream is = asset.getOriginal().getStream();
Kostiantyn Diachenko, Community Advisor, Certified Senior AEM Developer, creator of free AEM VLT Tool, maintainer of AEM Tools plugin.