I've successfully got email working within a report script, using the mail class that has been posted on these forums.

The next challenge is to take the data that the report is generating and add that to the email somehow, without saving the file output as normal.

Can anyone please give me pointers on how to add the data to the email? My report currently generates an xls file, but I don't want the user to have to select a save location as this report will hopefully be scheduled to run automatically.

Thanks :)

by Kay Fischbach
Posted on Wed, 11/16/2022 - 08:54
  1. Write the XLS to the output directory as you would do normally, then take the content of the file as a byte array and delete the file from the ouput directory.
  2. Your mail content is a MimeMultipart, that can hold both your mail text as well as your attachment.


Example code for getting the byte array:

var excelFileByteArray = Context.getFile(chosenFilename, Constants.LOCATION_OUTPUT);
Context.deleteFile(chosenFilename, Constants.LOCATION_OUTPUT);

Example code for the mail (this is for xlsx - take the appropriate MIME type for the ByteArrayDataSource from…):

this.send = function(sContent, additionalRecipients, subject, attachmentByteArray) {
    // set up your java.util.Properties here
    var instSession = Packages.javax.mail.Session.getInstance(props, null);
    var smtpTransport = instSession.getTransport("smtp");
    // Receiver Address:
    var msg = new Packages.javax.mail.internet.MimeMessage(instSession);
    // insert proper date
    msg.setSentDate(new Date());
    var recipients = [new Packages.javax.mail.internet.InternetAddress("")];
    recipients = recipients.concat(additionalRecipients);
    msg.setRecipients(Packages.javax.mail.internet.MimeMessage.RecipientType.TO, recipients);
    // "Subject": Subject of the mail    
    msg.setSubject(subject, "UTF-8");
    //main body
    var multipart = new Packages.javax.mail.internet.MimeMultipart();
    //message body - sContent is a JavaScript string containing HTML
    var messagePart = new Packages.javax.mail.internet.MimeBodyPart();
    messagePart.setContent(sContent, "text/html");
    //message attachment
    var attachmentPart = new Packages.javax.mail.internet.MimeBodyPart();
    var byteArrayDataSource = new Packages.javax.mail.util.ByteArrayDataSource(attachmentByteArray, "application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet");
    attachmentPart.setDataHandler(new Packages.javax.activation.DataHandler(byteArrayDataSource));




Featured achievement

Say hello to the ARIS Community! Personalize your community experience by following forums or tags, liking a post or uploading a profile picture.
Recent Unlocks


icon-arrow-down icon-arrow-cerulean-left icon-arrow-cerulean-right icon-arrow-down icon-arrow-left icon-arrow-right icon-arrow icon-back icon-close icon-comments icon-correct-answer icon-tick icon-download icon-facebook icon-flag icon-google-plus icon-hamburger icon-in icon-info icon-instagram icon-login-true icon-login icon-mail-notification icon-mail icon-mortarboard icon-newsletter icon-notification icon-pinterest icon-plus icon-rss icon-search icon-share icon-shield icon-snapchat icon-star icon-tutorials icon-twitter icon-universities icon-videos icon-views icon-whatsapp icon-xing icon-youtube icon-jobs icon-heart icon-heart2 aris-express bpm-glossary help-intro help-design Process_Mining_Icon help-publishing help-administration help-dashboarding help-archive help-risk icon-knowledge icon-question icon-events icon-message icon-more icon-pencil forum-icon