Sending e-mail from Confluence with Groovy, ScriptRunner

Hi all,
I have come to a bit of a dead end with a script I'm working on. The purpose is to automatically create a pdf export file from a specific page and e-mail it. I would configure and save the script as a scheduled job, but first I need to get the script itself working. I have added my current version of it below, and would appreciate any help in regards to understanding what is wrong here.

import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Path;
import java.nio.file.Paths;

import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.mail.Email;
import com.atlassian.mail.server.SMTPMailServer;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;

URL website =
new URL("https://<confluence URL>/spaces/flyingpdf/pdfpageexport.action?pageId=<page ID number>");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("export.pdf");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

Path p1 = Paths.get("export.pdf");
File attachment = p1.toFile();
SMTPMailServer mailServer = PageManager.getInstance().getMailServerManager().getDefaultSMTPMailServer();
if (mailServer != null) {
Email email = new Email("<e-mail address goes here>");

Multipart multipart = new MimeMultipart();
//set attachment
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachment);
attachmentBodyPart.setDataHandler(new DataHandler(source));

attachmentBodyPart.setContent("hello world", "text/plain;charset=utf-8");



} else {
throw new RuntimeException("no mail server!!!");

Thank you in advance for any input :)
Best regards,

2 answers

0 votes


What is the error? Or no errors and it is just not working?

Attached a screenshot of the error on line 29 (that's the only error I get), this seems to be the faulty line:
SMTPMailServer mailServer = PageManager.getInstance().getMailServerManager().getDefaultSMTPMailServer();

Change the wrong line to

def confluenceMailServerManager = ComponentLocator.getComponent(ConfluenceMailServerManager)
SMTPMailServer mailServer = confluenceMailServerManager.getDefaultSMTPMailServer();

and add import

import com.atlassian.confluence.mail.ConfluenceMailServerManager 

Hi @Alexey Matveev [cPrime] I took the code for sending email only and the email is not sent when I run the code from scriptrunner console and from the result it is null.

What might be the problem? 

Check if the outgoing mail is enabled. 

Thanks @Alexey Matveev [cPrime]  I have correctly configured the email and now it is being sent.

@Alexey Matveev [cPrime] I want to take the email functionality further so that when a user has signed up he/she receives a welcome email.

How can I get the email of the user who has signed up so that I can place it in the 'signedupuseremail' as below:

 Email email = new Email("<signedupuseremail>");


I am trying to use script runner and I have selected the user create event now the challenge is getting the email of the user who has signed up  and setting it in the 'signedupuseremail'

I saw your queston. I could not find this event in Jira, that is why I did not answer.

You could do it by the answer in the thread below, but you would need to develop a plugin with a servletfileterpluginmodule

The new users are signing up on confluence and not jira.The event is on script runner for confluence.

It does not matter. The way to solve is the same. You should find the event. If it does not exist, then you should write your own serverfilterpluginmodule to generate the event.

Thanks - I changed/added the lines as you described and received no errors :)
However, the result didn't exactly come out as planned, added the screenshot of it:


I think the problem is with the following line

fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE)

Jira can not access the resource. Are you sure that the resource https://<confluence URL>/spaces/flyingpdf/pdfpageexport.action?pageId=<page ID number> is reachable from the Jira server?. Does the page allow anonymous access? 


Apologies for not answering sooner! I have been testing the code both ways (when the page is accessible to anonymous users and not), but the result is still the same for both - receiving the no route to host error. Since page export is also included, I provided the anonymous with permissions to see and export the space, re-indexed just to be sure, but still no result, unfortunately.

I have been working with Script Runner for Confluence, the script is also run/tested from Confluence - if it's possible/better/easier to obtain this functionality by using ScriptRunner for Jira, I could look into that.

"no route to host" is a network error, it means your machine that the code is running on cannot reach the server over the network. 

I'd want to check a call to http://<confluence URL> on the command line with curl or wget to ensure the machine can see it, assuming it is that (the error might be "can't see the mail server" as well, but the line number was around the opening of the confluence url)

Thank you for input and information :) I'll check with the sysadmin.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Feb 07, 2019 in Marketplace Apps

A Timeless Love Story

It started as any story starts, on a normal, rainy day.   Admin meets App, and her name was Klok2, and like any first relationship we were both trying to make it work but neither one knew what...

465 views 8 27
Read article

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