Search in sources :

Example 6 with Mail

use of io.quarkus.mailer.Mail in project kogito-runtimes by kiegroup.

the class QuarkusMailSender method onDeadline.

@Incoming("kogito-deadline-consumer")
public void onDeadline(UserTaskDeadlineDataEvent event) {
    MailInfo mailInfo = MailInfo.of(event.getData());
    logger.info("Sending e-mail {}", mailInfo);
    Mail message = new Mail();
    if (mailInfo.to() != null) {
        message.setTo(Arrays.asList(mailInfo.to()));
    }
    if (mailInfo.from() != null) {
        message.setFrom(mailInfo.from());
    }
    if (mailInfo.replyTo() != null) {
        message.setReplyTo(mailInfo.replyTo());
    }
    if (mailInfo.subject() != null) {
        message.setSubject(mailInfo.subject());
    }
    if (mailInfo.body() != null) {
        message.setText(mailInfo.body());
    }
    mailer.send(message).subscribe().with(this::handleCompleted, this::handleFailure);
}
Also used : Mail(io.quarkus.mailer.Mail) Incoming(org.eclipse.microprofile.reactive.messaging.Incoming)

Example 7 with Mail

use of io.quarkus.mailer.Mail in project automatiko-engine by automatiko-io.

the class VerificationTest method testProcessExecutionV2.

@SuppressWarnings("unchecked")
@Test
public void testProcessExecutionV2() {
    String key = "test";
    String addPayload = "{\n" + "  \"email\": \"john@email.com\",\n" + "  \"person\": {\n" + "    \"adult\": true,\n" + "    \"age\": 30,\n" + "    \"name\": \"john\"\n" + "  },\n" + "  \"resume\": {\n" + "    \"attributes\": {\n" + "    },\n" + "    \"content\": \"aGVsbG8=\",\n" + "    \"name\": \"test.txt\"\n" + "  },\n" + "  \"coverLetter\": {\n" + "    \"attributes\": {\n" + "    },\n" + "    \"content\": \"aGVsbG8=\",\n" + "    \"name\": \"cover-letter.pdf\"\n" + "  }\n" + "}";
    given().contentType(ContentType.JSON).accept(ContentType.JSON).body(addPayload).when().post("/notifications?businessKey=" + key).then().statusCode(200).body("id", equalTo("test"), "extracted", nullValue());
    List<Mail> sent = mailbox.getMessagesSentTo("john@email.com");
    assertEquals(1, sent.size());
    Mail actual = sent.get(0);
    assertTrue(actual.getHtml().contains("<h1>Hello john</h1>"));
    assertEquals("Notification", actual.getSubject());
    assertEquals(1, actual.getAttachments().size());
    assertEquals("application/zip", actual.getAttachments().get(0).getContentType());
    assertEquals("documents.zip", actual.getAttachments().get(0).getName());
    given().accept(ContentType.JSON).when().get("/notifications").then().statusCode(200).body("$.size()", is(1));
    List<Map<String, String>> taskInfo = given().accept(ContentType.JSON).when().get("/notifications/" + key + "/tasks").then().statusCode(200).extract().as(List.class);
    assertEquals(1, taskInfo.size());
    String taskId = taskInfo.get(0).get("id");
    String taskName = taskInfo.get(0).get("name");
    assertEquals("validate", taskName);
    String payload = "{}";
    given().contentType(ContentType.JSON).accept(ContentType.JSON).body(payload).when().post("/notifications/" + key + "/" + taskName + "/" + taskId).then().statusCode(200).body("id", is(key), "extracted", notNullValue());
    given().accept(ContentType.JSON).when().get("/notifications").then().statusCode(200).body("$.size()", is(0));
}
Also used : Mail(io.quarkus.mailer.Mail) Map(java.util.Map) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 8 with Mail

use of io.quarkus.mailer.Mail in project automatiko-engine by automatiko-io.

the class SendEmailService method sendSimpleCorrelatedWithCC.

/**
 * Sends email to the given list of recipients and cc recipients with given subject and body.
 * Optionally given attachments will be put on the message as well
 *
 * In case of error a <code>ServiceExecutionError</code> will be thrown with error code set to <code>sendEmailFailure</code>
 * so it can be used within workflow definition to handle it
 *
 * @param correlation correlation information to be attached to the message
 * @param tosc comma separated list of recipients
 * @param ccs comma separated list of CC recipients
 * @param subject subject of the email
 * @param body body of the email
 * @param attachments optional attachments
 */
