Search in sources :

Example 1 with AttachmentMapInterface

use of com.dexels.navajo.adapter.mailmap.AttachmentMapInterface in project navajo by Dexels.

the class CommonsMailMap method sendMail.

/**
 * This is where the actual mail is constructed and send
 * Inline images can be used through attachments
 * Annotation is cid:{?}. This will be replaced with cid:?
 * The first ? refers to the index number in the attachments starting with 0
 * The second ? will contain the generated id
 * EXAMPLE:
 * <hmtl>Some text <img src=\"cid:{0}\"></html>
 * The {0} will then be replaced with the first generated inline tag
 * @throws UserException
 */
public void sendMail() throws UserException {
    final ClassLoader current = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(javax.mail.Session.class.getClassLoader());
        // Create the email message and fill the basics
        HtmlEmail email = getNewHtmlEmail();
        if (debug) {
            email.setDebug(debug);
        }
        fillHtmlEmailBasics(email);
        // add attachments
        List<String> inlineImages = new ArrayList<>();
        if (this.attachments != null) {
            logger.debug("# of attachments found: {}", attachments.size());
            for (int i = 0; i < this.attachments.size(); i++) {
                AttachmentMapInterface am = this.attachments.get(i);
                String file = am.getAttachFile();
                String userFileName = am.getAttachFileName();
                Binary content = am.getAttachFileContent();
                String contentDisposition = am.getAttachContentDisposition();
                // Figure out how to get the path and then the url
                String fileName = "";
                if (content != null) {
                    fileName = content.getTempFileName(false);
                } else {
                    fileName = file;
                }
                File fl = new File(fileName);
                URL url = fl.toURI().toURL();
                logger.debug("Using url: {}", url);
                if (contentDisposition != null && contentDisposition.equalsIgnoreCase("Inline")) {
                    // embed the image and get the content id
                    inlineImages.add(email.embed(url, userFileName));
                } else {
                    email.attach(this.getEmailAttachment(fileName, url, contentDisposition, userFileName, userFileName));
                }
            }
        } else {
            logger.debug("No attachments");
        }
        logger.debug("Setting body, before replace: " + bodyText);
        // Replace any inline image tags with the created ones
        bodyText = replaceInlineImageTags(bodyText, inlineImages);
        // Finally set the complete html
        logger.debug("Setting body: {}", bodyText);
        email.setHtmlMsg(bodyText);
        // set the alternative message
        email.setTextMsg(this.getNonHtmlText());
        logger.info("Sending mail to {} cc: {} bcc: {} with subject: {}", to, cc, bcc, subject);
        // send the email
        email.send();
    } catch (Exception e) {
        if (ignoreFailures) {
            AuditLog.log("CommonsMailMap", e.getMessage(), e, Level.WARNING, myAccess.accessID);
            failure = e.getMessage();
        } else {
            AuditLog.log("CommonsMailMap", e.getMessage(), e, Level.SEVERE, myAccess.accessID);
            throw new UserException(-1, e.getMessage(), e);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(current);
    }
}
Also used : HtmlEmail(org.apache.commons.mail.HtmlEmail) ArrayList(java.util.ArrayList) AttachmentMapInterface(com.dexels.navajo.adapter.mailmap.AttachmentMapInterface) URL(java.net.URL) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) EmailException(org.apache.commons.mail.EmailException) Binary(com.dexels.navajo.document.types.Binary) UserException(com.dexels.navajo.script.api.UserException) File(java.io.File) Session(javax.mail.Session)

Example 2 with AttachmentMapInterface

use of com.dexels.navajo.adapter.mailmap.AttachmentMapInterface in project navajo by Dexels.

the class MailMapAlternative method sendMail.

