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

Sending e-mail from Confluence with Groovy, ScriptRunner

Margaret Till Nov 08, 2017

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
Alexey Matveev [cPrime] Community Leader Nov 08, 2017


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

Margaret Till Nov 08, 2017

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

Alexey Matveev [cPrime] Community Leader Nov 08, 2017

Change the wrong line to

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

and add import

import com.atlassian.confluence.mail.ConfluenceMailServerManager 
Ian Feb 13, 2018

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? 

Alexey Matveev [cPrime] Community Leader Feb 13, 2018

Check if the outgoing mail is enabled. 

Ian Feb 13, 2018

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

Ian Mar 27, 2018

@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'

Alexey Matveev [cPrime] Community Leader Mar 27, 2018

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

Ian Mar 27, 2018

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

Alexey Matveev [cPrime] Community Leader Mar 27, 2018

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.

0 votes
Margaret Till Nov 09, 2017

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:


Alexey Matveev [cPrime] Community Leader Nov 09, 2017

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? 

Margaret Till Nov 16, 2017


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.

Nic Brough [Adaptavist] Community Leader Nov 16, 2017

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

Margaret Till Nov 16, 2017

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

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Marketplace Apps & Integrations

Webinar alert! An IT Admin's Guide to Securing Collaboration at Scale

Hello, Atlassian Community! My name is Dave Meyer and I'm a Principal Product Manager at Atlassian. I wanted to give this community a heads up about an upcoming Webinar that might be of interest...

186 views 2 7
Read article

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