Search in sources :

Example 1 with BinaryDataSource

use of com.dexels.navajo.datasource.BinaryDataSource 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 2 with BinaryDataSource

use of com.dexels.navajo.datasource.BinaryDataSource 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 3 with BinaryDataSource

use of com.dexels.navajo.datasource.BinaryDataSource 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)

Aggregations

AttachmentMapInterface (com.dexels.navajo.adapter.mailmap.AttachmentMapInterface)3 BinaryDataSource (com.dexels.navajo.datasource.BinaryDataSource)3 Binary (com.dexels.navajo.document.types.Binary)3 UserException (com.dexels.navajo.script.api.UserException)3 DataHandler (javax.activation.DataHandler)3 FileDataSource (javax.activation.FileDataSource)3 BodyPart (javax.mail.BodyPart)3 Multipart (javax.mail.Multipart)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 MappableException (com.dexels.navajo.script.api.MappableException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Session (javax.mail.Session)2 Document (org.w3c.dom.Document)2 Properties (java.util.Properties)1 MessagingException (javax.mail.MessagingException)1 AddressException (javax.mail.internet.AddressException)1