It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to send email from plugin?

If JIRA or Confluence already have smtp configured then how to use those settings to send email from a plugin?

7 answers

1 accepted

0 votes
Answer accepted

Event is one solution. You can also send email directly without trigger by using SMTPMailServer.

JIRA pseudo code:

SMTPMailServer  mailServer = MailFactory.getServerManager().getDefaultSMTPMailServer();            
if (NotificationRecipient.MIMETYPE_HTML.equalsIgnoreCase(prop.getProperty(messageTypeKey)))
mailServer.send(new Email(to.getEmailAddress()).setSubject(subject).setBody(message).setMimeType(Constants.NOTIFICATION_MIME_TYPE_HTML));
else
mailServer.send(new Email(to.getEmailAddress()).setSubject(subject).setBody(message).setMimeType(Constants.NOTIFICATION_MIME_TYPE_TEXT));

And here is an example using the mail queue:

SMTPMailServer mailServer = mailServerManager.getDefaultSMTPMailServer()
email.setFrom(mailServer.getDefaultFrom())
email.setSubject("email subject")
email.setMimeType(emailFormat == "HTML" ? "text/html" : "text/plain")
email.setBody("the body")
SingleMailQueueItem item = new SingleMailQueueItem(email)
ManagerFactory.getMailQueue().addItem(item)

Just a note: ManagerFactory been deprecated since JIRA 4.2. Use ComponentAccessor.getMailQueue().addItem(item); instead.

You can just use the 'ConfluenceMailQueueItem' class within your plugin.

Here is my example with groovy:

{groovy:output=wiki}
import com.atlassian.confluence.mail.template.ConfluenceMailQueueItem;

def toAddress="user@company.com";
def subject="testmail";
def body="some body";
def mimeType="text/html";

ConfluenceMailQueueItem mail = new ConfluenceMailQueueItem(toAddress, subject, body, mimeType);
mail.send();

println "mail successful send";
{groovy}

where to put this code?

The easiest way, I think, is to fire a new event from the plugin and create a mail notification for the event. You can create your own mail templates for each event.

Hi , Can u pls tell , how to send mail via JIRA plugin

a solution for Confluence is available here : http://blog.k15t.com/sending-email-from-confluence-plugins

I think it should not be too hard to adapt it to Jira since there are quite a few MailQueueItem factories available in the API, but I haven't tried anything in Jira.

Link is broke :(

All of them are right answer. So I marked the 3 as acceptable answer. Others were equally correct.

Somehow in subject line "[JIRA]" gets added.  How do I remove it?  With

email.setSubject("email subject")

when I get email subject line is "[JIRA] email subject"

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted yesterday in Jira

Upcoming Enterprise releases targeting Q3 2019: Jira Software 8.5 & Jira Service Desk 4.5

Since launching the Jira Software 8.0 and Jira Service Desk 4.0 platform releases in February, many of you have been asking when the next Enterprise release will be so you can take advantage of the m...

218 views 1 4
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you