Search in sources :

Example 76 with DataSource

use of javax.activation.DataSource in project TranskribusCore by Transkribus.

the class SendMail method createMultiPartMessage.

public static Multipart createMultiPartMessage(String message, File[] atts) throws MessagingException {
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    // fill message
    messageBodyPart.setText(message);
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    // Part two is attachment
    messageBodyPart = new MimeBodyPart();
    for (File f : atts) {
        DataSource source = new FileDataSource(f);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(f.getName());
        multipart.addBodyPart(messageBodyPart);
    }
    return multipart;
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource)

Example 77 with DataSource

use of javax.activation.DataSource 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 78 with DataSource

use of javax.activation.DataSource in project i2p.i2p-bote by i2p.

the class MultipartFilter method processRequest.

/**
 * Saves all uploaded files in a request to the temporary directory so
 * they can be attached to an email.<br/>
 * All other request parameters are passed down the filter chain as a
 * new request.
 * @param request
 * @return a new <code>HttpServletRequest</code>
 * @throws IOException
 * @throws ServletException
 */
private HttpServletRequest processRequest(final HttpServletRequest request) throws IOException, ServletException {
    // only process multipart requests
    if (!isMultipart(request))
        return request;
    DataSource dataSource = new DataSource() {

        @Override
        public OutputStream getOutputStream() throws IOException {
            return null;
        }

        @Override
        public String getName() {
            return "HTTP request data source";
        }

        @Override
        public InputStream getInputStream() throws IOException {
            return request.getInputStream();
        }

        @Override
        public String getContentType() {
            return request.getContentType();
        }
    };
    Map<String, String[]> nonFileParameters = new HashMap<String, String[]>();
    try {
        // <nasty hack>
        // JavaMail assumes ISO-8859-1 for text parts when parsing multipart data.
        // There seems to be no easy way to override this behavior, or to change the
        // content type of a BodyPart. Since the ISO-8859-1 encoding will mess up
        // non-ascii UTF-8 characters, we change all "text/plain" parts to
        // "text/plain; charset=UTF-8" to keep JavaMail from using the ISO-8859-1
        // default.
        // See also com.sun.mail.handlers.text_plain.getCharset(String)
        MimeMultipart multipart = new MimeMultipart(dataSource) {

            @Override
            public BodyPart getBodyPart(int index) throws MessagingException {
                BodyPart part = super.getBodyPart(index);
                part.setDataHandler(new DataHandler(new MimePartDataSource((MimeBodyPart) part) {

                    @Override
                    public String getContentType() {
                        String contentType = super.getContentType();
                        if ("text/plain".equals(contentType))
                            return "text/plain; charset=UTF-8";
                        else
                            return contentType;
                    }
                }));
                return part;
            }
        };
        for (int i = 0; i < multipart.getCount(); i++) {
            BodyPart bodyPart = multipart.getBodyPart(i);
            String origFilename = bodyPart.getFileName();
            if (origFilename != null && origFilename.length() > 0) {
                // write the temporary file
                InputStream input = bodyPart.getInputStream();
                File tempFile = File.createTempFile("i2pbote_attachment_", ".tmp");
                // under normal circumstances, the temp files are deleted after the email is added to the outbox
                tempFile.deleteOnExit();
                FileOutputStream output = new FileOutputStream(tempFile);
                try {
                    Util.copy(input, output);
                } finally {
                    output.close();
                }
                UploadedFile uploadedFile = new UploadedFile(origFilename, tempFile.getAbsolutePath());
                String paramName = getParamName(bodyPart);
                request.setAttribute(paramName, uploadedFile);
            } else {
                // not a file, add the parameter to the map
                String paramName = getParamName(bodyPart);
                Object content = bodyPart.getContent();
                String paramValue = content == null ? "" : content.toString();
                add(nonFileParameters, paramName, paramValue);
            }
        }
    } catch (MessagingException e) {
        throw new ServletException("Can't write uploaded data to a temp file.", e);
    }
    // Wrap the request with the parameter map which we just created and return it.
    return wrapRequest(request, nonFileParameters);
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) HashMap(java.util.HashMap) MessagingException(javax.mail.MessagingException) InputStream(java.io.InputStream) DataHandler(javax.activation.DataHandler) MimePartDataSource(javax.mail.internet.MimePartDataSource) DataSource(javax.activation.DataSource) ServletException(javax.servlet.ServletException) MimeMultipart(javax.mail.internet.MimeMultipart) FileOutputStream(java.io.FileOutputStream) MimePartDataSource(javax.mail.internet.MimePartDataSource) File(java.io.File)

Example 79 with DataSource

use of javax.activation.DataSource in project quickstarts by jboss-switchyard.

the class InternalEchoServiceBean method echoImage.

@Override
public String echoImage(String fileName) {
    String newFileName = "external-switchyard.png";
    DataSource image = message.getAttachment(fileName);
    // Something is wrong in Camel it throws StackOverFlow error.
    // message.removeAttachment(fileName);
    message.addAttachment(newFileName, image);
    newFileName = _echoService.echoImage(newFileName);
    return newFileName;
}
Also used : DataSource(javax.activation.DataSource)

Example 80 with DataSource

use of javax.activation.DataSource in project orientdb by orientechnologies.

the class OMailPlugin method addAttachment.

/**
 * Adds a file as an attachment to the email's content
 *
 * @param multipart
 * @param filePath
 * @throws MessagingException
 */
private void addAttachment(final Multipart multipart, final String filePath) throws MessagingException {
    MimeBodyPart attachPart = new MimeBodyPart();
    DataSource source = new FileDataSource(filePath);
    attachPart.setDataHandler(new DataHandler(source));
    attachPart.setFileName(new File(filePath).getName());
    multipart.addBodyPart(attachPart);
}
Also used : FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) File(java.io.File) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource)

Aggregations

DataSource (javax.activation.DataSource)196 DataHandler (javax.activation.DataHandler)114 FileDataSource (javax.activation.FileDataSource)60 MimeBodyPart (javax.mail.internet.MimeBodyPart)53 MimeMultipart (javax.mail.internet.MimeMultipart)48 MimeMessage (javax.mail.internet.MimeMessage)39 InputStream (java.io.InputStream)37 IOException (java.io.IOException)36 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)35 ByteArrayInputStream (java.io.ByteArrayInputStream)33 File (java.io.File)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)30 Test (org.junit.Test)30 InternetAddress (javax.mail.internet.InternetAddress)27 Properties (java.util.Properties)26 MessagingException (javax.mail.MessagingException)25 Multipart (javax.mail.Multipart)24 Session (javax.mail.Session)21 BodyPart (javax.mail.BodyPart)19 ArrayList (java.util.ArrayList)16