Package javax.mail.internet

Examples of javax.mail.internet.MimeBodyPart


            MimeMultipart mp = new MimeMultipart(ds);
            for(int i = 0; i < mp.getCount(); i++) {

                // Get an individual part. This contains all the versioned
                // values for a particular key referenced by content-location
                MimeBodyPart part = (MimeBodyPart) mp.getBodyPart(i);

                // Get the key
                String contentLocation = part.getHeader("Content-Location")[0];
                String base64Key = contentLocation.split("/")[2];
                ByteArray key = new ByteArray(RestUtils.decodeVoldemortKey(base64Key));

                if(logger.isDebugEnabled()) {
                    logger.debug("Content-Location : " + contentLocation);
                    logger.debug("Base 64 key : " + base64Key);
                }

                // Create an array list for holding all the (versioned values)
                List<Versioned<byte[]>> valueResultList = new ArrayList<Versioned<byte[]>>();

                /*
                 * Get the nested Multi-part object. This contains one part for each unique versioned value.
                 *
                 * GetContent method can corrupt the embedded data, for example 0x8c be converted to 0xc2, 0x8c,
                 * hence use getInputStream.
                 *
                 * This thread tracks this question
                 * http://stackoverflow.com/questions/23023583/mimebodypart-getcontent-corrupts-binary-data
                 *
                 * getInputStream() : Return a decoded input stream for this Message's "content.
                 *
                 * getRawInputStream() : Return an InputStream to the raw data with any Content-Transfer-Encoding
                 * intact. This method is useful if the "Content-Transfer-Encoding" header is incorrect or corrupt,
                 * which would prevent the getInputStream method from returning the correct data. In such a case
                 * the application may use this method and attempt to decode the raw data itself.
                 *
                 */

                ByteArrayDataSource nestedDS = new ByteArrayDataSource(part.getInputStream(),
                                                                       "multipart/mixed");
                MimeMultipart valueParts = new MimeMultipart(nestedDS);

                for(int valueId = 0; valueId < valueParts.getCount(); valueId++) {

                    MimeBodyPart valuePart = (MimeBodyPart) valueParts.getBodyPart(valueId);
                    String serializedVC = valuePart.getHeader(RestMessageHeaders.X_VOLD_VECTOR_CLOCK)[0];
                    int contentLength = Integer.parseInt(valuePart.getHeader(RestMessageHeaders.CONTENT_LENGTH)[0]);

                    if(logger.isDebugEnabled()) {
                        logger.debug("Received serialized Vector Clock : " + serializedVC);
                    }

                    VectorClockWrapper vcWrapper = mapper.readValue(serializedVC,
                                                                    VectorClockWrapper.class);

                    // get the value bytes
                    InputStream input = valuePart.getInputStream();
                    byte[] bodyPartBytes = new byte[contentLength];
                    input.read(bodyPartBytes);

                    VectorClock clock = new VectorClock(vcWrapper.getVersions(),
                                                        vcWrapper.getTimestamp());
View Full Code Here


                LOG.trace("Attachment #{}: FileName: {}", i, attachmentFilename);
            }
            if (handler != null) {
                if (shouldAddAttachment(exchange, attachmentFilename, handler)) {
                    // Create another body part
                    BodyPart messageBodyPart = new MimeBodyPart();
                    // Set the data handler to the attachment
                    messageBodyPart.setDataHandler(handler);

                    if (attachmentFilename.toLowerCase().startsWith("cid:")) {
                        // add a Content-ID header to the attachment
                        // must use angle brackets according to RFC: http://www.ietf.org/rfc/rfc2392.txt
                        messageBodyPart.addHeader("Content-ID", "<" + attachmentFilename.substring(4) + ">");
                        // Set the filename without the cid
                        messageBodyPart.setFileName(attachmentFilename.substring(4));
                    } else {
                        // Set the filename
                        messageBodyPart.setFileName(attachmentFilename);
                    }

                    LOG.trace("Attachment #" + i + ": ContentType: " + messageBodyPart.getContentType());

                    if (contentTypeResolver != null) {
                        String contentType = contentTypeResolver.resolveContentType(attachmentFilename);
                        LOG.trace("Attachment #" + i + ": Using content type resolver: " + contentTypeResolver + " resolved content type as: " + contentType);
                        if (contentType != null) {
                            String value = contentType + "; name=" + attachmentFilename;
                            messageBodyPart.setHeader("Content-Type", value);
                            LOG.trace("Attachment #" + i + ": ContentType: " + messageBodyPart.getContentType());
                        }
                    }

                    // Set Disposition
                    messageBodyPart.setDisposition(partDisposition);
                    // Add part to multipart
                    multipart.addBodyPart(messageBodyPart);
                } else {
                    LOG.trace("shouldAddAttachment: false");
                }
View Full Code Here

        throws MessagingException, IOException {

        MimeMultipart multipartAlternative = new MimeMultipart("alternative");
        mimeMessage.setContent(multipartAlternative);

        MimeBodyPart plainText = new MimeBodyPart();
        plainText.setText(getAlternativeBody(configuration, exchange), determineCharSet(configuration, exchange));
        // remove the header with the alternative mail now that we got it
        // otherwise it might end up twice in the mail reader
        exchange.getIn().removeHeader(configuration.getAlternativeBodyHeader());
        multipartAlternative.addBodyPart(plainText);

        // if there are no attachments, add the body to the same mulitpart message
        if (!exchange.getIn().hasAttachments()) {
            addBodyToMultipart(configuration, multipartAlternative, exchange);
        } else {
            // if there are attachments, but they aren't set to be inline, add them to
            // treat them as normal. It will append a multipart-mixed with the attachments and the body text
            if (!configuration.isUseInlineAttachments()) {
                BodyPart mixedAttachments = new MimeBodyPart();
                mixedAttachments.setContent(createMixedMultipartAttachments(configuration, exchange));
                multipartAlternative.addBodyPart(mixedAttachments);
            } else {
                // if the attachments are set to be inline, attach them as inline attachments
                MimeMultipart multipartRelated = new MimeMultipart("related");
                BodyPart related = new MimeBodyPart();

                related.setContent(multipartRelated);
                multipartAlternative.addBodyPart(related);

                addBodyToMultipart(configuration, multipartRelated, exchange);

                addAttachmentsToMultipart(multipartRelated, Part.INLINE, exchange);
View Full Code Here

    }

    protected void addBodyToMultipart(MailConfiguration configuration, MimeMultipart activeMultipart, Exchange exchange)
        throws MessagingException, IOException {

        BodyPart bodyMessage = new MimeBodyPart();
        populateContentOnBodyPart(bodyMessage, configuration, exchange);
        activeMultipart.addBodyPart(bodyMessage);
    }
View Full Code Here

        try {
            //Create the message body
            MimeMultipart multipart = new MimeMultipart();
            //Add message as the first mime body part
            MimeBodyPart part = new MimeBodyPart();
            part.setContent(sout.toString(), "text/plain");
            part.setHeader(RFC2822Headers.CONTENT_TYPE, "text/plain");
            multipart.addBodyPart(part);

            //Add the original message as the second mime body part
            part = new MimeBodyPart();
            part.setContent(message.getContent(), message.getContentType());
            part.setHeader(RFC2822Headers.CONTENT_TYPE, message.getContentType());
            multipart.addBodyPart(part);

            //if set, attach the full stack trace
            if (attachStackTrace && mail.getErrorMessage() != null) {
                part = new MimeBodyPart();
                part.setContent(mail.getErrorMessage(), "text/plain");
                part.setHeader(RFC2822Headers.CONTENT_TYPE, "text/plain");
                multipart.addBodyPart(part);
            }

            reply.setContent(multipart);
            reply.setHeader(RFC2822Headers.CONTENT_TYPE, multipart.getContentType());
View Full Code Here

                case NONE: //NONE:
                    break;
            }
            MimeMultipart multipart = new MimeMultipart();
            //Add message as the first mime body part
            MimeBodyPart part       = new MimeBodyPart();
            part.setText(sout.toString());
            part.setDisposition("inline");
            multipart.addBodyPart(part);
            if(getAttachmentType() != NONE) {
                part = new MimeBodyPart();
                switch(getAttachmentType()) {
                    case HEADS: //HEADS:
                        part.setText(head);
                        break;
                    case BODY: //BODY:
                        try {
                            part.setText(message.getContent().toString());
                        } catch(Exception e) {
                            part.setText("body unavailable");
                        }
                        break;
                    case ALL: //ALL:
                        StringBuffer textBuffer =
                            new StringBuffer(1024)
                                .append(head)
                                .append("\n\n")
                                .append(message.toString());
                        part.setText(textBuffer.toString());
                        break;
                    case MESSAGE: //MESSAGE:
                        part.setContent(message, "message/rfc822");
                        break;
                }
                part.setDisposition("Attachment");
                multipart.addBodyPart(part);
            }
            reply.setContent(multipart);
            reply.setHeader(RFC2822Headers.CONTENT_TYPE, multipart.getContentType());
            reply.setRecipients(Message.RecipientType.TO, apparentlyTo);
View Full Code Here

                //This is a straight text message... just append the single part normally
                addToText(message);
            } else if (message.isMimeType("multipart/mixed")) {
                //Find the first body part, and determine what to do then.
                MimeMultipart multipart = (MimeMultipart)message.getContent();
                MimeBodyPart part = (MimeBodyPart)multipart.getBodyPart(0);
                attachFooter(part);
                //We have to do this because of a bug in JavaMail (ref id 4404733)
                message.setContent(multipart);
            } else if (message.isMimeType("multipart/alternative")) {
                //Find the HTML and text message types and add to each
                MimeMultipart multipart = (MimeMultipart)message.getContent();
                for (int i = 0; i < multipart.getCount(); i++) {
                    MimeBodyPart part = (MimeBodyPart)multipart.getBodyPart(i);
                    attachFooter(part);
                }
                //We have to do this because of a bug in JavaMail (ref id 4404733)
                message.setContent(multipart);
            } else {
View Full Code Here

      }
      msg.setRecipients(Message.RecipientType.TO, addresses);
      msg.setSubject(subject);
      msg.setSentDate(new Date());
      final Multipart mp = new MimeMultipart();
      final MimeBodyPart mbp1 = new MimeBodyPart();
      mbp1.setText(message);
      mp.addBodyPart(mbp1);
      if (paths != null && paths.length > 0) {
        for (final Path path : paths) {
          final MimeBodyPart mbp = new MimeBodyPart();
          final FileDataSource fds = new FileDataSource(path.toFile());
          mbp.setDataHandler(new DataHandler(fds));
          mbp.setFileName(fds.getName());
          mp.addBodyPart(mbp);
        }
      }
      // text attachment
      // MimeBodyPart mbp2 = new MimeBodyPart();
View Full Code Here

        //msg.setRecipients(Message.RecipientType.BCC, addressTo);
  
        MimeMultipart multipart = new MimeMultipart("alternative");
       
        if(attachmentURL!=null) {
          MimeBodyPart filePart = new MimeBodyPart();
          filePart.setFileName( attachmentFileName );
         
      URL url = new URL(attachmentURL);
      URLConnection connection = url.openConnection();
      connection.setRequestProperty( "User-Agent","OpenForum");
         
          ByteArrayDataSource ds = new ByteArrayDataSource(connection.getInputStream(), attachmentMimeType);
          filePart.setDataHandler(new DataHandler(ds));
          multipart.addBodyPart(filePart);
        }
       
        MimeBodyPart textPart = new MimeBodyPart();
        textPart.setContent(textMessage, "text/plain");
        multipart.addBodyPart(textPart);
       
        if(htmlMessage!=null) {
          MimeBodyPart htmlPart = new MimeBodyPart();
          String htmlData = htmlMessage;
          htmlPart.setContent(htmlData, "text/html");
          multipart.addBodyPart(htmlPart);
        }
       
        // Setting the Subject and Content Type
        msg.setSubject(subject);
View Full Code Here

                        return (String) message.getContent();
                    }
                } else if (message.getContent() instanceof Multipart) {
                    Multipart multipart = (Multipart) message.getContent();
                    if (multipart.getCount() == 2) {
                        MimeBodyPart textPart = (MimeBodyPart) multipart.getBodyPart(0);
                        if (textPart.getContent() instanceof String) {
                            if (StringUtils.startsWith(textPart.getContentType(), "text/plain")) {
                                return (String) textPart.getContent();
                            }
                        }
                    }
                }
            }
View Full Code Here

TOP

Related Classes of javax.mail.internet.MimeBodyPart

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.