Search in sources :

Example 1 with MessengerException

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);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MessengerException(com.bluenimble.platform.messaging.MessengerException) DataHandler(javax.activation.DataHandler) ApiStreamSource(com.bluenimble.platform.api.ApiStreamSource) Date(java.util.Date) IOException(java.io.IOException) MessengerException(com.bluenimble.platform.messaging.MessengerException) DataSource(javax.activation.DataSource) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 2 with MessengerException

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);
}
Also used : TemplateSource(com.github.jknack.handlebars.io.TemplateSource) StringWriter(java.io.StringWriter) InputStream(java.io.InputStream) MessengerException(com.bluenimble.platform.messaging.MessengerException) IOException(java.io.IOException) IOException(java.io.IOException) MessengerException(com.bluenimble.platform.messaging.MessengerException)

Aggregations

MessengerException (com.bluenimble.platform.messaging.MessengerException)2 IOException (java.io.IOException)2 ApiStreamSource (com.bluenimble.platform.api.ApiStreamSource)1 TemplateSource (com.github.jknack.handlebars.io.TemplateSource)1 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1 Date (java.util.Date)1 DataHandler (javax.activation.DataHandler)1 DataSource (javax.activation.DataSource)1 Message (javax.mail.Message)1 Multipart (javax.mail.Multipart)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1 MimeMessage (javax.mail.internet.MimeMessage)1 MimeMultipart (javax.mail.internet.MimeMultipart)1