Package javax.mail

Examples of javax.mail.BodyPart


                        this.message.setText(this.body);
                    }
                }
            } else {
                Multipart multipart = new MimeMultipart();
                BodyPart bodypart = null;
                if (this.body != null) {
                    bodypart = new MimeBodyPart();
                    bodypart.setText(this.body);
                    multipart.addBodyPart(bodypart);
                }
                this.message.setContent(multipart);

                for (Iterator i = this.attachmentList.iterator(); i.hasNext();) {
                    a = (Attachment) i.next();
                    DataSource ds = null;
                    if (a.isURL) {
                        String name = (String) a.getObject();
                        Source src = resolver.resolveURI(name);
                        sourcesList.add(src);
                        if (src.exists()) {
                            ds = new SourceDataSource(
                                    src,
                                    a.getType(src.getMimeType()),
                                    a.getName(name.substring(name.lastIndexOf('/') + 1)));
                        }
                    } else {
                        if (a.getObject() instanceof Part) {
                            Part part = (Part) a.getObject();
                            ds = new FilePartDataSource(
                                    part,
                                    a.getType(part.getMimeType()),
                                    a.getName(part.getUploadName()));
                        } else {
                            // TODO: other classes?
                            throw new AddressException("Not yet supported: " + a.getObject());
                        }
                    }

                    bodypart = new MimeBodyPart();
                    bodypart.setDataHandler(new DataHandler(ds));
                    bodypart.setFileName(ds.getName());
                    multipart.addBodyPart(bodypart);
                }
            }

            Transport.send(this.message);
