Example usage for javax.mail.util ByteArrayDataSource ByteArrayDataSource

List of usage examples for javax.mail.util ByteArrayDataSource ByteArrayDataSource

Introduction

In this page you can find the example usage for javax.mail.util ByteArrayDataSource ByteArrayDataSource.

Prototype

public ByteArrayDataSource(String data, String type) throws IOException 

Source Link

Document

Create a ByteArrayDataSource with data from the specified String and with the specified MIME type.

Usage

From source file:org.openehealth.ipf.platform.camel.lbs.cxf.process.ServiceBean.java

private Object[] postMe(Exchange exchange) throws Exception {
    List<?> params = exchange.getIn().getBody(List.class);
    Holder<String> name = (Holder<String>) params.get(0);
    Holder<DataHandler> attachInfo = (Holder<DataHandler>) params.get(1);
    DataHandler onewayAttach = (DataHandler) params.get(2);

    name.value = "Greetings from Apache Camel!!!! Request was " + name.value;

    String contentType = attachInfo.value.getContentType();
    String resultContent = toString(attachInfo.value) + toString(onewayAttach);

    DataSource outDataSource = new ByteArrayDataSource(resultContent.getBytes(), contentType);
    attachInfo.value = new DataHandler(outDataSource);

    return new Object[] { null, name, attachInfo };
}

From source file:es.caib.sgtsic.xml.XmlManager.java

public DataHandler generateXml(T item) throws JAXBException {

    byte[] b = marshal(item).toByteArray();
    String mimetype = "application/xml";
    DataSource ds = new ByteArrayDataSource(b, mimetype);
    return new DataHandler(ds);

}

From source file:org.vosao.utils.EmailUtil.java

/**
 * Send email with html content and attachments.
 * @param htmlBody/* w ww. j a v a  2s.c  o  m*/
 * @param subject
 * @param fromAddress
 * @param fromText
 * @param toAddress
 * @return null if OK or error message.
 */
public static String sendEmail(final String htmlBody, final String subject, final String fromAddress,
        final String fromText, final String toAddress, final List<FileItem> files) {

    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    try {
        Multipart mp = new MimeMultipart();
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(htmlBody, "text/html");
        htmlPart.setHeader("Content-type", "text/html; charset=UTF-8");
        mp.addBodyPart(htmlPart);
        for (FileItem item : files) {
            MimeBodyPart attachment = new MimeBodyPart();
            attachment.setFileName(item.getFilename());
            String mimeType = MimeType.getContentTypeByExt(FolderUtil.getFileExt(item.getFilename()));
            if (mimeType.equals("text/plain")) {
                mimeType = MimeType.DEFAULT;
            }
            DataSource ds = new ByteArrayDataSource(item.getData(), mimeType);
            attachment.setDataHandler(new DataHandler(ds));
            mp.addBodyPart(attachment);
        }
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(fromAddress, fromText));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress, toAddress));
        msg.setSubject(subject, "UTF-8");
        msg.setContent(mp);
        Transport.send(msg);
        return null;
    } catch (AddressException e) {
        return e.getMessage();
    } catch (MessagingException e) {
        return e.getMessage();
    } catch (UnsupportedEncodingException e) {
        return e.getMessage();
    }
}

From source file:es.caib.sgtsic.xml.XmlManager.java

public DataHandler generateXml(List<T> items) throws JAXBException {

    byte[] b = generateXmlString(items).getBytes();
    String mimetype = "text/plain";
    DataSource ds = new ByteArrayDataSource(b, mimetype);
    return new DataHandler(ds);

}

From source file:com.googlecode.ddom.frontend.saaj.impl.SwAProfile.java

@Override
public MessageDeserializer createDeserializer(InputStream in) {
    // TODO: optimize this and add support for lazy loading of attachments
    final MultipartReader mpr = new MultipartReader(in, boundary);
    return new MessageDeserializer() {
        public MimeHeaders getSOAPPartHeaders() throws IOException, SOAPException {
            if (!mpr.nextPart()) {
                throw new SOAPException("Message has no SOAP part");
            }//from  ww  w.  j  a va2 s . c  o m
            return readMimeHeaders(mpr);
        }

        public InputStream getSOAPPartInputStream() throws IOException, SOAPException {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            IOUtils.copy(mpr.getContent(), baos);
            return new ByteArrayInputStream(baos.toByteArray());
        }

        public AttachmentSet getAttachments() throws IOException, SOAPException {
            AttachmentSet attachments = new SimpleAttachmentSet();
            while (mpr.nextPart()) {
                // TODO: shouldn't we have a factory method in AttachmentSet?
                AttachmentPartImpl attachment = new AttachmentPartImpl(readMimeHeaders(mpr));
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                IOUtils.copy(mpr.getContent(), baos);
                attachment.setDataHandler(new DataHandler(
                        new ByteArrayDataSource(baos.toByteArray(), attachment.getContentType())));
                attachments.add(attachment);
            }
            return attachments;
        }
    };
}

