Expand my Community achievements bar.

SOLVED

How to send an email with attachments?

Avatar

Level 2

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?

1 Accepted Solution

Avatar

Correct answer by
Level 9

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.

View solution in original post

2 Replies

Avatar

Correct answer by
Level 9

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.

Asset data you can take using Asset API: 

Asset asset = damResource.adaptTo(Asset.class);
InputStream is = asset.getOriginal().getStream();