Package javax.mail

Examples of javax.mail.BodyPart


    public EmailMessage addAttachment(File file) throws MessagingException {
        return addAttachment(file.getName(), file);
    }

    public EmailMessage addAttachment(String attachmentName, File file) throws MessagingException {
        BodyPart attachmentPart = new MimeBodyPart();
        DataSource fileDataSource = new FileDataSource(file);
        attachmentPart.setDataHandler(new DataHandler(fileDataSource));
        attachmentPart.setFileName(attachmentName);
        _attachments.add(attachmentPart);
        return this;
    }
View Full Code Here


        return this;
    }

    public EmailMessage addAttachment(String attachmentName, InputStream stream)
            throws MessagingException {
        BodyPart attachmentPart = new MimeBodyPart(stream);
        attachmentPart.setFileName(attachmentName);
        _attachments.add(attachmentPart);
        return this;
    }
View Full Code Here

            // Add attachments
            for(BodyPart part: _attachments) {
                multipart.addBodyPart(part);
            }

            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent(_body.toString(), _mimeType);
            multipart.addBodyPart(messageBodyPart);

            message.setContent(multipart);
        } else {
            message.setContent(_body.toString(), _mimeType);
View Full Code Here

        // Create a Multipart
        Multipart multipart = new MimeMultipart( "alternative" );

        // Add text message
        BodyPart messageBodyPart = new MimeBodyPart();
        String text = "Summary\n-------\n\n" + summary + "\n\nDescription\n-----------\n\n" + description;
        messageBodyPart.setText( text );
        messageBodyPart.setDataHandler( new DataHandler( new ByteArrayDataSource( text,
                                                                                  "text/plain; charset=UTF8;" ) ) );
        multipart.addBodyPart( messageBodyPart );

        // Add ical
        messageBodyPart = new MimeBodyPart();
        String filename = "ical-" + type + "-" + taskId + ".ics";
        messageBodyPart.setFileName( filename );
        messageBodyPart.setHeader( "Content-Class",
                                   "urn:content-classes:calendarmessage" );
        messageBodyPart.setHeader( "Content-ID",
                                   "calendar_message" );
        String icalStr = getIcal( summary,
                                  description,
                                  startDate,
                                  priority,
                                  userInfo.getDisplayName( creator ),
                                  creatorEmail,
                                  type );

        messageBodyPart.setDataHandler( new DataHandler( new ByteArrayDataSource( icalStr,
                                                                                  "text/calendar; charset=UTF8; " ) ) );
        multipart.addBodyPart( messageBodyPart );

        message.setContent( multipart );
        message.saveChanges();
View Full Code Here

      }
      // Multipart.
      else
      {
        // Create the message part
        BodyPart messageBodyPart = new MimeBodyPart();

        // Fill the message
        messageBodyPart.setText(_content == null ? "" : _content);

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);

        if (_attachment1 != null)
View Full Code Here

  }

  private void _addAttachment(Multipart multipart, UploadedFile file)
    throws MessagingException
  {
    BodyPart messageBodyPart = new MimeBodyPart();
    DataSource source = new UploadedFileDataSource(file);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(file.getFilename());
    multipart.addBodyPart(messageBodyPart);
  }
View Full Code Here

            boolean found = false;
           
            MimeMultipart mm = (MimeMultipart)content;
            for (int i=0;i<mm.getCount();i++)
            {
              BodyPart bp = mm.getBodyPart(i);
             
              if (!found && (bp instanceof MimeBodyPart))
              {
                MimeBodyPart mbp = (MimeBodyPart)bp;
                String type = mbp.getContentType();
               
                if (type.startsWith(_HTML_TEXT) ||
                    type.startsWith(_PLAIN_TEXT) )
                {
                  found = true;
                  count = mbp.getLineCount();
                  content = mbp.getContent();
                  contentType = type;
                 
                  // be happy with the first thing we find either plain
                  // text or html, and skip over it as an attachment
                  continue;
                }
              }

              // OK, now let's see if it's an attachment
              String disp = bp.getDisposition();
              if (disp == null || disp.equalsIgnoreCase(Part.ATTACHMENT))
              {
                attachments.add(bp);
              }
            }
View Full Code Here

        sm.setReplyTo(replyTo);
        sm.setFrom(new InternetAddress(this.fromAddress));
        sm.setSubject(this.subject);

        // process mail-body
        BodyPart messageBodyPart = new MimeBodyPart();

        // decide, if to take content from source or plain text
        // from variable to build mailbody
        String messageString;
        if (this.bodyURI != null) {
            Source      inSrc   = resolver.resolveURI(this.bodyURI);
            this.usedSources.add(inSrc);
            InputStream inStr   = inSrc.getInputStream();
            byte[]      byteArr = new byte[inStr.available()];
            inStr.read(byteArr);
           
            messageString = new String(byteArr);
           
            // String mailBody = new String(byteArr);
            // this.setMessageBody(messageBodyPart, mailBody, this.bodyMimeType);
        } else {
            messageString = this.body;
            // this.setMessageBody(messageBodyPart, this.body, this.bodyMimeType);           
        }

        // make it a simple plain text message in the case of a set plain/text
        // mime-type and any attachements
        if("text/plain".equals(this.bodyMimeType) && this.attachments.size() == 0) {    
            sm.setText(messageString);
        }
        // add message as message body part
        else {
            messageBodyPart.setContent(messageString, this.bodyMimeType);
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);

            // process attachments
            Iterator i = this.attachments.iterator();
            while (i.hasNext()) {
                AttachmentDescriptor aD = (AttachmentDescriptor) i.next();
                messageBodyPart = new MimeBodyPart();
   
                if (!aD.isTextContent()) {
                    Source inputSource = resolver.resolveURI(aD.isURLSource() ? aD.strAttrSrc : aD.strAttrFile);
                    this.usedSources.add(inputSource);
   
                    DataSource dataSource = new SourceDataSource(inputSource, aD.strAttrMimeType, aD.strAttrName);

                    messageBodyPart.setDataHandler(new DataHandler(dataSource));
                } else {
                    messageBodyPart.setContent(aD.strContent, aD.strAttrMimeType);
                }
   
                messageBodyPart.setFileName(aD.strAttrName);
                multipart.addBodyPart(messageBodyPart);
            }
            sm.setContent(multipart);
        }
View Full Code Here

                LOG.trace("Attachment #" + i + ": FileName: " + 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

            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

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.