private final void sendMail() throws UserException {
    retries++;
    try {
        String result = "";
        result = text;
        Properties props = System.getProperties();
        props.put("mail.smtp.host", mailServer);
        Session session = Session.getInstance(props);
        javax.mail.Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(sender));
        InternetAddress[] addresses = new InternetAddress[this.recipientArray.length];
        for (int i = 0; i < this.recipientArray.length; i++) {
            addresses[i] = new InternetAddress(this.recipientArray[i]);
        }
        msg.setRecipients(javax.mail.Message.RecipientType.TO, addresses);
        if (ccArray != null) {
            InternetAddress[] extra = new InternetAddress[this.ccArray.length];
            for (int i = 0; i < this.ccArray.length; i++) {
                extra[i] = new InternetAddress(this.ccArray[i]);
            }
            msg.setRecipients(javax.mail.Message.RecipientType.CC, extra);
        }
        if (bccArray != null) {
            InternetAddress[] extra = new InternetAddress[this.bccArray.length];
            for (int i = 0; i < this.bccArray.length; i++) {
                extra[i] = new InternetAddress(this.bccArray[i]);
            }
            msg.setRecipients(javax.mail.Message.RecipientType.BCC, extra);
        }
        msg.setSubject(subject);
        msg.setSentDate(new java.util.Date());
        // Use stylesheet if specified.
        if (!xslFile.equals("")) {
            java.io.File xsl = new java.io.File(xslFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            doc.write(bos);
            bos.close();
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            Document navajoDoc = XMLDocumentUtils.createDocument(bis, false);
            bis.close();
            result = XMLDocumentUtils.transform(navajoDoc, xsl);
        }
        if (attachments == null && contentType.equals("text/plain")) {
            msg.setText(result);
        } else {
            Multipart multipart = new MimeMultipart("mixed");
            BodyPart textBody = new MimeBodyPart();
            textBody.setContent(result, contentType);
            Multipart related = new MimeMultipart("related");
            related.addBodyPart(textBody);
            if (bodyparts != null && !bodyparts.isEmpty()) {
                // Put related bodyparts in related.
                for (int i = 0; i < bodyparts.size(); i++) {
                    AttachmentMapInterface am = bodyparts.get(i);
                    String file = am.getAttachFile();
                    String userFileName = am.getAttachFileName();
                    Binary content = am.getAttachFileContent();
                    String encoding = am.getEncoding();
                    String attachContentType = am.getAttachContentType();
                    MimeBodyPart bp = new MimeBodyPart();
                    logger.debug("Embedding: {}", userFileName);
                    if (file != null) {
                        if (userFileName == null) {
                            userFileName = file;
                        }
                        FileDataSource fileDatasource = new FileDataSource(file);
                        bp.setDataHandler(new DataHandler(fileDatasource));
                    } else if (content != null) {
                        BinaryDataSource bds = new BinaryDataSource(content, "");
                        DataHandler dh = new DataHandler(bds);
                        bp.setDataHandler(dh);
                        if (encoding != null) {
                            bp.setHeader("Content-Transfer-Encoding", encoding);
                            encoding = null;
                        }
                    }
                    bp.setFileName(userFileName);
                    if (attachContentType != null) {
                        bp.setHeader("Content-Type", attachContentType);
                    }
                    bp.setHeader("Content-ID", "<attach-nr-" + i + ">");
                    related.addBodyPart(bp);
                }
            }
            MimeBodyPart bop = new MimeBodyPart();
            bop.setContent(related);
            multipart.addBodyPart(bop);
            if (attachments != null) {
                for (int i = 0; i < attachments.size(); i++) {
                    AttachmentMapInterface am = attachments.get(i);
                    String file = am.getAttachFile();
                    String userFileName = am.getAttachFileName();
                    Binary content = am.getAttachFileContent();
                    String encoding = am.getEncoding();
                    String attachContentType = am.getAttachContentType();
                    String attachContentDisposition = am.getAttachContentDisposition();
                    MimeBodyPart bp = new MimeBodyPart();
                    logger.debug("Attaching: {}", userFileName);
                    if (file != null) {
                        if (userFileName == null) {
                            userFileName = file;
                        }
                        FileDataSource fileDatasource = new FileDataSource(file);
                        bp.setDataHandler(new DataHandler(fileDatasource));
                    } else if (content != null) {
                        BinaryDataSource bds = new BinaryDataSource(content, "");
                        DataHandler dh = new DataHandler(bds);
                        bp.setDataHandler(dh);
                        if (encoding != null) {
                            bp.setHeader("Content-Transfer-Encoding", encoding);
                        }
                    }
                    bp.setFileName(userFileName);
                    if (attachContentType != null) {
                        bp.setHeader("Content-Type", attachContentType);
                    }
                    bp.setDisposition(attachContentDisposition);
                    multipart.addBodyPart(bp);
                }
            }
            msg.setContent(multipart);
        }
        logger.info("Sending mail to {} cc: {} bcc: {} with subject: {}", recipients, cc, bcc, subject);
        Transport.send(msg);
    } catch (Exception e) {
        if (ignoreFailures) {
            AuditLog.log("MailMap", e.getMessage(), e, Level.WARNING, myAccess.accessID);
            failure = e.getMessage();
        } else {
            AuditLog.log("MailMap", e.getMessage(), e, Level.SEVERE, myAccess.accessID);
            throw new UserException(-1, e.getMessage());
        }
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) DataHandler(javax.activation.DataHandler) Properties(java.util.Properties) Document(org.w3c.dom.Document) BinaryDataSource(com.dexels.navajo.datasource.BinaryDataSource) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) FileDataSource(javax.activation.FileDataSource) UserException(com.dexels.navajo.script.api.UserException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AttachmentMapInterface(com.dexels.navajo.adapter.mailmap.AttachmentMapInterface) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) ByteArrayInputStream(java.io.ByteArrayInputStream) Binary(com.dexels.navajo.document.types.Binary) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session)

