Apache Sling Commons Messaging Mail

Clone this repo:
  1. 25f9d09 SLING-10676 - remove SECURITY.md which is not needed by Bertrand Delacretaz · 7 days ago master
  2. eb87260 SLING-10676 - add or update SECURITY.md by Bertrand Delacretaz · 8 days ago
  3. 93c571a SLING-10676 - add or update SECURITY.md by Bertrand Delacretaz · 8 days ago
  4. 2f848bd [spotbugs] exclude EI_EXPOSE_REP2 by Oliver Lietz · 11 days ago
  5. f29c91a [pmd] EmptyCatchBlock by Oliver Lietz · 11 days ago

Apache Sling

Build Status Test Status Coverage Sonarcloud Status JavaDoc Maven Central License

Apache Sling Commons Messaging Mail

This module is part of the Apache Sling project.

This module provides a simple layer on top of Jakarta Mail 2.0 (package jakarta.mail) including a message builder and a service to send mails via SMTPS.

  • Mail Service: sends MIME messages
  • Message Builder: builds plain text and HTML messages with attachments and inline images
  • Message ID Provider: allows overwriting default message IDs by custom ones




Example factory configuration (SimpleMailServiceConfiguration) for SimpleMailService:

  "mail.smtps.from": "envelope-from@example.org",
  "mail.smtps.host": "smtp.example.org",
  "mail.smtps.port": 465,
  "username": "SMTP-USERNAME-PLAIN",
  "messageIdProvider.target": "(names=hostname)"

Optional MessageIdProvider

Example factory configuration (SimpleMessageIdProviderConfiguration) for optional SimpleMessageIdProvider:

  "names": [
  "host": "author.cms.example.org"


Create a multipart MIME message with an attachment (filename: song.flac) where the HTML part contains an inline image (cid: ska) and send it:

    MailService mailService;

    String subject = "Rudy, A Message to You";
    String text = "Stop your messing around\nBetter think of your future\nTime you straighten right out\nCreating problems in town\n…";
    String html = […];
    byte[] attachment = […];
    byte[] inline = […];

    MimeMessage message = mailService.getMessageBuilder()
        .from("dandy.livingstone@kingston.jamaica.example.net", "Dandy Livingstone")
        .to("the.specials@coventry.england.example.net", "The Specials")
        .attachment(attachment, "audio/flac", "song.flac")
        .inline(inline, "image/png", "ska")



Integration Tests

Integration tests require a running SMTP server. By default a GreenMail server is started.

An external SMTP server for validating messages with real mail clients can be used by setting required properties:

mvn clean install\
  -Dsling.test.mail.from.name=From\ Sender\
  -Dsling.test.mail.to.name=To\ Recipient\
  -Dsling.test.mail.replyTo.name=Reply\ To