Package javax.mail

Examples of javax.mail.BodyPart


        assertTrue("Content type not mulipart but " + response.getContentType(), response
                .getContentType().matches("multipart/related;\\s*boundary=\".*\""));

        // parse the multipart, check the second part is a geotiff
        Multipart multipart = getMultipart(response);
        BodyPart coveragePart = multipart.getBodyPart(1);
        assertEquals("image/tiff", coveragePart.getContentType());
    }
View Full Code Here


                    }
                } else {
                    message.setText(mailBody);
                }
            } else {
                BodyPart body = new MimeBodyPart();
                body.setText(mailBody);
                multipart.addBodyPart(body);
                for (File f : attachments) {
                    BodyPart attach = new MimeBodyPart();
                    attach.setFileName(f.getName());
                    attach.setDataHandler(new DataHandler(new FileDataSource(f.getAbsolutePath())));
                    multipart.addBodyPart(attach);
                }
                message.setContent(multipart);
            }
        }
View Full Code Here

            cdata.append(preamble);
        }
        child.setResponseData(cdata.toString(),child.getDataEncodingNoDefault());
        int count = mmp.getCount();
        for (int j=0; j<count;j++){
            BodyPart bodyPart = mmp.getBodyPart(j);
            final Object bodyPartContent = bodyPart.getContent();
            final String contentType = bodyPart.getContentType();
            SampleResult sr = new SampleResult();
            sr.setSampleLabel("Part: "+j);
            sr.setContentType(contentType);
            sr.setDataEncoding(RFC_822_DEFAULT_ENCODING);
            sr.setEncodingAndType(contentType);
View Full Code Here

            String boundary=ct.getParameter("boundary");
            for (int i = 0; i < multipart.getCount(); i++) { // throws ME
                sb.append("--");
                sb.append(boundary);
                sb.append("\n");
                BodyPart bodyPart = multipart.getBodyPart(i); // throws ME
                writeBodyPart(sb, bodyPart); // throws IOE, ME
            }
            sb.append("--");
            sb.append(boundary);
            sb.append("--");
            sb.append("\n");
        } else if(content instanceof BodyPart){
            BodyPart bodyPart = (BodyPart) content;
            writeBodyPart(sb, bodyPart); // throws IOE, ME
        } else if (content instanceof String){
            sb.append(content);
        } else {
            sb.append("Content has class: "+content.getClass().getCanonicalName());
View Full Code Here

            throws MessageException {

        MimeMultipart mimeMultipart = new MimeMultipart( "related" );
        try {
            // Assemble the body
            BodyPart body = new VolantisMimeBodyPart();
            BodyPart asset;
            body.setDataHandler(new DataHandler(
                    new ByteArrayDataSource(
                        message.getMessage(),
                        message.getMimeType())));
            body.setHeader("Content-Type", message.getMimeType());
            mimeMultipart.addBodyPart( body );

            // Assemble Assets
            Map assetMap = message.getAssetMap();
            Iterator i = assetMap.keySet().iterator();
            String mimeReference;
            MarinerURL url;
            MessageAsset messageAsset;

            // Sizes are in bytes
            Integer maxImageSize = message.getMaxFileSize();
            Integer maxMessageSize = message.getMaxMMSize();
            int actualMessageSize = 0;

            ArrayList imageAssets = new ArrayList();

            // Process each of the assets
            while( i.hasNext() ) {

                mimeReference = ( String ) i.next();
                // We can only retrieve assets that reside on the
                // server.
                messageAsset = ( MessageAsset ) assetMap.get( mimeReference );

                // Set appropriate headers
                asset = new MimeBodyPart();
                asset.setHeader( "Content-Disposition",
                        "inline; filename=\"" + mimeReference + "\"" );
                asset.setHeader( "Content-ID", "<" + mimeReference + ">" );

                if (messageAsset.isTextMessage()) {
                    // Handle simple textual content
                    String contentType = "text/plain";
                    if (message.getCharacterEncoding() != null) {
                            contentType += "; charset=" +
                                            message.getCharacterEncoding();
                    }
                    asset.setContent( messageAsset.getText(), contentType);
                    mimeMultipart.addBodyPart( asset );
                    actualMessageSize += determineAssetSize( asset );
                } else {
                    // Handle other content, but only if they reside on the
                    // server
                    if( messageAsset.getLocationType() ==
                            MessageAsset.ON_SERVER ) {
                        url = fixupURL( message.getBaseURL(),
                                new MarinerURL( messageAsset.getUrl() ) );
                        URL assetURL = new URL( url.getExternalForm() );

                        // Now sort out the content type and whether the asset
                        // exists or not...
                        try {
                            URLConnection connection =
                                    assetURL.openConnection();

                            // Check to see if a connection object has been
                            // successfully obtained
                            if( connection != null ) {
                                // Set the connection up
                                connection.setDoInput( true );
                                connection.setDoOutput( false );
                                connection.setAllowUserInteraction( false );

                                // Actually connect
                                connection.connect();

                                // Force the connection to be used
                                connection.getInputStream();

                                // Grab the content type from the stream to
                                // use in the mime body part of the asset
                                String contentType =
                                        connection.getContentType();
                                // By getting here, the asset exists and the
                                // connection succesfully opened so add the
                                // asset
                                asset.setDataHandler( new DataHandler( assetURL ) );
                                asset.setHeader( "Content-location",
                                        url.getExternalForm() );
                                asset.setHeader( "Content-Type", contentType );
                                mimeMultipart.addBodyPart( asset );

                                // Store the images for later size processing
                                imageAssets.add( asset );
                            }
                        } catch( MalformedURLException mue ) {
                            // Quietly ignore this asset
                            if( logger.isDebugEnabled() ) {
                                logger.debug( "Ignoring asset with malformed URL: " +
                                        url.toString() );
                            }
                        } catch( IOException ioe ) {
                            // Quietly ignore this asset
                            if( logger.isDebugEnabled() ) {
                                logger.debug( "Ignoring asset with URL that " +
                                        "doesn't exist: " + assetURL +
                                        " (" + url.toString() + ")" );
                            }
                        }
                    } else {
                        // For device based assets there's not much that can
                        // be set apart from where it is!
                        asset.setHeader( "Content-Location",
                                "file://" + messageAsset.getUrl() );
                    }
                }
            }
View Full Code Here

        // image sizes to the actual message size.
        Iterator assets = imageAssets.iterator();
        int assetSize;

        while( assets.hasNext() ) {
            BodyPart bodyPart = ( MimeBodyPart ) assets.next();

            // Check to see if this transcoded image is greater than
            // the allocated chunk from the remainder of the message.
            assetSize = determineAssetSize( bodyPart );
            actualMessageSize += assetSize;
View Full Code Here

        Iterator itr = attachments.iterator();
        while( itr.hasNext() ) {
            DeviceMessageAttachment attachment =
                    ( DeviceMessageAttachment ) itr.next();
            DataSource ds;
            BodyPart bodyPart;

            try {
                if( attachment.getValueType() == MessageAttachment.FILE ) {
                    // The attachment is a file type so create a FileDataSource
                    ds = new FileDataSource( attachment.getValue() );
                } else {
                    // If for some reason the URL does not return a content
                    // type then this type of datasource defaults to
                    // application/octet-stream.
                    ds = new URLDataSource( new URL( attachment.getValue() ) );
                }

                bodyPart = new MimeBodyPart();
                bodyPart.setDataHandler( new DataHandler( ds ) );
                bodyPart.setDisposition( Part.ATTACHMENT );
                bodyPart.setFileName( getFileName( attachment.getValue(),
                                                   attachment.getMimeType() ) );
            } catch( Exception me ) {
                // If we catch an exception above we might as well
                // skip this attachment
                logger.error( "body-part-creation-failure-skipping-attachment",
                              me );
                continue;
            }
            try {
                String bodyMimeType = bodyPart.getContentType();
                String messageMimeType = attachment.getMimeType();

                // Check if the Content-Type determined by the DataHandler
                // is the same or a more specific than the mimeType specified
                // in the attachment. E.g. "text/html; charset=us-ascii" as
                // against "text/html;" If it is more specific then leave as is,
                // if it is different then use the mime type specified in the
                // attachment
                if( messageMimeType != null &&
                        bodyMimeType.indexOf( messageMimeType ) == -1 ) {
                    bodyPart.addHeader( "Content-Type", messageMimeType );
                }
            } catch( Exception e ) {
                // Here we have failed to correct the mime type if required.
                // Leave the bodyPart as is and continue as this is not critical
                logger.warn( "attachment-mime-type-assignment-failure", e );
View Full Code Here

            message.setSentDate(new Date());
           
            // the content with the attachment
            final MimeMultipart multipart = new MimeMultipart();
            message.setContent(multipart);
            final BodyPart attachment = new MimeBodyPart();
            attachment.setFileName("unknown-devices.zip");
            final DataSource ds = new URLDataSource(file.toURL());
            attachment.setDataHandler(new DataHandler(ds));
            attachment.setHeader("Content-ID","");
            multipart.addBodyPart(attachment);
            return message;
        } catch (Exception e) {
            LOGGER.warn("unable-to-create-email", e);
            return null;
View Full Code Here

        // Create a test instance
        MimeMessageAssembler mimeMessageAssembler = new MimeMessageAssembler();

        // Test a successul calculation with this asset
        BodyPart asset = new MimeBodyPart() {
            // JavaDoc inherited
            public void writeTo(OutputStream outputStream)
                    throws IOException, MessagingException {
                // Have specific test behaviour here to avoid the need to
                // create a fully fledged body part which is not the point of
View Full Code Here

                       " doesn't start with multipart/mixed as expected",
                       multipart.getContentType().startsWith("multipart/mixed"));
            assertEquals("multipart should have 2 parts",
                         2,
                         multipart.getCount());
            BodyPart part1 = multipart.getBodyPart(0);
            BodyPart part2 = multipart.getBodyPart(1);

            assertTrue("body part 1 content type not as",
                       part1.getContentType().startsWith("text/html"));
            assertEquals("body part 1 size not as",
                         bodyContent.toString().length(),
                         part1.getSize());
            assertTrue("body part 2 content type " + part2.getContentType() +
                       " doesn't start with image/jpeg as expected",
                       part2.getContentType().startsWith("image/jpeg"));
            assertEquals("body part 2 size not as",
                         getClass().getResource(imageAsset.getValue()).
                         openConnection().getContentLength(),
                         part2.getSize());
        } catch (MessagingException e) {
            e.printStackTrace();
            throw e;
        } catch (IOException e) {
            e.printStackTrace();
View Full Code Here

TOP

Related Classes of javax.mail.BodyPart

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.