Search in sources :

Example 1 with MailResult

use of org.apache.sling.commons.messaging.mail.MailResult in project sling by apache.

the class SimpleMailServiceIT method send.

@Test
public void send() throws Exception {
    final Dictionary<String, Object> properties = MailBuilderConfigurations.full(wiser.getServer().getPort());
    createFactoryConfiguration(FACTORY_PID, properties);
    final MessageService messageService = getService(MessageService.class);
    final CompletableFuture<Result> future = messageService.send("simple test message", "recipient@example.net");
    final MailResult result = (MailResult) future.get();
    final String message = new String(result.getMessage(), StandardCharsets.UTF_8);
    // TODO assert
    logger.info("message: {}", message);
}
Also used : MailResult(org.apache.sling.commons.messaging.mail.MailResult) MessageService(org.apache.sling.commons.messaging.MessageService) Result(org.apache.sling.commons.messaging.Result) MailResult(org.apache.sling.commons.messaging.mail.MailResult) Test(org.junit.Test)

Example 2 with MailResult

use of org.apache.sling.commons.messaging.mail.MailResult in project sling by apache.

the class SimpleMailServiceIT method sendWithData.

@Test
public void sendWithData() throws Exception {
    final Dictionary<String, Object> properties = MailBuilderConfigurations.full(wiser.getServer().getPort());
    createFactoryConfiguration(FACTORY_PID, properties);
    final MessageService messageService = getService(MessageService.class);
    final Map configuration = Collections.singletonMap("mail.subject", "Testing the Simple Mail Service with a custom subject");
    final Map data = Collections.singletonMap("mail", configuration);
    final CompletableFuture<Result> future = messageService.send("simple test message", "recipient@example.net", data);
    final MailResult result = (MailResult) future.get();
    final String message = new String(result.getMessage(), StandardCharsets.UTF_8);
    // TODO assert
    logger.info("message: {}", message);
}
Also used : MailResult(org.apache.sling.commons.messaging.mail.MailResult) Map(java.util.Map) MessageService(org.apache.sling.commons.messaging.MessageService) Result(org.apache.sling.commons.messaging.Result) MailResult(org.apache.sling.commons.messaging.mail.MailResult) Test(org.junit.Test)

Example 3 with MailResult

use of org.apache.sling.commons.messaging.mail.MailResult in project sling by apache.

the class SimpleMailService method sendMail.

private MailResult sendMail(final String message, final String recipient, final Map data, final MailBuilder mailBuilder) {
    try {
        final Email email = mailBuilder.build(message, recipient, data);
        final String messageId = email.send();
        logger.info("mail '{}' sent", messageId);
        final byte[] bytes = MailUtil.toByteArray(email);
        return new MailResult(bytes);
    } catch (EmailException | MessagingException | IOException e) {
        throw new CompletionException(e);
    }
}
Also used : MailResult(org.apache.sling.commons.messaging.mail.MailResult) Email(org.apache.commons.mail.Email) MessagingException(javax.mail.MessagingException) CompletionException(java.util.concurrent.CompletionException) EmailException(org.apache.commons.mail.EmailException) IOException(java.io.IOException)

Aggregations

MailResult (org.apache.sling.commons.messaging.mail.MailResult)3 MessageService (org.apache.sling.commons.messaging.MessageService)2 Result (org.apache.sling.commons.messaging.Result)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 Map (java.util.Map)1 CompletionException (java.util.concurrent.CompletionException)1 MessagingException (javax.mail.MessagingException)1 Email (org.apache.commons.mail.Email)1 EmailException (org.apache.commons.mail.EmailException)1