From source file:com.mirth.connect.server.userutil.HTTPUtil.java

/**
 * Serializes an HTTP request body into XML. Multipart requests will also automatically be
 * parsed into separate XML nodes.//from w w  w .  ja va 2 s.c  o  m
 * 
 * @param httpBody
 *            The request body/payload string to parse.
 * @param contentType
 *            The MIME content type of the request.
 * @return The serialized XML string.
 * @throws MessagingException
 * @throws IOException
 * @throws DonkeyElementException
 * @throws ParserConfigurationException
 */
public static String httpBodyToXml(String httpBody, String contentType)
        throws MessagingException, IOException, DonkeyElementException, ParserConfigurationException {
    ContentType type = getContentType(contentType);
    Object content;

    if (type.getMimeType().startsWith(FileUploadBase.MULTIPART)) {
        content = new MimeMultipart(new ByteArrayDataSource(httpBody, type.toString()));
    } else {
        content = httpBody;
    }

    return HttpMessageConverter.contentToXml(content, type, true, null);
}

From source file:servlets.mailPDF_Demo.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {//w  w  w  . j  av a  2 s  . c  o m
        System.out.println("Reached @ mailPDF_Demo.");

        String pdfBase64 = request.getParameter("pdfBase64");
        String reportdate = request.getParameter("reportdate");
        String emailaddresses = request.getParameter("emailList");
        String emailMessage = request.getParameter("emailMessage");

        List<String> emailList = Arrays.asList(emailaddresses.split("\\s*,\\s*"));

        System.out.println("pdfBase54 is --------------\n" + pdfBase64 + "\n-----------------End");
        System.out.println(reportdate);
        System.out.println(emailaddresses);

        byte[] decodedBytes = decode(pdfBase64.substring(28));
        MultiPartEmail email = new MultiPartEmail();
        email.setSmtpPort(587);
        email.setDebug(false);
        email.setHostName("smtp.gmail.com");
        email.setAuthentication("reporting@groupnp.com", "D3sign2015");
        email.setTLS(true);

        for (String emailAddress : emailList) {

            email.addTo(emailAddress);
        }

        email.setFrom("reporting@groupnp.com");
        email.setSubject("Yougov Tracker (" + reportdate + ")");

        MimeMultipart part1 = new MimeMultipart();
        BodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.setContent(emailMessage, "text/html; charset=utf-8");
        part1.addBodyPart(messageBodyPart1);
        email.addPart(part1);

        MimeMultipart part2 = new MimeMultipart();
        BodyPart messageBodyPart2 = new MimeBodyPart();
        messageBodyPart2
                .setDataHandler(new DataHandler(new ByteArrayDataSource(decodedBytes, "application/pdf")));
        messageBodyPart2.removeHeader("Content-Transfer-Encoding");
        messageBodyPart2.addHeader("Content-Transfer-Encoding", "base64");
        messageBodyPart2.setFileName("HCL_DemoTracker_" + reportdate + ".pdf");
        part2.addBodyPart(messageBodyPart2);
        email.addPart(part2);

        email.send();

    } catch (EmailException | MessagingException ex) {
        Logger.getLogger(mailPDF_Demo.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:servlets.mailPDF_Bronze_ACS.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {//from   w  ww .  ja  va2  s .c  o  m
        System.out.println("Reached @ mailPDF_Bronze.");

        String pdfBase64 = request.getParameter("pdfBase64");
        String reportdate = request.getParameter("reportdate");
        String emailaddresses = request.getParameter("emailList");
        String emailMessage = request.getParameter("emailMessage");

        List<String> emailList = Arrays.asList(emailaddresses.split("\\s*,\\s*"));

        System.out.println("pdfBase54 is --------------\n" + pdfBase64 + "\n-----------------End");
        System.out.println(reportdate);
        System.out.println(emailaddresses);

        byte[] decodedBytes = decode(pdfBase64.substring(28));
        MultiPartEmail email = new MultiPartEmail();
        email.setSmtpPort(587);
        email.setDebug(false);
        email.setHostName("smtp.gmail.com");
        email.setAuthentication("reporting@groupnp.com", "D3sign2015");
        email.setTLS(true);

        for (String emailAddress : emailList) {

            email.addTo(emailAddress);
        }

        email.setFrom("reporting@groupnp.com");
        email.setSubject("ACS Tracker (" + reportdate + ")");

        MimeMultipart part1 = new MimeMultipart();
        BodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.setContent(emailMessage, "text/html; charset=utf-8");
        part1.addBodyPart(messageBodyPart1);
        email.addPart(part1);

        MimeMultipart part2 = new MimeMultipart();
        BodyPart messageBodyPart2 = new MimeBodyPart();
        messageBodyPart2
                .setDataHandler(new DataHandler(new ByteArrayDataSource(decodedBytes, "application/pdf")));
        messageBodyPart2.removeHeader("Content-Transfer-Encoding");
        messageBodyPart2.addHeader("Content-Transfer-Encoding", "base64");
        messageBodyPart2.setFileName("ACSTracker_" + reportdate + ".pdf");
        part2.addBodyPart(messageBodyPart2);
        email.addPart(part2);

        email.send();

    } catch (EmailException | MessagingException ex) {
        Logger.getLogger(mailPDF_Bronze_ACS.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:servlets.mailPDF_Bronze_Temenos.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {//from  w w  w .  ja  va2  s. com
        System.out.println("Reached @ mailPDF_Bronze.");

        String pdfBase64 = request.getParameter("pdfBase64");
        String reportdate = request.getParameter("reportdate");
        String emailaddresses = request.getParameter("emailList");
        String emailMessage = request.getParameter("emailMessage");

        List<String> emailList = Arrays.asList(emailaddresses.split("\\s*,\\s*"));

        System.out.println("pdfBase54 is --------------\n" + pdfBase64 + "\n-----------------End");
        System.out.println(reportdate);
        System.out.println(emailaddresses);

        byte[] decodedBytes = decode(pdfBase64.substring(28));
        MultiPartEmail email = new MultiPartEmail();
        email.setSmtpPort(587);
        email.setDebug(false);
        email.setHostName("smtp.gmail.com");
        email.setAuthentication("reporting@groupnp.com", "D3sign2015");
        email.setTLS(true);

        for (String emailAddress : emailList) {

            email.addTo(emailAddress);
        }

        email.setFrom("reporting@groupnp.com");
        email.setSubject("Temenos Tracker (" + reportdate + ")");

        MimeMultipart part1 = new MimeMultipart();
        BodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.setContent(emailMessage, "text/html; charset=utf-8");
        part1.addBodyPart(messageBodyPart1);
        email.addPart(part1);

        MimeMultipart part2 = new MimeMultipart();
        BodyPart messageBodyPart2 = new MimeBodyPart();
        messageBodyPart2
                .setDataHandler(new DataHandler(new ByteArrayDataSource(decodedBytes, "application/pdf")));
        messageBodyPart2.removeHeader("Content-Transfer-Encoding");
        messageBodyPart2.addHeader("Content-Transfer-Encoding", "base64");
        messageBodyPart2.setFileName("TemenosTracker_" + reportdate + ".pdf");
        part2.addBodyPart(messageBodyPart2);
        email.addPart(part2);

        email.send();

    } catch (EmailException | MessagingException ex) {
        Logger.getLogger(mailPDF_Bronze_Temenos.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:servlets.mailPDF_Bronze_Yougov.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {//from   w  ww . j  a  v  a2s . c  om
        System.out.println("Reached @ mailPDF_Bronze_Yougov.");

        String pdfBase64 = request.getParameter("pdfBase64");
        String reportdate = request.getParameter("reportdate");
        String emailaddresses = request.getParameter("emailList");
        String emailMessage = request.getParameter("emailMessage");

        List<String> emailList = Arrays.asList(emailaddresses.split("\\s*,\\s*"));

        System.out.println("pdfBase54 is --------------\n" + pdfBase64 + "\n-----------------End");
        System.out.println(reportdate);
        System.out.println(emailaddresses);

        byte[] decodedBytes = decode(pdfBase64.substring(28));
        MultiPartEmail email = new MultiPartEmail();
        email.setSmtpPort(587);
        email.setDebug(false);
        email.setHostName("smtp.gmail.com");
        email.setAuthentication("reporting@groupnp.com", "D3sign2015");
        email.setTLS(true);

        for (String emailAddress : emailList) {

            email.addTo(emailAddress);
        }

        email.setFrom("reporting@groupnp.com");
        email.setSubject("Yougov Tracker (" + reportdate + ")");

        MimeMultipart part1 = new MimeMultipart();
        BodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.setContent(emailMessage, "text/html; charset=utf-8");
        part1.addBodyPart(messageBodyPart1);
        email.addPart(part1);

        MimeMultipart part2 = new MimeMultipart();
        BodyPart messageBodyPart2 = new MimeBodyPart();
        messageBodyPart2
                .setDataHandler(new DataHandler(new ByteArrayDataSource(decodedBytes, "application/pdf")));
        messageBodyPart2.removeHeader("Content-Transfer-Encoding");
        messageBodyPart2.addHeader("Content-Transfer-Encoding", "base64");
        messageBodyPart2.setFileName("YougovTracker_" + reportdate + ".pdf");
        part2.addBodyPart(messageBodyPart2);
        email.addPart(part2);

        email.send();

    } catch (EmailException | MessagingException ex) {
        Logger.getLogger(mailPDF_Bronze_Yougov.class.getName()).log(Level.SEVERE, null, ex);
    }

}