use of com.bluenimble.platform.messaging.MessengerException in project serverless by bluenimble.
the class SmtpMessenger method send.
@Override
public void send(Sender sender, Recipient[] recipients, String subject, String content, ApiStreamSource... attachments) throws MessengerException {
ClassLoader pcl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
// creates a new e-mail message
try {
if (sender == null) {
sender = NoSender;
}
Message message = new MimeMessage(session);
String senderEmail = sender.id();
if (Lang.isNullOrEmpty(senderEmail)) {
senderEmail = user;
}
String senderName = sender.name();
if (Lang.isNullOrEmpty(senderName)) {
message.setFrom(new InternetAddress(senderEmail));
} else {
message.setFrom(new InternetAddress(senderEmail, senderName));
}
InternetAddress[] toAddresses = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
toAddresses[i] = new InternetAddress(recipients[i].id());
}
message.setRecipients(Message.RecipientType.TO, toAddresses);
message.setSubject(subject);
message.setSentDate(new Date());
// creates message part
MimeBodyPart messageText = new MimeBodyPart();
messageText.setContent(content, ApiContentTypes.Html);
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageText);
// adds attachments
if (attachments != null && attachments.length > 0) {
for (final ApiStreamSource attachment : attachments) {
MimeBodyPart mbd = new MimeBodyPart();
DataSource ds = new DataSource() {
@Override
public String getContentType() {
return attachment.contentType();
}
@Override
public InputStream getInputStream() throws IOException {
return attachment.stream();
}
@Override
public String getName() {
return attachment.name();
}
@Override
public OutputStream getOutputStream() throws IOException {
throw new UnsupportedOperationException("getOutputStream not supported");
}
};
mbd.setDataHandler(new DataHandler(ds));
multipart.addBodyPart(mbd);
}
}
// sets the multi-part as e-mail's content
message.setContent(multipart);
// sends the e-mail
Transport.send(message);
} catch (Exception ex) {
throw new MessengerException(ex.getMessage(), ex);
} finally {
Thread.currentThread().setContextClassLoader(pcl);
}
}
use of com.bluenimble.platform.messaging.MessengerException in project serverless by bluenimble.
the class SmtpMessenger method send.
@Override
public void send(Sender sender, Recipient[] recipients, String subject, final ApiResource content, JsonObject data, ApiStreamSource... attachments) throws MessengerException {
final String uuid = content.owner() + Lang.SLASH + content.name();
CachedTemplate cTemplate = templates.get(uuid);
if (cTemplate == null || cTemplate.timestamp < content.timestamp().getTime()) {
cTemplate = new CachedTemplate();
cTemplate.timestamp = content.timestamp().getTime();
TemplateSource source = new TemplateSource() {
@Override
public long lastModified() {
return content.timestamp().getTime();
}
@Override
public String filename() {
return uuid;
}
@Override
public String content() throws IOException {
InputStream is = null;
try {
is = content.toInput();
return IOUtils.toString(is);
} finally {
IOUtils.closeQuietly(is);
}
}
};
try {
cTemplate.template = engine.compile(source, StartDelimitter, EndDelimitter);
} catch (IOException e) {
throw new MessengerException(e.getMessage(), e);
}
templates.put(uuid, cTemplate);
}
StringWriter sw = new StringWriter();
try {
cTemplate.template.apply(data, sw);
} catch (Exception e) {
throw new MessengerException(e.getMessage(), e);
}
send(sender, recipients, subject, sw.toString(), attachments);
}
Aggregations