0 votes
711 views
in Integrations by

1 Answer

0 votes
ago by (30.6k points)

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

Related questions

0 votes
1 answer 1.5k views
0 votes
1 answer 2.6k views
0 votes
1 answer 1.0k views
0 votes
1 answer 686 views
0 votes
1 answer 1.7k views
0 votes
1 answer 1.6k views
0 votes
1 answer 769 views
0 votes
1 answer 1.1k views
asked May 15, 2016 in Integrations by anonymous
0 votes
1 answer 1.0k views

723 questions

807 answers

98 comments

4.9k users

Join BPM Community Discord Channel

Welcome to BPM Tips Q&A, Community wiki/forum where you can ask questions and receive answers from other IBM BPM experts and members of the community. Users with 2000 points will automatically be promoted to expert level.
Created by Dosvak LLC
Our Youtube Channel
...