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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

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"

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
Community showcase
Posted in Teamwork

How does your team use images, attachments and video to enrich your collaboration?

Hi Atlassian Community! My name is Owen and I work on Atlassian's Media Platform team. The Media Platform team handles the heavy lifting of managing uploads, serving images, attaching files and s...

162 views 2 7
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you