The OOB Send E-Mail service (System Data) takes attachments from the BPM document store on BAW 8.6+ / 20+ (the service has an attachments parameter that accepts document references); on 8.5.x it sends text / HTML only, so attachments need a Java integration. Both ways:
// BAW: Send E-Mail with attachments from the document store (service flow)
tw.local.attachments = new tw.object.listOf.String(); // document ids of this instance (TWDocument.id)
var docs = tw.system.currentProcessInstance.documents;
for (var i = 0; i < docs.length; i++) if (docs[i].name.match(/\.pdf$/i)) tw.local.attachments.insertIntoList(tw.local.attachments.listLength, docs[i].id);
// map to the Send E-Mail service: to, subject, body (HTML), attachments -> the documents are attached
// any version: Java integration with Jakarta Mail, content fetched from the document store through REST
public static void send(String smtpHost, String from, String to, String subject, String html, String docIdsCsv, String baseUrl, String user, String pw) throws Exception {
Properties p = new Properties(); p.put("mail.smtp.host", smtpHost);
MimeMessage m = new MimeMessage(Session.getInstance(p)); m.setFrom(from); m.setRecipients(Message.RecipientType.TO, to); m.setSubject(subject, "UTF-8");
MimeMultipart mp = new MimeMultipart(); MimeBodyPart body = new MimeBodyPart(); body.setContent(html, "text/html; charset=UTF-8"); mp.addBodyPart(body);
for (String id : docIdsCsv.split(",")) {
HttpURLConnection c = (HttpURLConnection) new URL(baseUrl + "/rest/bpm/wle/v1/document/" + id.trim() + "/content").openConnection();
c.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString((user + ":" + pw).getBytes("UTF-8")));
byte[] bytes = c.getInputStream().readAllBytes(); String name = c.getHeaderField("Content-Disposition"); name = name != null && name.contains("filename=") ? name.replaceAll(".*filename=\"?([^\";]+).*", "$1") : "attachment";
MimeBodyPart att = new MimeBodyPart(); att.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, c.getContentType()))); att.setFileName(name); mp.addBodyPart(att);
}
m.setContent(mp); Transport.send(m);
}For ECM documents (FileNet) fetch the content through the CMIS content stream (the Get Document Content integration of the Content Management toolkit returns Base64) and pass the bytes to the same mail method. Keep attachments small (mail relays cap at 10-25 MB) - for larger files send a link to the document instead.
References