From cc6f4db4ca422515d55cfea6f46626941327bc48 Mon Sep 17 00:00:00 2001 From: Mathias Vandaele Date: Fri, 22 Nov 2024 15:41:30 +0100 Subject: [PATCH] feat(email-connector): add document handling, multiple attachment sending possible --- .../outbound/JakartaEmailActionExecutor.java | 24 ++++++++++++++++++- .../protocols/actions/SmtpSendEmail.java | 10 ++++---- .../client/jakarta/JakartaExecutorTest.java | 13 +++++----- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/connectors/email/src/main/java/io/camunda/connector/email/client/jakarta/outbound/JakartaEmailActionExecutor.java b/connectors/email/src/main/java/io/camunda/connector/email/client/jakarta/outbound/JakartaEmailActionExecutor.java index c7710eb2cc..a68fabbc8d 100644 --- a/connectors/email/src/main/java/io/camunda/connector/email/client/jakarta/outbound/JakartaEmailActionExecutor.java +++ b/connectors/email/src/main/java/io/camunda/connector/email/client/jakarta/outbound/JakartaEmailActionExecutor.java @@ -28,6 +28,7 @@ import jakarta.mail.util.ByteArrayDataSource; import java.io.IOException; import java.util.*; +import java.util.function.Consumer; public class JakartaEmailActionExecutor implements EmailActionExecutor { @@ -280,6 +281,12 @@ private SendEmailResponse smtpSendEmail( attachment.setDataHandler(new DataHandler(dataSource)); attachment.setFileName(smtpSendEmail.attachment().metadata().getFileName()); multipart.addBodyPart(attachment); + Multipart multipart = new MimeMultipart(); + MimeBodyPart textContent = new MimeBodyPart(); + textContent.setText(smtpSendEmail.body()); + multipart.addBodyPart(textContent); + if (!Objects.isNull(smtpSendEmail.attachments())) { + smtpSendEmail.attachments().forEach(getDocumentConsumer(multipart)); } message.setContent(multipart); try (Transport transport = session.getTransport()) { @@ -287,7 +294,7 @@ private SendEmailResponse smtpSendEmail( transport.sendMessage(message, message.getAllRecipients()); } return new SendEmailResponse(smtpSendEmail.subject(), true); - } catch (MessagingException | IOException e) { + } catch (MessagingException e) { throw new RuntimeException(e); } } @@ -387,6 +394,21 @@ private Optional createParsedInternetAddresses(Object object) }); } + private Consumer getDocumentConsumer(Multipart multipart) { + return document -> { + try { + BodyPart attachment = new MimeBodyPart(); + DataSource dataSource = + new ByteArrayDataSource(document.asInputStream(), document.metadata().getContentType()); + attachment.setDataHandler(new DataHandler(dataSource)); + attachment.setFileName(document.metadata().getFileName()); + multipart.addBodyPart(attachment); + } catch (IOException | MessagingException e) { + throw new RuntimeException(e); + } + }; + } + private List createDocumentList( List attachments, OutboundConnectorContext connectorContext) { return attachments.stream() diff --git a/connectors/email/src/main/java/io/camunda/connector/email/outbound/protocols/actions/SmtpSendEmail.java b/connectors/email/src/main/java/io/camunda/connector/email/outbound/protocols/actions/SmtpSendEmail.java index e8748d281e..b15c24e553 100644 --- a/connectors/email/src/main/java/io/camunda/connector/email/outbound/protocols/actions/SmtpSendEmail.java +++ b/connectors/email/src/main/java/io/camunda/connector/email/outbound/protocols/actions/SmtpSendEmail.java @@ -12,6 +12,7 @@ import io.camunda.document.Document; import jakarta.validation.Valid; import jakarta.validation.constraints.NotNull; +import java.util.List; import java.util.Map; @TemplateSubType(id = "sendEmailSmtp", label = "Send Email") @@ -129,11 +130,12 @@ public record SmtpSendEmail( @TemplateProperty( label = "Attachment", group = "sendEmailSmtp", - id = "attachmentSmtp", + id = "attachmentsSmtp", + tooltip = "Email's attachments, should be set as a list ", type = TemplateProperty.PropertyType.String, feel = Property.FeelMode.required, optional = true, - description = "Email's attachment", - binding = @TemplateProperty.PropertyBinding(name = "data.smtpAction.attachment")) - Document attachment) + description = "Email's attachment. e.g., =[ document1, document2]", + binding = @TemplateProperty.PropertyBinding(name = "data.smtpAction.attachments")) + List attachments) implements SmtpAction {} diff --git a/connectors/email/src/test/java/io/camunda/connector/email/client/jakarta/JakartaExecutorTest.java b/connectors/email/src/test/java/io/camunda/connector/email/client/jakarta/JakartaExecutorTest.java index 95dcfb8c0e..27857fbbff 100644 --- a/connectors/email/src/test/java/io/camunda/connector/email/client/jakarta/JakartaExecutorTest.java +++ b/connectors/email/src/test/java/io/camunda/connector/email/client/jakarta/JakartaExecutorTest.java @@ -195,13 +195,14 @@ void executeSmtpSendEmailWithAttachment() throws MessagingException, IOException when(smtpSendEmail.body()).thenReturn(body); when(smtpSendEmail.htmlBody()).thenReturn(bodyAsHtml); try (FileInputStream fileInputStream = new FileInputStream("src/test/resources/img/img.png")) { - when(smtpSendEmail.attachment()) + when(smtpSendEmail.attachments()) .thenReturn( - this.documentFactory.create( - DocumentCreationRequest.from(fileInputStream) - .contentType(ContentType.IMAGE_PNG.getMimeType()) - .fileName("test") - .build())); + List.of( + this.documentFactory.create( + DocumentCreationRequest.from(fileInputStream) + .contentType(ContentType.IMAGE_PNG.getMimeType()) + .fileName("test") + .build()))); } when(session.getTransport()).thenReturn(transport);