public void sendSimpleCorrelatedWithCC(String correlation, String tos, String ccs, String subject, String body, File<byte[]>... attachments) {
    try {
        for (String to : tos.split(",")) {
            Mail mail = Mail.withHtml(to, subject, body);
            for (String cc : ccs.split(",")) {
                mail.addCc(cc);
            }
            for (File<byte[]> attachment : attachments) {
                if (attachment == null) {
                    continue;
                }
                mail.addAttachment(attachment.name(), attachment.content(), attachment.type());
            }
            mail.addHeader("Message-ID", EmailUtils.messageIdWithCorrelation(correlation, host.orElse("localhost")));
            mailer.send(mail);
        }
    } catch (Exception e) {
        throw new ServiceExecutionError("sendEmailFailure", e.getMessage(), e);
    }
}
Also used : ServiceExecutionError(io.automatiko.engine.api.workflow.ServiceExecutionError) Mail(io.quarkus.mailer.Mail)

Example 9 with Mail

use of io.quarkus.mailer.Mail in project automatiko-engine by automatiko-io.

the class SendEmailService method sendSimpleWithCC.

/**
 * Sends email to the given list of recipients and cc recipients with given subject and body.
 * Optionally given attachments will be put on the message as well
 *
 * In case of error a <code>ServiceExecutionError</code> will be thrown with error code set to <code>sendEmailFailure</code>
 * so it can be used within workflow definition to handle it
 *
 * @param tosc comma separated list of recipients
 * @param ccs comma separated list of CC recipients
 * @param subject subject of the email
 * @param body body of the email
 * @param attachments optional attachments
 */
public void sendSimpleWithCC(String tos, String ccs, String subject, String body, File<byte[]>... attachments) {
    try {
        for (String to : tos.split(",")) {
            Mail mail = Mail.withHtml(to, subject, body);
            for (String cc : ccs.split(",")) {
                mail.addCc(cc);
            }
            for (File<byte[]> attachment : attachments) {
                if (attachment == null) {
                    continue;
                }
                mail.addAttachment(attachment.name(), attachment.content(), attachment.type());
            }
            mailer.send(mail);
        }
    } catch (Exception e) {
        throw new ServiceExecutionError("sendEmailFailure", e.getMessage(), e);
    }
}
Also used : ServiceExecutionError(io.automatiko.engine.api.workflow.ServiceExecutionError) Mail(io.quarkus.mailer.Mail)

Example 10 with Mail

use of io.quarkus.mailer.Mail in project automatiko-engine by automatiko-io.

the class SendEmailService method sendWithBCC.

/**
 * Sends email to the given list of recipients and cc recipients with given subject and body that is created based on the
 * given template.
 * Optionally given attachments will be put on the message as well
 *
 * In case of error a <code>ServiceExecutionError</code> will be thrown with error code set to <code>sendEmailFailure</code>
 * so it can be used within workflow definition to handle it
 *
 * In case template cannot be found with given name a <code>ServiceExecutionError</code> will
 * be thrown with error code set to <code>emailTemplateNotFound</code>
 *
 * @param tos comma separated list of recipients
 * @param bccs comma separated list of BCC recipients
 * @param subject subject of the email
 * @param templateName name of the email template to be used to create the body of the email message
 * @param body body of the email
 * @param attachments optional attachments
 */
public void sendWithBCC(String tos, String bccs, String subject, String templateName, Object body, File<byte[]>... attachments) {
    Template template = getTemplate(templateName);
    try {
        Map<String, Object> templateData = new HashMap<>();
        templateData.put("body", body);
        String content = template.instance().data(templateData).render();
        for (String to : tos.split(",")) {
            Mail mail = Mail.withHtml(to, subject, content);
            for (String bcc : bccs.split(",")) {
                mail.addBcc(bcc);
            }
            for (File<byte[]> attachment : attachments) {
                if (attachment == null) {
                    continue;
                }
                mail.addAttachment(attachment.name(), attachment.content(), attachment.type());
            }
            mailer.send(mail);
        }
    } catch (Exception e) {
        throw new ServiceExecutionError("sendEmailFailure", e.getMessage(), e);
    }
}
Also used : ServiceExecutionError(io.automatiko.engine.api.workflow.ServiceExecutionError) Mail(io.quarkus.mailer.Mail) HashMap(java.util.HashMap) Template(io.quarkus.qute.Template)

Aggregations

Mail (io.quarkus.mailer.Mail)17 ServiceExecutionError (io.automatiko.engine.api.workflow.ServiceExecutionError)12 HashMap (java.util.HashMap)8 Template (io.quarkus.qute.Template)7 QuarkusTest (io.quarkus.test.junit.QuarkusTest)3 Test (org.junit.jupiter.api.Test)3 HumanTaskWorkItem (io.automatiko.engine.api.runtime.process.HumanTaskWorkItem)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Incoming (org.eclipse.microprofile.reactive.messaging.Incoming)1 UserTaskDeadlineDataEvent (org.kie.kogito.services.event.UserTaskDeadlineDataEvent)1 UserTaskDeadlineEventBody (org.kie.kogito.services.event.impl.UserTaskDeadlineEventBody)1