Package javax.mail

Examples of javax.mail.Part


    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);
                        // Parts marked with a disposition of Part.ATTACHMENT are clearly attachments
                        CollectionHelper.appendValue(map, fileName, part.getDataHandler());
                    }
                }
            }
        }
    }
View Full Code Here


            } else if (content instanceof Multipart) {
                // A multipart body.
                log.info("Received multi-part email message (" +
                         ((Multipart) content).getCount() + " parts)");
                for (int i = 0; i < ((Multipart) content).getCount(); i++) {
                    Part part = ((Multipart) content).getBodyPart(i);
                    String partText = (String) part.getContent();
                    log.info("Part " + i + ": " +
                             " (" + message.getContentType() + "): " +
                             partText);
                }
            }
View Full Code Here

            } else if (content instanceof Multipart) {
                // A multipart body.
                log.info("Received multi-part email message (" +
                         ((Multipart) content).getCount() + " parts)");
                for (int i = 0; i < ((Multipart) content).getCount(); i++) {
                    Part part = ((Multipart) content).getBodyPart(i);
                    String partText = (String) part.getContent();
                    log.info("Part " + i + ": " +
                             " (" + message.getContentType() + "): " +
                             partText);
                }
            }
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 #{}: Disposition: {}", i, part.getDisposition());
                    LOG.trace("Part #{}: Description: {}", i, part.getDescription());
                    LOG.trace("Part #{}: ContentType: {}", i, part.getContentType());
                    LOG.trace("Part #{}: FileName: {}", i, part.getFileName());
                    LOG.trace("Part #{}: Size: {}", i, part.getSize());
                    LOG.trace("Part #{}: LineCount: {}", i, 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

            System.out.println("subject:" + message.getSubject());
        }
        Object content = message.getContent();
        if (content instanceof Multipart) {
            Multipart multipart = (Multipart) content;
            Part part = multipart.getBodyPart(0);
            System.out.println("content:" + part.getContent());
        } else {
            System.out.println("content:" + content);
        }
        return null;
    }
View Full Code Here

      if (subject.contains("New Event: ") && from.contains("casamind.com") && sender.contains("@google.com")) {
        Multipart multipart = (Multipart) message.getContent();

        for (int i = 0, n = multipart.getCount(); i < n; i++) {
          Part part = multipart.getBodyPart(i);

          String disposition = part.getDisposition();

          if (disposition != null && disposition.equals(Part.ATTACHMENT)) {
            if (part.getFileName().equals("invite.ics")) {
              log.info("Found an iCal attachement. Filename: " + part.getFileName());
              CalendarBuilder builder = new CalendarBuilder();
              Calendar calendar = builder.build(part.getInputStream());
              Component event = calendar.getComponent(Component.VEVENT);
              Property startDate = event.getProperty(Property.DTSTART);
              Property endDate = event.getProperty(Property.DTEND);
              Property attendee = null;
              String login = null;
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

    /**
     * @see org.apache.mailet.Mailet#service(org.apache.mailet.Mail)
     */
    public void service(Mail mail) throws MessagingException {
        MimeMessage message = mail.getMessage();
        Part strippedMessage = null;
        log("Starting message decryption..");
        if (message.isMimeType("application/x-pkcs7-mime") || message.isMimeType("application/pkcs7-mime")) {
            try {
                SMIMEEnveloped env = new SMIMEEnveloped(message);
                Collection<RecipientInformation> recipients = env.getRecipientInfos().getRecipients();
                Iterator<RecipientInformation> iter = recipients.iterator();
                while (iter.hasNext()) {
                    RecipientInformation info = iter.next();
                    RecipientId id = info.getRID();
                    if (id.match(keyHolder.getCertificate())) {
                        try {
                            MimeBodyPart part = SMIMEUtil.toMimeBodyPart(info.getContent(keyHolder.getPrivateKey(), "BC"));
                            // strippedMessage contains the decrypted message.
                            strippedMessage = part;
                            log("Encrypted message decrypted");
                        } catch (Exception e) {
                            throw new MessagingException("Error during the decryption of the message", e); }
                    } else {
                        log("Found an encrypted message but it isn't encrypted for the supplied key");
                    }
                }
            } catch (CMSException e) { throw new MessagingException("Error during the decryption of the message",e); }
        }
       
        // if the decryption has been successful..
        if (strippedMessage != null) {
            // I put the private key's public certificate as a mailattribute.
            // I create a list of certificate because I want to minic the
            // behavior of the SMIMEVerifySignature mailet. In that way
            // it is possible to reuse the same matchers to analyze
            // the result of the operation.
            ArrayList<X509Certificate> list = new ArrayList<X509Certificate>(1);
            list.add(keyHolder.getCertificate());
            mail.setAttribute(mailAttribute, list);

            // I start the message stripping.
            try {
                MimeMessage newmex = new MimeMessage(message);
                Object obj = strippedMessage.getContent();
                if (obj instanceof Multipart) {
                    log("The message is multipart, content type "+((Multipart)obj).getContentType());
                    newmex.setContent((Multipart)obj);
                } else {
                    newmex.setContent(obj, strippedMessage.getContentType());
                    newmex.setDisposition(null);
                }
                newmex.saveChanges();
                mail.setMessage(newmex);
            } catch (IOException e) {
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

        logger.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

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.