How to send email from plugin?

Pankaj Jangid
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 11, 2011

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

7 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Radek Kantor
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 7, 2012

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));

3 votes
Sergej Schlund July 26, 2012

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}

J D
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 21, 2014

where to put this code?

3 votes
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 7, 2012

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)

Royce Wong
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 4, 2015

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

1 vote
Jobin Kuruvilla [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 11, 2011

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.

George February 12, 2015

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

0 votes
Zubair Patel March 12, 2015

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"

0 votes
Pankaj Jangid
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 8, 2012

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

0 votes
Pietro Descombes February 7, 2012

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.

J D
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 21, 2014

Link is broke :(

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events