Sending Email from Confluence Plugins

Confluence provides a great infrastructure for doing all kinds of things in plugins. One requirement I came across a few times is sending emails from Confluence, which I will explain in this article.

There are two ways of sending emails in Confluence: 1. Synchronous 2. Asynchronous using a mail queue

I will explain option 2, as synchronous sending will make the user wait until the email is sent to the mail server. Asynchronous sending is implemented using a queuing mechanism, and will send the email every 60 seconds. This implementation build on the com.atlassian.core.task.MultiQueueTaskManager which can be injected as a dependency by the Confluence plugin system. After that it is very easy to send emails:

:
String emailText = "Email Text with HTML";
ConfluenceMailQueueItem item = new ConfluenceMailQueueItem(
        contact.getEmail(), 
        null, 
        subject, 
        emailText, 
        ConfluenceMailQueueItem.MIME_TYPE_HTML);
item.setFromName("Example Inc.");
item.setFromAddress("website@example.com");
taskManager.addTask("mail", item);
:

For rendering the email text from templates the VelocityManager is quite handy, which I will explain in another post.