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 :)
- 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.
- 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();
}
The javax.mail package appear to have been removed from the most recent releases, with little notice.
Also, it seems to choke on the TLSv2 authentication supported by Office 365 (or at least I haven't found any way of making it work): having a wide-open smtp relay is a bit dangerous these days.