Examples of BodyPart


Examples of javax.mail.BodyPart

     * @throws EmailException An error occured while adding the part.
     * @since 1.0
     */
    public Email addPart(MimeMultipart multipart, int index) throws EmailException
    {
            BodyPart bodyPart = createBodyPart();
        try
        {
            bodyPart.setContent(multipart);
            getContainer().addBodyPart(bodyPart, index);
        }
        catch (MessagingException me)
        {
            throw new EmailException(me);
View Full Code Here

Examples of javax.mail.BodyPart

        {
            throw new EmailException("Invalid message supplied");
        }
        try
        {
            BodyPart primary = getPrimaryBodyPart();

            if ((primary instanceof MimePart) && EmailUtils.isNotEmpty(charset))
            {
                ((MimePart) primary).setText(msg, charset);
            }
            else
            {
                primary.setText(msg);
            }
        }
        catch (MessagingException me)
        {
            throw new EmailException(me);
View Full Code Here

Examples of javax.mail.BodyPart

            {
                // before a multipart message can be sent, we must make sure that
                // the content for the main body part was actually set.  If not,
                // an IOException will be thrown during super.send().

                BodyPart body = this.getPrimaryBodyPart();
                try
                {
                    body.getContent();
                }
                catch (IOException e)
                {
                    // do nothing here.  content will be set to an empty string
                    // as a result.
View Full Code Here

Examples of javax.mail.BodyPart

    {
        if (EmailUtils.isEmpty(name))
        {
            name = ds.getName();
        }
        BodyPart bodyPart = createBodyPart();
        try
        {
            getContainer().addBodyPart(bodyPart);

            bodyPart.setDisposition(disposition);
            bodyPart.setFileName(name);
            bodyPart.setDescription(description);
            bodyPart.setDataHandler(new DataHandler(ds));
        }
        catch (MessagingException me)
        {
            throw new EmailException(me);
        }
View Full Code Here

Examples of javax.mail.BodyPart

     *
     * @return the created body part
     */
    protected BodyPart createBodyPart()
    {
        BodyPart bodyPart = new MimeBodyPart();
        return bodyPart;
    }
View Full Code Here

Examples of javax.mail.BodyPart

       
        MimeMultipart part = (MimeMultipart)msg.getContent();
       
        assertEquals("Should have two body items (message + attachment)", 2, part.getCount());
       
        BodyPart attach = part.getBodyPart(1);
        assertTrue("There should be a PDF named \"test.pdf\" attached", "test.pdf".equalsIgnoreCase(attach.getFileName()));       
    }
View Full Code Here

Examples of javax.mail.BodyPart

       
        MimeMultipart part = (MimeMultipart)msg.getContent();
       
        assertEquals("Should have two body items (message + attachment)", 2, part.getCount());
       
        BodyPart attach = part.getBodyPart(1);
        assertTrue("There should be a PDF named \"test.pdf\" attached", "test.pdf".equalsIgnoreCase(attach.getFileName()));       
    }
View Full Code Here

Examples of javax.mail.BodyPart

      throw new IllegalStateException("Error while retrieving the number of enclosed BodyPart objects.", e);
    }

    for (int i = 0; i < count; i++) {

      final BodyPart bp;

      try {
        bp = multipart.getBodyPart(i);
      }
      catch (MessagingException e) {
        throw new IllegalStateException("Error while retrieving body part.", e);
      }

      final String contentType;
      String filename;
      final String disposition;
      final String subject;

      try {

        contentType = bp.getContentType();
        filename    = bp.getFileName();
        disposition = bp.getDisposition();
        subject     = mailMessage.getSubject();

        if (filename == null && bp instanceof MimeBodyPart) {
          filename = ((MimeBodyPart) bp).getContentID();
        }

      }
      catch (MessagingException e) {
        throw new IllegalStateException("Unable to retrieve body part meta data.", e);
      }

      if (LOGGER.isInfoEnabled()) {
        LOGGER.info(String.format("BodyPart - Content Type: '%s', filename: '%s', disposition: '%s', subject: '%s'",
               new Object[]{contentType, filename, disposition, subject}));
      }

      if (Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
        LOGGER.info(String.format("Handdling attachment '%s', type: '%s'", filename, contentType));
      }

      final Object content;

      try {
        content = bp.getContent();
      }
      catch (IOException e) {
        throw new IllegalStateException("Error while retrieving the email contents.", e);
      }
      catch (MessagingException e) {
View Full Code Here

Examples of javax.mail.BodyPart

                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

Examples of javax.mail.BodyPart

            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
TOP
Copyright © 2018 www.massapi.com. 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.