View Full Code Here


            return UtilMisc.toList((String) c);
        } else if (c instanceof Multipart) {
            List<String> textContent = FastList.newInstance();
            int count = ((Multipart) c).getCount();
            for (int i = 0; i < count; i++) {
                BodyPart bp = ((Multipart) c).getBodyPart(i);
                textContent.addAll(this.getBodyText(bp));
            }
            return textContent;
        } else {
            return FastList.newInstance();
View Full Code Here

   * attachments).
   */

  public boolean respond(Request request) throws IOException {
    Hashtable queryData;
    BodyPart tmpPart;
    Properties props = request.props;

    /* Retrieve our query data string */

    queryData = request.getQueryData();
  
    /* Determine if this is a getPart request */

    if (request.url.startsWith(props.getProperty(prefix + "parturl","/getPart"))) {

      /* Use the partNum to retrieve the BodyPart object from the Session Manager */
      request.log(Server.LOG_DIAGNOSTIC, prefix, "Looking for Part " + props.getProperty(session)
  + " " + queryData.get("PartNum") + " [" + session + "]");

      tmpPart = (BodyPart) SessionManager.get(props.getProperty(session),
                                              queryData.get("PartNum"));

      /* If we have no BodyPart, we can't do anything, return false */

      if (tmpPart == null) {
        request.log(Server.LOG_DIAGNOSTIC, prefix, "no part: " + queryData.get("PartNum"));
        return false;
      }

      try {
        /* Use BodyPart object to stream attachment to browser for handling */

        request.sendResponse(tmpPart.getInputStream(),-1,tmpPart.getContentType(),-1);
    
        /* Return true to denote we've handled this request */

        return true;
      } catch (MessagingException e) {
View Full Code Here

    String headerlist,startmsg,msglimit;
    String[] tmpHdr;
    long startmillis,endmillis;
    int[] msgnumbers;
    int i = 0;
    BodyPart tmpPart;
    String tmpToken;
    StringBuffer partcount;
    Message[] tmpMsgs = null;
    RefileInfo tmpRefile = null;
    Properties props = hr.request.props;
    String partURL = hr.get("parturl","/getPart");

    hr.killToken();

    /* Get a copy of the requested server connection */

    tmpEmailConn = (EmailConnection) serverConnections.get(connHandle);

    if (tmpEmailConn == null) {
      handleFatalError(hr,"No server connection");
      return;
    }

    /* Check to see if we are still authenticated */

    if (!tmpEmailConn.isAuthenticated) {
      handleFatalError(hr,"Not authenticated");
      return;
    }

    /* Get the folder name to operate on */

    folderName = hr.get("foldername");

    /* Check our special case (INBOX) */

    if (!("INBOX".equals(folderName)) && !("inbox".equals(folderName))) {
      if ((tmpEmailConn.defaultDir != null) && (folderName != null)) {
        folderName = tmpEmailConn.defaultDir + "/" + folderName;
      }
    }

    hr.request.log(Server.LOG_DIAGNOSTIC,connHandle,"folderName in <message> tag = " +
                   folderName);
    hr.request.log(Server.LOG_DIAGNOSTIC,connHandle,"defaultDir in <message> tag = " +
                   tmpEmailConn.defaultDir);

    /* Get the selected folder */

    try {
      selectFolder(hr,tmpEmailConn,folderName);
    } catch (FolderNotFoundException e) {
      props.put(connHandle + "mailError","Folder: " + folderName + " was not found");

      hr.request.log(Server.LOG_DIAGNOSTIC,connHandle,"Folder: " + folderName + " not found");
      return;
    } catch (ReadOnlyFolderException e) {
      props.put(connHandle + "mailError","Folder: " + folderName +
     " is not a valid mailbox");

      hr.request.log(Server.LOG_DIAGNOSTIC,connHandle,"Folder: " + folderName + e.getMessage());
      return;
    } catch (MessagingException e) {
      handleFatalError(hr,"selecting folder " + folderName, e);
      return;
    }

    /* Determine our action */

    if ("getheaders".equals(hr.get("action"))) {


      /* Get the requested message objects and do a prefetch of our requested headers */

      headerlist = hr.get("headerlist");

      try {
        startmillis = System.currentTimeMillis();

        tmpMsgs = tmpEmailConn.currentFolder.getMessageObjects(hr);

        endmillis = System.currentTimeMillis();

        hr.request.log(Server.LOG_DIAGNOSTIC,connHandle,(endmillis-startmillis) +
                       " milliseconds to retrieve message objects");

        fetchHeaderInfo(hr,tmpMsgs,tmpEmailConn,headerlist);
      } catch (MessagingException e) {
        handleFatalError(hr,"Fetching messages", e);
        return;
      }
    } else if ("getnewheaders".equals(hr.get("action"))) {
      /* Get the new message objects and do a prefetch of our requested headers */

      headerlist = hr.get("headerlist");

      try {
        tmpMsgs = tmpEmailConn.currentFolder.getNewMessageObjects(hr);

        if (tmpMsgs != null) {
          fetchHeaderInfo(hr,tmpMsgs,tmpEmailConn,headerlist);
        }
      } catch (MessagingException e) {
        handleFatalError(hr,"Fetching new messages", e);
        return;
      }
    } else if ("getmsg".equals(hr.get("action"))) {
      msgNum = hr.get("msgnum");
      headerlist = hr.get("headerlist");
      hr.request.log(Server.LOG_DIAGNOSTIC,connHandle,"MsgNum = " + msgNum);

      /* Get the message & headers requested */

      try {
        tmpMsgs = tmpEmailConn.currentFolder.getMessageObjects(hr,msgNum);
        getMsgHeaders(hr,tmpMsgs[0],headerlist);
      } catch (MessagingException e) {
        handleFatalError(hr,"Fetching message headers", e);
        return;
      }

      /* Put the message server's msg number for this message into a return prop */

      props.put(connHandle + "msgnum",Integer.toString(tmpMsgs[0].getMessageNumber()));

      try {
        /* Get the content type and put it into a property */

        props.put(connHandle + msgNum + ".msgmimetype",tmpMsgs[0].getContentType());

        /* Get the content object for the request message */

        msgContent = tmpMsgs[0].getContent();

        /* Determine if it is a multipart message */ /* XXXX */
      
        if (msgContent instanceof String) {
          props.put(connHandle + msgNum + ".body",msgContent);
        } else if (msgContent instanceof Multipart) {
          partcount = new StringBuffer();
   
          /* Take care of the body */

          tmpMsgContent = (((Multipart) msgContent).getBodyPart(0)).getContent();

          if (tmpMsgContent instanceof String) {
            props.put(connHandle + msgNum + ".body",tmpMsgContent);
            partIndex = 1;
          } else {
            partIndex = 0;
          }

          /* Populate multipart attachment properties - and put our objects into
           * the sessionManager for the handler to pickup.
           */

          partCount = ((Multipart) msgContent).getCount();

          for (;partIndex<partCount;partIndex++) {
            tmpPart = ((Multipart) msgContent).getBodyPart(partIndex);

            /* put our BodyPart object into the session manager for the
             * Handler to find
             */

            SessionManager.put(hr.sessionId,"Part" + partIndex,tmpPart);
            hr.request.log(Server.LOG_DIAGNOSTIC, hr.prefix,
                       "creating part: " + hr.sessionId + " " + "Part" +  partIndex);

            /* Populate the properties for partname & parturl */

            if (tmpPart.getFileName() != null) {
              props.put(connHandle + msgNum + "." + partIndex +
                        ".partname",tmpPart.getFileName());
              props.put(connHandle + msgNum + "." + partIndex + ".parturl",
                        partURL + "/" + tmpPart.getFileName());
            } else if (tmpPart.getDescription() != null) {
              props.put(connHandle + msgNum + "." + partIndex +
                        ".partname",tmpPart.getDescription());
              props.put(connHandle + msgNum + "." + partIndex + ".parturl",
                        partURL + "/" + tmpPart.getDescription());
            } else {
              props.put(connHandle + msgNum + "." + partIndex +
                        ".partname","No Name");
              props.put(connHandle + msgNum + "." + partIndex + ".parturl",
                        partURL + "/NoName");
View Full Code Here

    public static String toString(Message message) throws MessagingException, IOException {
        Object content = message.getContent();
        if (content instanceof MimeMultipart) {
            MimeMultipart multipart = (MimeMultipart) content;
            if (multipart.getCount() > 0) {
                BodyPart part = multipart.getBodyPart(0);
                content = part.getContent();
            }
        }
        if (content != null) {
            return content.toString();
        }
View Full Code Here

     */
    @Converter
    public static String toString(Multipart multipart) throws MessagingException, IOException {
        int size = multipart.getCount();
        for (int i = 0; i < size; i++) {
            BodyPart part = multipart.getBodyPart(i);
            if (part.getContentType().startsWith("text")) {
                return part.getContent().toString();
            }
        }
        return null;
    }
View Full Code Here

        emailToInternetAddressArray(toEmailAddresses));
    message.setRecipients(Message.RecipientType.CC,
        emailToInternetAddressArray(ccEmailAddresses));
    message.addFrom(InternetAddress.parse(fromAddress));
    message.setSentDate(new Date());
    BodyPart mainBody = new MimeBodyPart();
    mainBody.setContent(content, "text/html;charset=gbk");
    multipart.addBodyPart(mainBody);
    for (Entry<String, String> e : mailAttachment.entrySet()) {
      BodyPart bodyPart = new MimeBodyPart();
      bodyPart.setDataHandler(new DataHandler(new FileDataSource(e
          .getKey())));
      bodyPart.setFileName(e.getValue());
      bodyPart.setHeader("Content-ID", e.getValue());
      multipart.addBodyPart(bodyPart);
    }
    message.setContent(multipart);
    message.saveChanges();
    Transport.send(message, message.getAllRecipients());
View Full Code Here

        emailToInternetAddressArray(toEmailAddresses));
    message.setRecipients(Message.RecipientType.CC,
        emailToInternetAddressArray(ccEmailAddresses));
    message.addFrom(InternetAddress.parse(fromAddress));
    message.setSentDate(new Date());
    BodyPart mainBody = new MimeBodyPart();
    mainBody.setContent(content, "text/html;charset=gbk");
    multipart.addBodyPart(mainBody);
    for (Entry<String, String> e : mailAttachment.entrySet()) {
      BodyPart bodyPart = new MimeBodyPart();
      bodyPart.setDataHandler(new DataHandler(new FileDataSource(e
          .getKey())));
      bodyPart.setFileName(e.getValue());
      bodyPart.setHeader("Content-ID", e.getValue());
      multipart.addBodyPart(bodyPart);
    }
    message.setContent(multipart);
    message.saveChanges();
    Transport.send(message, message.getAllRecipients());
View Full Code Here

            // select the plain text bodypart
            String messageBody = null;
            if (wrapper.getMainPartCount() > 1) {
                for (int ind=0; ind < wrapper.getMainPartCount(); ind++) {
                    BodyPart p = wrapper.getPart(ind + "");
                    if (p.getContentType().toLowerCase().indexOf("text/plain") > -1) {
                        messageBody = (String) p.getContent();
                        break;
                    }
                }
            }
View Full Code Here

            String attachmentFilename = entry.getKey();
            DataHandler handler = entry.getValue();
            if (handler != null) {
                if (shouldOutputAttachment(camelMessage, attachmentFilename, handler)) {
                    // Create another body part
                    BodyPart messageBodyPart = new MimeBodyPart();
                    // Set the data handler to the attachment
                    messageBodyPart.setDataHandler(handler);
                    // Set the filename
                    messageBodyPart.setFileName(attachmentFilename);
                    // Set Disposition
                    messageBodyPart.setDisposition(Part.ATTACHMENT);
                    // Add part to multipart
                    multipart.addBodyPart(messageBodyPart);
                }
            }
        }
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.