How to send E-Mails based on Confluence Page Content with REST-API

Artur A_ May 6, 2021

Hello,
I would like to write a java based app that sends e-mails using the confluence REST-API.

Formerly I wrote a bot that did that using the frontend after updating the page(not nice, but worked fine). The basic steps were:

1. Update(PUT) page with new content (daily report)
2. Send e-mail using the frontend (share feature)

I can imagine the app working like:

Option1: Use PUT to update page, use an not yet known way to send an email using Rest-API.
Option2: Use PUT to update page, use another way to send an e-mail with the already available content using Java. 


I think option2 would be enough to get the results we need. 
I still would like to know if there is a "nice" way of confluence doing that.


Any hints appreciated. Thank you.

 

1 answer

1 accepted

1 vote
Answer accepted
Thomas Deiler
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 6, 2021

Dear @Artur A_ ,

welcome to the community.

Unfortunately the REST API doesn't allow to direct send mails. They are only sent when you change content and somebody is a watcher. (Btw. You can modify the watchers with REST.)

So I recommend option 2. Java Mail Api is simple to use:

import javax.mail.*;
import java.util.Properties;

public class SendMail {

private Properties props;
private Session session;

public SendMail (String host, String port) {
this.props = new Properties();
this.props.put("mail.smtp.host", host);
this.props.put("mail.smtp.port", port);
this.props.put("mail.transport.protocol","smtp");
this.props.put("mail.smtp.auth", "false");
this.session = Session.getDefaultInstance(props, null);
}

public void send(String subject, String msgBody, String from, String[] to) {
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
for (int i=0; i<to.length; i++) {
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
}
msg.setSubject(subject);
msg.setText(msgBody);
Transport.send(msg);
} catch (MessagingException e) {
String msg = "Send failed. " + e.getMessage();
throw new RuntimeException(msg);
}
}

}

 So long

Thomas

Artur A_ May 6, 2021

Hello Thomas,

thank you for your swift and clear answer. Much appreciated.

Thomas Deiler
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 7, 2021

Dear @Artur A_ 

don't forget to press the "green accept answer" button to indicate other readers the successful answering of your question.

So long
Thomas

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events