LS

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:

wb.write();
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 https://learn.microsoft.com/de-de/archive/blogs/vsofficedeveloper/office-2007-file-format-mime-types-for-http-content-s…):

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);
    msg.setFrom(adrFrom);
     
    // insert proper date
    msg.setSentDate(new Date());
    
    
    var recipients = [new Packages.javax.mail.internet.InternetAddress("redacted2@example.com")];
    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");
    multipart.addBodyPart(messagePart);
    
    //message attachment https://stackoverflow.com/a/23084334
    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));
    attachmentPart.setFileName(chosenFilename);
    multipart.addBodyPart(attachmentPart);

    msg.setContent(multipart);
    
    smtpTransport.connect();
    smtpTransport.send(msg);
    smtpTransport.close();
}

 

0

Featured achievement

Rookie
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

Leaderboard

|
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