Skip to content

Commit

Permalink
feat(email-connector): add document handling, multiple attachment sen…
Browse files Browse the repository at this point in the history
…ding possible
  • Loading branch information
mathias-vandaele committed Nov 28, 2024
1 parent 439b9c4 commit cc6f4db
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -280,14 +281,20 @@ 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()) {
this.jakartaUtils.connectTransport(transport, authentication);
transport.sendMessage(message, message.getAllRecipients());
}
return new SendEmailResponse(smtpSendEmail.subject(), true);
} catch (MessagingException | IOException e) {
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
Expand Down Expand Up @@ -387,6 +394,21 @@ private Optional<InternetAddress[]> createParsedInternetAddresses(Object object)
});
}

private Consumer<Document> 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<Document> createDocumentList(
List<EmailAttachment> attachments, OutboundConnectorContext connectorContext) {
return attachments.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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<Document> attachments)
implements SmtpAction {}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit cc6f4db

Please sign in to comment.