import javax.mail.Address
import javax.mail.Message
import javax.mail.Multipart
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeBodyPart
import javax.mail.internet.MimeMessage
import javax.mail.internet.MimeMultipart
final String sender = "sender@deephaven.io"
final String[] recipients = ["recipient@deephaven.io"] as String[]
final String subject = "Awesome report"
final String plotFilePath = "/tmp/StockPricePlot.png"
StockTrades = db.i("LearnDeephaven", "StockTrades").where("Date = currentDateNy()", "USym in `AAPL`, `GOOG`")
StockPricePlot = plotBy("Stock Prices", StockTrades, "Timestamp", "Last", "USym")
// Waits for the chart to finish being written to disk, with a timeout of 30 seconds.
// The image in your email may be empty or incomplete if you do not wait for the chart to finish being saved
StockPricePlot.save(plotFilePath, true, 30)
tableAsHtml = html(StockTrades)
msg = "<p>Howdy! Below is my table</p>\n" + tableAsHtml + "\n<p> And here is the chart </p>\n<img src=\"cid:image1\" />"
Properties sessionProperties = System.getProperties()
sessionProperties.put("mail.smtp.host", "localhost")
Session session = Session.getDefaultInstance(sessionProperties, null)
Message message = new MimeMessage(session)
message.setFrom(sender==null ? null : new InternetAddress(sender))
message.setSubject(subject)
recipientsAddresses = []
for (int i=0; i<recipients.length; i++){
recipientsAddresses[i]=new InternetAddress(recipients[i])
}
message.addRecipients(Message.RecipientType.TO, recipientsAddresses as Address[])
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart()
messageBodyPart.setContent(msg, "text/html")
// creates multi-part
Multipart multipart = new MimeMultipart()
multipart.addBodyPart(messageBodyPart)
// attach images as MimeBodyParts
MimeBodyPart imagePart = new MimeBodyPart()
imagePart.setHeader("Content-ID", "image1")
imagePart.setDisposition(MimeBodyPart.INLINE)
imagePart.attachFile(plotFilePath)
multipart.addBodyPart(imagePart)
message.setContent(multipart)
Transport.send(message)