Example 3 with AttachmentMapInterface

use of com.dexels.navajo.adapter.mailmap.AttachmentMapInterface in project navajo by Dexels.

the class ResourceComponent method send.

@Override
public void send(String from, String[] to, String[] cc, String[] bcc, String subject, String body, List<AttachmentMapInterface> attachments, String contentType, boolean relatedMultipart) throws UserException {
    javax.mail.Message msg = new MimeMessage(session);
    if (from == null || "".equals(from)) {
        throw new UserException(-1, "Error: Required sender address not set!");
    }
    try {
        msg.setFrom(new InternetAddress(from));
        InternetAddress[] addresses = new InternetAddress[to.length];
        for (int i = 0; i < to.length; i++) {
            addresses[i] = new InternetAddress(to[i]);
        }
        msg.setRecipients(javax.mail.Message.RecipientType.TO, addresses);
        if (cc != null) {
            InternetAddress[] extra = new InternetAddress[cc.length];
            for (int i = 0; i < cc.length; i++) {
                extra[i] = new InternetAddress(cc[i]);
            }
            msg.setRecipients(javax.mail.Message.RecipientType.CC, extra);
        }
        if (bcc != null) {
            InternetAddress[] extra = new InternetAddress[bcc.length];
            for (int i = 0; i < bcc.length; i++) {
                extra[i] = new InternetAddress(bcc[i]);
            }
            msg.setRecipients(javax.mail.Message.RecipientType.BCC, extra);
        }
        msg.setSubject(subject);
        msg.setSentDate(new java.util.Date());
        if (attachments == null && contentType.equals("text/plain")) {
            msg.setText(body);
        } else {
            Multipart multipart = (relatedMultipart ? new MimeMultipart("related") : new MimeMultipart());
            BodyPart textBody = new MimeBodyPart();
            textBody.setContent(body, contentType);
            multipart.addBodyPart(textBody);
            if (attachments != null) {
                for (int i = 0; i < attachments.size(); i++) {
                    AttachmentMapInterface am = attachments.get(i);
                    String file = am.getAttachFile();
                    String userFileName = am.getAttachFileName();
                    Binary content = am.getAttachFileContent();
                    String encoding = am.getEncoding();
                    MimeBodyPart bp = new MimeBodyPart();
                    if (file != null) {
                        if (userFileName == null) {
                            userFileName = file;
                        }
                        FileDataSource fileDatasource = new FileDataSource(file);
                        bp.setDataHandler(new DataHandler(fileDatasource));
                    } else if (content != null) {
                        BinaryDataSource bds = new BinaryDataSource(content, "");
                        DataHandler dh = new DataHandler(bds);
                        bp.setDataHandler(dh);
                        if (encoding != null) {
                            bp.setHeader("Content-Transfer-Encoding", encoding);
                            encoding = null;
                        }
                    }
                    bp.setFileName(userFileName);
                    if (relatedMultipart) {
                        bp.setHeader("Content-ID", "<attach-nr-" + i + ">");
                    }
                    // iPhone headers
                    // bp.setDisposition("attachment");
                    bp.setDisposition(am.getAttachContentDisposition());
                    multipart.addBodyPart(bp);
                }
            }
            msg.setContent(multipart);
        }
        Transport.send(msg);
    } catch (AddressException e) {
        throw new UserException("Error sending mail:", e);
    } catch (MessagingException e) {
        throw new UserException("Error sending mail:", e);
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MessagingException(javax.mail.MessagingException) AttachmentMapInterface(com.dexels.navajo.adapter.mailmap.AttachmentMapInterface) DataHandler(javax.activation.DataHandler) BinaryDataSource(com.dexels.navajo.datasource.BinaryDataSource) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) AddressException(javax.mail.internet.AddressException) FileDataSource(javax.activation.FileDataSource) UserException(com.dexels.navajo.script.api.UserException) Binary(com.dexels.navajo.document.types.Binary) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 4 with AttachmentMapInterface

use of com.dexels.navajo.adapter.mailmap.AttachmentMapInterface in project navajo by Dexels.

the class MailMap method sendMail.

private final void sendMail() throws UserException {
    final ClassLoader current = Thread.currentThread().getContextClassLoader();
    retries++;
    try {
        Thread.currentThread().setContextClassLoader(javax.mail.Session.class.getClassLoader());
        String result = "";
        result = text;
        Session session = createSession();
        javax.mail.Message msg = new MimeMessage(session);
        if (sender == null || "".equals(sender)) {
            throw new UserException(-1, "Error: Required sender address not set!");
        }
        msg.setFrom(new InternetAddress(sender));
        InternetAddress[] recipients = convertToInternetAddresses(this.recipientArray);
        msg.setRecipients(javax.mail.Message.RecipientType.TO, recipients);
        InternetAddress[] ccrecipients = null;
        if (ccArray != null) {
            ccrecipients = convertToInternetAddresses(this.ccArray);
            msg.setRecipients(javax.mail.Message.RecipientType.CC, ccrecipients);
        }
        InternetAddress[] bccrecipients = null;
        if (bccArray != null) {
            bccrecipients = convertToInternetAddresses(this.bccArray);
            msg.setRecipients(javax.mail.Message.RecipientType.BCC, bccrecipients);
        }
        msg.setSubject(subject);
        msg.setSentDate(new java.util.Date());
        // Use stylesheet if specified.
        if (!xslFile.equals("")) {
            java.io.File xsl = new java.io.File(xslFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            doc.write(bos);
            bos.close();
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            Document dc = XMLDocumentUtils.createDocument(bis, false);
            bis.close();
            result = XMLDocumentUtils.transform(dc, xsl);
        }
        if (attachments == null && contentType.equals("text/plain")) {
            msg.setText(result);
        } else if (attachments == null || attachments.isEmpty()) {
            msg.setContent(result, contentType);
        } else {
            Multipart multipart = (relatedMultipart ? new MimeMultipart("related") : new MimeMultipart());
            BodyPart textBody = new MimeBodyPart();
            textBody.setContent(result, contentType);
            multipart.addBodyPart(textBody);
            if (attachments != null) {
                for (int i = 0; i < attachments.size(); i++) {
                    AttachmentMapInterface am = attachments.get(i);
                    String file = am.getAttachFile();
                    String userFileName = am.getAttachFileName();
                    Binary content = am.getAttachFileContent();
                    String encoding = am.getEncoding();
                    MimeBodyPart bp = new MimeBodyPart();
                    if (file != null) {
                        if (userFileName == null) {
                            userFileName = file;
                        }
                        FileDataSource fileDatasource = new FileDataSource(file);
                        bp.setDataHandler(new DataHandler(fileDatasource));
                    } else if (content != null) {
                        BinaryDataSource bds = new BinaryDataSource(content, "");
                        DataHandler dh = new DataHandler(bds);
                        bp.setDataHandler(dh);
                        bp.setFileName(userFileName);
                        if (encoding != null) {
                            bp.setHeader("Content-Transfer-Encoding", encoding);
                            encoding = null;
                        }
                    }
                    if (relatedMultipart) {
                        bp.setHeader("Content-ID", "<attach-nr-" + i + ">");
                    }
                    bp.getFileName();
                    // iPhone headers
                    bp.setDisposition(am.getAttachContentDisposition());
                    multipart.addBodyPart(bp);
                }
            }
            msg.setContent(multipart);
        }
        logger.info("Sending mail to: {}, cc: {}, bcc: {} with subject: {}", Arrays.toString(recipients), ccrecipients != null ? Arrays.toString(ccrecipients) : "[]", bccrecipients != null ? Arrays.toString(bccrecipients) : "[]", subject);
        Transport.send(msg);
    } catch (Exception e) {
        logger.error("Exception on sending mail!", e);
        if (ignoreFailures) {
            AuditLog.log("MailMap", e.getMessage(), e, Level.WARNING, myAccess.accessID);
            logger.warn("ingoreFailures flag is set. Ignoring the invalid e-mail address.");
            failureBuffer.append(e.getMessage());
            failureBuffer.append(";");
        } else {
            AuditLog.log("MailMap", e.getMessage(), e, Level.SEVERE, myAccess.accessID);
            throw new UserException(-1, e.getMessage(), e);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(current);
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) DataHandler(javax.activation.DataHandler) Document(org.w3c.dom.Document) BinaryDataSource(com.dexels.navajo.datasource.BinaryDataSource) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) FileDataSource(javax.activation.FileDataSource) UserException(com.dexels.navajo.script.api.UserException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AttachmentMapInterface(com.dexels.navajo.adapter.mailmap.AttachmentMapInterface) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) ByteArrayInputStream(java.io.ByteArrayInputStream) Binary(com.dexels.navajo.document.types.Binary) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session)

Example 5 with AttachmentMapInterface

use of com.dexels.navajo.adapter.mailmap.AttachmentMapInterface in project navajo by Dexels.

the class ResourceMailAdapter method store.

@Override
public void store() throws MappableException, UserException {
    MailResource resource = MailResourceFactory.getInstance().getMailResource(resourceName);
    if (resource == null) {
        throw new MappableException("Missing mail resource with name: " + resourceName);
    }
    String body = this.text;
    if (this.xslFile != null && !"".equals(xslFile)) {
        try {
            body = parseWithXslStyle(xslFile);
        } catch (Throwable e) {
            throw new MappableException("Error parsing using XSL file", e);
        }
    }
    List<AttachmentMapInterface> attach = new ArrayList<AttachmentMapInterface>(attachments);
    resource.send(sender, this.recipientArray, this.ccArray, this.bccArray, this.subject, body, attach, contentType, relatedMultipart);
}
Also used : MappableException(com.dexels.navajo.script.api.MappableException) ArrayList(java.util.ArrayList) AttachmentMapInterface(com.dexels.navajo.adapter.mailmap.AttachmentMapInterface)

Aggregations

AttachmentMapInterface (com.dexels.navajo.adapter.mailmap.AttachmentMapInterface)5 Binary (com.dexels.navajo.document.types.Binary)4 MappableException (com.dexels.navajo.script.api.MappableException)4 UserException (com.dexels.navajo.script.api.UserException)4 BinaryDataSource (com.dexels.navajo.datasource.BinaryDataSource)3 DataHandler (javax.activation.DataHandler)3 FileDataSource (javax.activation.FileDataSource)3 BodyPart (javax.mail.BodyPart)3 Multipart (javax.mail.Multipart)3 Session (javax.mail.Session)3 InternetAddress (javax.mail.internet.InternetAddress)3 MimeBodyPart (javax.mail.internet.MimeBodyPart)3 MimeMessage (javax.mail.internet.MimeMessage)3 MimeMultipart (javax.mail.internet.MimeMultipart)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 Document (org.w3c.dom.Document)2 File (java.io.File)1 URL (java.net.URL)1