Package javax.mail

Examples of javax.mail.Part


    ArrayList<String> mimeTypes     = new ArrayList<String>();
    String viewFileName;
    try {
      dumpPart((Part)message, att, inl, imgs, nonImgs, mimeTypes, message.getSubject());
      for (String attachFileName: att.keySet()) {
        Part p = (Part) att.get(attachFileName);
        writeAttachment(p, attachFileName);
        ready.put(attachFileName, attachFileName);
        if (!imgs.containsKey(attachFileName) && !nonImgs.containsKey(attachFileName))
          addAttachment(attachFileName, p.getContentType());
      }
     
      if (inl.containsKey("text/html")) {
        viewFileName = prepareHTMLMessage(baseURL, inl, imgs, nonImgs, ready, mimeTypes);
      } else if (inl.containsKey("text/plain")) {
View Full Code Here


   * @param html
   *            whether to apply content type "text/html" for an HTML mail,
   *            using default content type ("text/plain") else
   */
  public void setText(final String text, boolean html) throws MessagingException {
    Part partToUse = null;
    if (this.mimeMultipart != null)
    {
      BodyPart bodyPart = null;
      for (int i = 0; i < this.mimeMultipart.getCount(); i++)
      {
        BodyPart bp = this.mimeMultipart.getBodyPart(i);
        if (bp.getFileName() == null)
        {
          bodyPart = bp;
        }
      }
      if (bodyPart == null)
      {
        MimeBodyPart mimeBodyPart = new MimeBodyPart();
        this.mimeMultipart.addBodyPart(mimeBodyPart);
        bodyPart = mimeBodyPart;
      }
      partToUse = bodyPart;
    }
    else
    {
      partToUse = this.mimeMessage;
    }
    if (html)
    {
      // need to use a javax.activation.DataSource (!) to set a text
      // with content type "text/html"
      partToUse.setDataHandler(new DataHandler(new DataSource() {
        public InputStream getInputStream() throws IOException {
          return new ByteArrayInputStream(text.getBytes());
        }

        public OutputStream getOutputStream() throws IOException {
          throw new UnsupportedOperationException("Read-only javax.activation.DataSource");
        }

        public String getContentType() {
          return "text/html";
        }

        public String getName() {
          return "text";
        }
      }));
    }
    else
    {
      partToUse.setText(text);
    }
  }
View Full Code Here

    protected void extractAttachmentsFromMultipart(Multipart mp, Map<String, DataHandler> map)
        throws javax.mail.MessagingException, IOException {

        for (int i = 0; i < mp.getCount(); i++) {
            Part part = mp.getBodyPart(i);
            LOG.trace("Part #" + i + ": " + part);

            if (part.isMimeType("multipart/*")) {
                LOG.trace("Part #" + i + ": is mimetype: multipart/*");
                extractAttachmentsFromMultipart((Multipart)part.getContent(), map);
            } else {
                String disposition = part.getDisposition();
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Part #" + i + ": Disposition: " + part.getDisposition());
                    LOG.trace("Part #" + i + ": Description: " + part.getDescription());
                    LOG.trace("Part #" + i + ": ContentType: " + part.getContentType());
                    LOG.trace("Part #" + i + ": FileName: " + part.getFileName());
                    LOG.trace("Part #" + i + ": Size: " + part.getSize());
                    LOG.trace("Part #" + i + ": LineCount: " + part.getLineCount());
                }

                if (disposition != null && (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE))) {
                    // only add named attachments
                    String fileName = part.getFileName();
                    if (fileName != null) {
                        LOG.debug("Mail contains file attachment: " + fileName);
                        if (!map.containsKey(fileName)) {
                            // Parts marked with a disposition of Part.ATTACHMENT are clearly attachments
                            map.put(fileName, part.getDataHandler());
                        } else {
                            LOG.warn("Cannot extract duplicate attachment: " + fileName);
                        }
                    }
                }
View Full Code Here

    protected void extractAttachmentsFromMultipart(Multipart mp, Map<String, DataHandler> map)
        throws MessagingException, IOException {

        for (int i = 0; i < mp.getCount(); i++) {
            Part part = mp.getBodyPart(i);
            LOG.trace("Part #" + i + ": " + part);

            if (part.isMimeType("multipart/*")) {
                LOG.trace("Part #" + i + ": is mimetype: multipart/*");
                extractAttachmentsFromMultipart((Multipart) part.getContent(), map);
            } else {
                String disposition = part.getDisposition();
                String fileName = part.getFileName();

                if (LOG.isTraceEnabled()) {
                    LOG.trace("Part #{}: Disposition: {}", i, disposition);
                    LOG.trace("Part #{}: Description: {}", i, part.getDescription());
                    LOG.trace("Part #{}: ContentType: {}", i, part.getContentType());
                    LOG.trace("Part #{}: FileName: {}", i, fileName);
                    LOG.trace("Part #{}: Size: {}", i, part.getSize());
                    LOG.trace("Part #{}: LineCount: {}", i, part.getLineCount());
                }

                if (validDisposition(disposition, fileName)
                        || fileName != null) {
                    LOG.debug("Mail contains file attachment: {}", fileName);
                    if (!map.containsKey(fileName)) {
                        // Parts marked with a disposition of Part.ATTACHMENT are clearly attachments
                        map.put(fileName, part.getDataHandler());
                    } else {
                        LOG.warn("Cannot extract duplicate file attachment: {}.", fileName);
                    }
                }
            }
View Full Code Here

   
    protected static void extractFromMultipart(Multipart mp, Map<String, DataHandler> map)
        throws javax.mail.MessagingException, IOException {

        for (int i = 0; i < mp.getCount(); i++) {
            Part part = mp.getBodyPart(i);          
            LOG.trace("Part #" + i + ": " + part);

            if (part.isMimeType("multipart/*")) {
                LOG.trace("Part #" + i + ": is mimetype: multipart/*");
                extractFromMultipart((Multipart)part.getContent(), map);
            } else {
                String disposition = part.getDisposition();
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Part #" + i + ": Disposition: " + part.getDisposition());
                    LOG.trace("Part #" + i + ": Description: " + part.getDescription());
                    LOG.trace("Part #" + i + ": ContentType: " + part.getContentType());
                    LOG.trace("Part #" + i + ": FileName: " + part.getFileName());
                    LOG.trace("Part #" + i + ": Size: " + part.getSize());
                    LOG.trace("Part #" + i + ": LineCount: " + part.getLineCount());
                }

                if (disposition != null && (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE))) {
                    // only add named attachments
                    String fileName = part.getFileName();
                    if (fileName != null) {
                        LOG.debug("Mail contains file attachment: " + fileName);
                        // Parts marked with a disposition of Part.ATTACHMENT are clearly attachments
                        CollectionHelper.appendValue(map, fileName, part.getDataHandler());
                    }
                }
            }
        }
    }   
View Full Code Here

        System.out.println("Created message: " + message);
        Object content = message.getContent();
        assertTrue(content instanceof MimeMultipart);
        MimeMultipart contentMP = (MimeMultipart) content;
        assertEquals(contentMP.getCount(), 3); // first attachement for text body and second for file attached
        Part part = contentMP.getBodyPart(0);
        assertTrue(part.isMimeType("text/plain"));
        Set names = new HashSet();
        part = contentMP.getBodyPart(1);
        String disposition = part.getDisposition();
        assertTrue(disposition.equalsIgnoreCase(Part.ATTACHMENT));
        DataHandler att = part.getDataHandler();
        names.add(att.getName().toLowerCase());
        part = contentMP.getBodyPart(2);
        disposition = part.getDisposition();
        assertTrue(disposition.equalsIgnoreCase(Part.ATTACHMENT));
        att = part.getDataHandler();
        names.add(att.getName().toLowerCase());
        assertTrue(names.contains("example.xml"));
        assertTrue(names.contains("id"));
        assertEquals("subject", "Drink a beer James", message.getSubject());
    }
View Full Code Here

        log.info("Created message: " + message);
        Object content = message.getContent();
        assertTrue(content instanceof MimeMultipart);
        MimeMultipart contentMP = (MimeMultipart) content;
        assertEquals(contentMP.getCount(), 3); // first attachement for text body and second for file attached
        Part part = contentMP.getBodyPart(0);
        assertTrue(part.isMimeType("text/plain"));
        Set names = new HashSet();
        part = contentMP.getBodyPart(1);
        String disposition = part.getDisposition();
        assertTrue(disposition.equalsIgnoreCase(Part.ATTACHMENT));
        DataHandler att = part.getDataHandler();
        names.add(att.getName().toLowerCase());
        part = contentMP.getBodyPart(2);
        disposition = part.getDisposition();
        assertTrue(disposition.equalsIgnoreCase(Part.ATTACHMENT));
        att = part.getDataHandler();
        names.add(att.getName().toLowerCase());
        assertTrue(names.contains("example.xml"));
        assertTrue(names.contains("id"));
        assertEquals("subject", "Drink a beer James", message.getSubject());
    }
View Full Code Here

            else if (content instanceof MimeMultipart) {
                // mail with attachment
                mp = (MimeMultipart)content;
                int nbMP = mp.getCount();
                for (int i=0; i < nbMP; i++) {
                    Part part = mp.getBodyPart(i);
                    String disposition = part.getDisposition();
                    if ((disposition != null) &&
                        ((disposition.equals(Part.ATTACHMENT) ||
                         (disposition.equals(Part.INLINE))))) {
                        //Parts marked with a disposition of Part.ATTACHMENT from part.getDisposition() are clearly attachments
                        DataHandler att = part.getDataHandler();
                        normalizedMessage.addAttachment(att.getName(), att);
                    } else {
                        MimeBodyPart mbp = (MimeBodyPart)part;
                        if (mbp.isMimeType("text/plain")) {
                          // Handle plain
View Full Code Here

        try {
            Object content = this.source.getContent();
             if (content instanceof Multipart) {
                 Multipart multipart = (Multipart)content;
                 for(int i = 0; i < multipart.getCount(); i++) {
                     Part bodyPart = multipart.getBodyPart(i);
                     if (bodyPart.getDisposition() != null) {
                         if (bodyPart.getDisposition().equalsIgnoreCase(Part.ATTACHMENT)) {
                             result.add(new GmailAttachment(i, bodyPart.getFileName(), bodyPart.getContentType(), bodyPart.getInputStream()));
                         }
                     }
                 }
             }
        } catch (Exception e) {
View Full Code Here

       
        try {
            Object content = this.source.getContent();
             if (content instanceof Multipart) {
                 Multipart multipart = (Multipart)content;
                 Part bodyPart = multipart.getBodyPart(partIndex);
                 if (bodyPart.getDisposition() != null) {
                     if (bodyPart.getDisposition().equalsIgnoreCase(Part.ATTACHMENT)) {
                         result = new GmailAttachment(partIndex, bodyPart.getFileName(), bodyPart.getContentType(), bodyPart.getInputStream());
                     }
                 }
             }
             else {
                 throw new GmailException("Failed to get attachement with partIndex :" + partIndex);
View Full Code Here

TOP

Related Classes of javax.mail.Part

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.