Java 类javax.mail.Multipart 实例源码

项目:Cognizant-Intelligent-Test-Scripter    文件:Mailer.java   
private static Multipart getMessagePart() throws MessagingException, IOException {
    Multipart multipart = new MimeMultipart();
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText(getVal("msg.Body"));
    multipart.addBodyPart(messageBodyPart);
    if (getBoolVal("attach.reports")) {
        LOG.info("Attaching Reports as zip");
        multipart.addBodyPart(getReportsBodyPart());
    } else {
        if (getBoolVal("attach.standaloneHtml")) {
            multipart.addBodyPart(getStandaloneHtmlBodyPart());
        }
        if (getBoolVal("attach.console")) {
            multipart.addBodyPart(getConsoleBodyPart());
        }
        if (getBoolVal("attach.screenshots")) {
            multipart.addBodyPart(getScreenShotsBodyPart());
        }
    }
    messageBodyPart.setContent(getVal("msg.Body")
            .concat("\n\n\n")
            .concat(MailComponent.getHTMLBody()), "text/html");
    return multipart;
}
项目:alfresco-repository    文件:EMLTransformer.java   
/**
 * Find "text" parts of message recursively and appends it to sb StringBuilder
 * 
 * @param multipart Multipart to process
 * @param sb StringBuilder 
 * @throws MessagingException
 * @throws IOException
 */
private void processMultiPart(Multipart multipart, StringBuilder sb) throws MessagingException, IOException
{
    boolean isAlternativeMultipart = multipart.getContentType().contains(MimetypeMap.MIMETYPE_MULTIPART_ALTERNATIVE);
    if (isAlternativeMultipart)
    {
        processAlternativeMultipart(multipart, sb);
    }
    else
    {
        for (int i = 0, n = multipart.getCount(); i < n; i++)
        {
            Part part = multipart.getBodyPart(i);
            if (part.getContent() instanceof Multipart)
            {
                processMultiPart((Multipart) part.getContent(), sb);
            }
            else
            {
                processPart(part, sb);
            }
        }
    }
}
项目:oscm    文件:MailReader.java   
/**
 * Get the content of a mail message.
 * 
 * @param message
 *            the mail message
 * @return the content of the mail message
 */
private String getMessageContent(Message message) throws MessagingException {
    try {
        Object content = message.getContent();
        if (content instanceof Multipart) {
            StringBuffer messageContent = new StringBuffer();
            Multipart multipart = (Multipart) content;
            for (int i = 0; i < multipart.getCount(); i++) {
                Part part = multipart.getBodyPart(i);
                if (part.isMimeType("text/plain")) {
                    messageContent.append(part.getContent().toString());
                }
            }
            return messageContent.toString();
        }
        return content.toString();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}
项目:jsf-core    文件:ReciveMail.java   
/**   
 * 【保存附件】   
 */   
public void saveAttachMent(Part part) throws Exception {   
    String fileName = "";   
    if (part.isMimeType("multipart/*")) {   
        Multipart mp = (Multipart) part.getContent();   
        for (int i = 0; i < mp.getCount(); i++) {   
            BodyPart mpart = mp.getBodyPart(i);   
            String disposition = mpart.getDisposition();   
            if ((disposition != null)   
                    && ((disposition.equals(Part.ATTACHMENT)) || (disposition   
                            .equals(Part.INLINE)))) {   
                fileName = mpart.getFileName();   
                if (fileName.toLowerCase().indexOf("gb2312") != -1) {   
                    fileName = MimeUtility.decodeText(fileName);   
                }   
                saveFile(fileName, mpart.getInputStream());   
            } else if (mpart.isMimeType("multipart/*")) {   
                saveAttachMent(mpart);   
            } else {   
                fileName = mpart.getFileName();   
                if ((fileName != null)   
                        && (fileName.toLowerCase().indexOf("GB2312") != -1)) {   
                    fileName = MimeUtility.decodeText(fileName);   
                    saveFile(fileName, mpart.getInputStream());   
                }   
            }   
        }   
    } else if (part.isMimeType("message/rfc822")) {   
        saveAttachMent((Part) part.getContent());   
    }   
}
项目:play    文件:MailHelper.java   
/**
 * ������
 */
private void handleAttachments(Message message, Part part) throws Exception {
    if (part.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) part.getContent();
        for (int i = 0; i < mp.getCount(); i++) {
            BodyPart bp = mp.getBodyPart(i);
            String disposition = bp.getDisposition();
            if (disposition != null && (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE))) {
                saveFile(message, bp);
            } else if (bp.isMimeType("multipart/*")) {
                handleAttachments(message, (Part) part.getContent());
            } else {
                saveFile(message, bp);
            }
        }
    } else if (part.isMimeType("message/rfc822")) {
        handleAttachments(message, (Part) part.getContent());
    }
}
项目:lemon    文件:JavamailService.java   
public void getMailTextContent(Part part, StringBuffer content)
        throws MessagingException, IOException {
    // 如果是文本类型的附件,通过getContent方法可以取到文本内容,但这不是我们需要的结果,所以在这里要做判断
    boolean isContainTextAttach = part.getContentType().indexOf("name") > 0;

    if (part.isMimeType("text/*") && !isContainTextAttach) {
        content.append(part.getContent().toString());
    } else if (part.isMimeType("message/rfc822")) {
        getMailTextContent((Part) part.getContent(), content);
    } else if (part.isMimeType("multipart/*")) {
        Multipart multipart = (Multipart) part.getContent();
        int partCount = multipart.getCount();

        for (int i = 0; i < partCount; i++) {
            BodyPart bodyPart = multipart.getBodyPart(i);
            getMailTextContent(bodyPart, content);
        }
    }
}
项目:fake-smtp-server    文件:EmailFactory.java   
private Email buildMultipartAlternativeMail(RawData rawData, String subject, Multipart multipart) throws MessagingException, IOException {
    Email email = null;
    for (int i = 0; i < multipart.getCount(); i++) {
        BodyPart part = multipart.getBodyPart(i);
        ContentType partContentType = ContentType.fromString(part.getContentType());
        Object partContent = part.getContent();
        email = new Email.Builder()
                .fromAddress(rawData.getFrom())
                .toAddress(rawData.getTo())
                .receivedOn(timestampProvider.now())
                .subject(subject)
                .rawData(rawData.getContentAsString())
                .content(Objects.toString(partContent, rawData.getContentAsString()).trim())
                .contentType(partContentType)
                .build();
        if (partContentType == ContentType.HTML) break;
    }
    return email;
}
项目:jsf-core    文件:ReciveMail.java   
/**  
 * 判断此邮件是否包含附件  
 */  
public boolean isContainAttach(Part part) throws Exception {   
    boolean attachflag = false;   
    String contentType = part.getContentType();   
    if (part.isMimeType("multipart/*")) {   
        Multipart mp = (Multipart) part.getContent();   
        for (int i = 0; i < mp.getCount(); i++) {   
            BodyPart mpart = mp.getBodyPart(i);   
            String disposition = mpart.getDisposition();   
            if ((disposition != null)   
                    && ((disposition.equals(Part.ATTACHMENT)) || (disposition   
                            .equals(Part.INLINE))))   
                attachflag = true;   
            else if (mpart.isMimeType("multipart/*")) {   
                attachflag = isContainAttach((Part) mpart);   
            } else {   
                String contype = mpart.getContentType();   
                if (contype.toLowerCase().indexOf("application") != -1)   
                    attachflag = true;   
                if (contype.toLowerCase().indexOf("name") != -1)   
                    attachflag = true;   
            }   
        }   
    } else if (part.isMimeType("message/rfc822")) {   
        attachflag = isContainAttach((Part) part.getContent());   
    }   
    return attachflag;   
}
项目:infotaf    文件:Utils.java   
/**
 * Récupération d'une pièce jointe d'un mail
 * @param part : Corps du mail (Bodypart)
 * @return
 * @throws Exception
 */
private static Map<String, InputStream> getAttachments(BodyPart part) throws Exception {
    Map<String, InputStream> result = new HashMap<String, InputStream>();
    Object content = part.getContent();
    if (content instanceof InputStream || content instanceof String) {
        if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()) || StringUtils.isNotBlank(part.getFileName())) {
            result.put(part.getFileName(), part.getInputStream());
            return result;
        } else {
            return new HashMap<String, InputStream>();
        }
    }

    if (content instanceof Multipart) {
            Multipart multipart = (Multipart) content;
            for (int i = 0; i < multipart.getCount(); i++) {
                BodyPart bodyPart = multipart.getBodyPart(i);
                result.putAll(getAttachments(bodyPart));
            }
    }
    return result;
}
项目:scipio-erp    文件:ServiceMcaCondition.java   
private List<String> getBodyText(Part part) throws MessagingException, IOException {
    Object c = part.getContent();
    if (c instanceof String) {
        return UtilMisc.toList((String) c);
    } else if (c instanceof Multipart) {
        List<String> textContent = new LinkedList<String>();
        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 new LinkedList<String>();
    }
}
项目:thatsapp    文件:MailUtilities.java   
private static TreeMap<String, InputStream> getAttachments(BodyPart part) throws Exception {
    TreeMap<String, InputStream> result = new TreeMap<>();
    Object content = part.getContent();
    if (content instanceof InputStream || content instanceof String) {
        if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()) || StringUtils.isNotBlank(part.getFileName())) {
            result.put(part.getFileName(), part.getInputStream());
            return result;
        } else {
            return new TreeMap<String, InputStream>();
        }
    }

    if (content instanceof Multipart) {
        Multipart multipart = (Multipart) content;
        for (int i = 0; i < multipart.getCount(); i++) {
            BodyPart bodyPart = multipart.getBodyPart(i);
            result.putAll(getAttachments(bodyPart));
        }
    }
    return result;
}
项目:anti-spam-weka-gui    文件:MailHelper.java   
private static Message buildMessage(Session session, String from, String recipients, String subject, String text, String filename) throws MessagingException, AddressException
{
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
    message.setSubject(subject);

    BodyPart messageTextPart = new MimeBodyPart();
    messageTextPart.setText(text);

    BodyPart messageAttachmentPart = new MimeBodyPart();
    DataSource source = new FileDataSource(new File(filename));
    messageAttachmentPart.setDataHandler(new DataHandler(source));
    messageAttachmentPart.setFileName(filename);

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageTextPart);
    multipart.addBodyPart(messageAttachmentPart);
    message.setContent(multipart);

    return message;
}
项目:camunda-bpm-mail    文件:Mail.java   
protected static void processMessageContent(Message message, Mail mail) throws MessagingException, IOException {

    if (isMultipartMessage(message)) {
      Multipart multipart = (Multipart) message.getContent();

      int numberOfParts = multipart.getCount();
      for (int partCount = 0; partCount < numberOfParts; partCount++) {
        BodyPart bodyPart = multipart.getBodyPart(partCount);

        processMessagePartContent(bodyPart, mail);
      }

    } else {
      processMessagePartContent(message, mail);
    }
  }
项目:camunda-bpm-mail    文件:MailTestUtil.java   
public static MimeMessage createMimeMessageWithHtml(Session session) throws MessagingException, AddressException {
  MimeMessage message = createMimeMessage(session);

  Multipart multiPart = new MimeMultipart();

  MimeBodyPart textPart = new MimeBodyPart();
  textPart.setText("text");
  multiPart.addBodyPart(textPart);

  MimeBodyPart htmlPart = new MimeBodyPart();
  htmlPart.setContent("<b>html</b>", MailContentType.TEXT_HTML.getType());
  multiPart.addBodyPart(htmlPart);

  message.setContent(multiPart);
  return message;
}
项目:scada    文件:MailUtils.java   
/** 
 * ���渽�� 
 * @param part �ʼ��ж��������е�����һ������� 
 * @param destDir  ��������Ŀ¼ 
 */ 
public static void saveAttachment(Part part, String destDir) throws UnsupportedEncodingException, MessagingException, 
        FileNotFoundException, IOException { 
    if (part.isMimeType("multipart/*")) { 
        Multipart multipart = (Multipart) part.getContent();    //�������ʼ� 
        //�������ʼ���������ʼ��� 
        int partCount = multipart.getCount(); 
        for (int i = 0; i < partCount; i++) { 
            //��ø������ʼ�������һ���ʼ��� 
            BodyPart bodyPart = multipart.getBodyPart(i); 
            //ijһ���ʼ���Ҳ�п������ɶ���ʼ�����ɵĸ����� 
            String disp = bodyPart.getDisposition(); 
            if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) { 
                InputStream is = bodyPart.getInputStream(); 
                saveFile(is, destDir, decodeText(bodyPart.getFileName())); 
            } else if (bodyPart.isMimeType("multipart/*")) { 
                saveAttachment(bodyPart,destDir); 
            } else { 
                String contentType = bodyPart.getContentType(); 
                if (contentType.indexOf("name") != -1 || contentType.indexOf("application") != -1) { 
                    saveFile(bodyPart.getInputStream(), destDir, decodeText(bodyPart.getFileName())); 
                } 
            } 
        } 
    } else if (part.isMimeType("message/rfc822")) { 
        saveAttachment((Part) part.getContent(),destDir); 
    } 
}
项目:alimama    文件:POP3ReceiveMailTest.java   
/**
 * 获得邮件文本内容
 * @param part 邮件体
 * @param content 存储邮件文本内容的字符串
 * @throws MessagingException
 * @throws IOException
 */
public static void getMailTextContent(Part part, StringBuffer content) throws MessagingException, IOException {
    //如果是文本类型的附件,通过getContent方法可以取到文本内容,但这不是我们需要的结果,所以在这里要做判断
    boolean isContainTextAttach = part.getContentType().indexOf("name") > 0;    
    if (part.isMimeType("text/*") && !isContainTextAttach) {
        content.append(part.getContent().toString());
    } else if (part.isMimeType("message/rfc822")) { 
        getMailTextContent((Part)part.getContent(),content);
    } else if (part.isMimeType("multipart/*")) {
        Multipart multipart = (Multipart) part.getContent();
        int partCount = multipart.getCount();
        for (int i = 0; i < partCount; i++) {
            BodyPart bodyPart = multipart.getBodyPart(i);
            getMailTextContent(bodyPart,content);
        }
    }
}
项目:alimama    文件:POP3ReceiveMailTest.java   
/**
 * 保存附件
 * @param part 邮件中多个组合体中的其中一个组合体
 * @param destDir  附件保存目录
 * @throws UnsupportedEncodingException
 * @throws MessagingException
 * @throws FileNotFoundException
 * @throws IOException
 */
public static void saveAttachment(Part part, String destDir) throws UnsupportedEncodingException, MessagingException,
        FileNotFoundException, IOException {
    if (part.isMimeType("multipart/*")) {
        Multipart multipart = (Multipart) part.getContent();    //复杂体邮件
        //复杂体邮件包含多个邮件体
        int partCount = multipart.getCount();
        for (int i = 0; i < partCount; i++) {
            //获得复杂体邮件中其中一个邮件体
            BodyPart bodyPart = multipart.getBodyPart(i);
            //某一个邮件体也有可能是由多个邮件体组成的复杂体
            String disp = bodyPart.getDisposition();
            if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
                InputStream is = bodyPart.getInputStream();
                saveFile(is, destDir, decodeText(bodyPart.getFileName()));
            } else if (bodyPart.isMimeType("multipart/*")) {
                saveAttachment(bodyPart,destDir);
            } else {
                String contentType = bodyPart.getContentType();
                if (contentType.indexOf("name") != -1 || contentType.indexOf("application") != -1) {
                    saveFile(bodyPart.getInputStream(), destDir, decodeText(bodyPart.getFileName()));
                }
            }
        }
    } else if (part.isMimeType("message/rfc822")) {
        saveAttachment((Part) part.getContent(),destDir);
    }
}
项目:job    文件:Job51ResumeParser.java   
private String parseHtml(BodyPart body) throws Exception {
  //System.err.println(body.getContentType());
  if(body.getContentType().startsWith("text/html")) {
    Object content = body.getContent();
    return content == null ? null : content.toString();
  } else if(body.getContentType().startsWith("multipart")) {
    Multipart subpart = (Multipart) body.getContent();
    for(int j = 0; j < subpart.getCount(); j++) {
      BodyPart subbody = subpart.getBodyPart(j);
      String html = parseHtml(subbody);
      if(html != null) {
        return html;
      }
    }
  }
  return null;
}
项目:development    文件:MailReader.java   
/**
 * Get the content of a mail message.
 * 
 * @param message
 *            the mail message
 * @return the content of the mail message
 */
private String getMessageContent(Message message) throws MessagingException {
    try {
        Object content = message.getContent();
        if (content instanceof Multipart) {
            StringBuffer messageContent = new StringBuffer();
            Multipart multipart = (Multipart) content;
            for (int i = 0; i < multipart.getCount(); i++) {
                Part part = multipart.getBodyPart(i);
                if (part.isMimeType("text/plain")) {
                    messageContent.append(part.getContent().toString());
                }
            }
            return messageContent.toString();
        }
        return content.toString();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}
项目:txazo    文件:MimeEmail.java   
private void parsePart(Part part) throws Exception {
    if (part.isMimeType("text/*")) {
        content.append((String) part.getContent());
    } else if (part.isMimeType("multipart/*")) {
        Part p = null;
        Multipart multipart = (Multipart) part.getContent();
        for (int i = 0; i < multipart.getCount(); i++) {
            p = multipart.getBodyPart(i);
            String disposition = p.getDisposition();
            if (disposition != null && (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE))) {
                attachments.add(MimeUtility.decodeText(p.getFileName()));
            }
            parsePart(p);
        }
    } else if (part.isMimeType("message/rfc822")) {
        parsePart((Part) part.getContent());
    }
}
项目:play    文件:MailHelper.java   
/**
 * �����ʼ�����
 */
public static String getContent(Part part) throws Exception {
    if (part.isMimeType("text/plain")) {
        return (String) part.getContent();
    } else if (part.isMimeType("text/html")) {
        return (String) part.getContent();
    } else if (part.isMimeType("multipart/*")) {
        Multipart multipart = (Multipart) part.getContent();
        String content = "";
        for (int i = 0; i < multipart.getCount(); i++) {
            content += getContent(multipart.getBodyPart(i));
        }
        return content;
    } else if (part.isMimeType("message/rfc822")) {
        return getContent((Part) part.getContent());
    }
    return "";
}
项目:omr    文件:MailClient.java   
public void checkInboxSave(int mode) throws MessagingException, IOException {

    Store store = session.getStore();
    store.connect();
    Folder root = store.getDefaultFolder();
    Folder inbox = root.getFolder("inbox");
    inbox.open(Folder.READ_WRITE);
    Message[] msgs = inbox.getMessages();

    for (Message msg2 : msgs) {

        POP3Message msg = (POP3Message) msg2;

        Object object = msg.getContent();
        if (object instanceof Multipart) {
            Multipart multipart = (Multipart) object;

            for (int i = 0, n = multipart.getCount(); i < n; i++) {
                MailClient.handlePart(multipart.getBodyPart(i));
            }

        }

        System.out.println("    From: " + msg.getFrom()[0]);
        System.out.println(" Subject: " + msg.getSubject());
        System.out.println(" Content: " + object);
    }
    inbox.close(true);
    store.close();
}
项目:omr    文件:MailClient.java   
public void checkInboxSave(int mode) throws MessagingException, IOException {

    Store store = session.getStore();
    store.connect();
    Folder root = store.getDefaultFolder();
    Folder inbox = root.getFolder("inbox");
    inbox.open(Folder.READ_WRITE);
    Message[] msgs = inbox.getMessages();

    for (Message msg2 : msgs) {

        POP3Message msg = (POP3Message) msg2;

        Object object = msg.getContent();
        if (object instanceof Multipart) {
            Multipart multipart = (Multipart) object;

            for (int i = 0, n = multipart.getCount(); i < n; i++) {
                MailClient.handlePart(multipart.getBodyPart(i));
            }

        }

        System.out.println("    From: " + msg.getFrom()[0]);
        System.out.println(" Subject: " + msg.getSubject());
        System.out.println(" Content: " + object);
    }
    inbox.close(true);
    store.close();
}
项目:omr    文件:MailClient.java   
public void checkInboxSave(int mode) throws MessagingException, IOException {

    Store store = session.getStore();
    store.connect();
    Folder root = store.getDefaultFolder();
    Folder inbox = root.getFolder("inbox");
    inbox.open(Folder.READ_WRITE);
    Message[] msgs = inbox.getMessages();

    for (Message msg2 : msgs) {

        POP3Message msg = (POP3Message) msg2;

        Object object = msg.getContent();
        if (object instanceof Multipart) {
            Multipart multipart = (Multipart) object;

            for (int i = 0, n = multipart.getCount(); i < n; i++) {
                MailClient.handlePart(multipart.getBodyPart(i));
            }

        }

        System.out.println("    From: " + msg.getFrom()[0]);
        System.out.println(" Subject: " + msg.getSubject());
        System.out.println(" Content: " + object);
    }
    inbox.close(true);
    store.close();
}
项目:omr    文件:MailClient.java   
public void checkInboxSave(int mode) throws MessagingException, IOException {

    Store store = session.getStore();
    store.connect();
    Folder root = store.getDefaultFolder();
    Folder inbox = root.getFolder("inbox");
    inbox.open(Folder.READ_WRITE);
    Message[] msgs = inbox.getMessages();

    for (Message msg2 : msgs) {

        POP3Message msg = (POP3Message) msg2;

        Object object = msg.getContent();
        if (object instanceof Multipart) {
            Multipart multipart = (Multipart) object;

            for (int i = 0, n = multipart.getCount(); i < n; i++) {
                MailClient.handlePart(multipart.getBodyPart(i));
            }

        }

        System.out.println("    From: " + msg.getFrom()[0]);
        System.out.println(" Subject: " + msg.getSubject());
        System.out.println(" Content: " + object);
    }
    inbox.close(true);
    store.close();
}
项目:alfresco-repository    文件:EMLTransformer.java   
/**
 * Finds the suitable part from an multipart/alternative and appends it's text content to StringBuilder sb
 * 
 * @param multipart
 * @param sb
 * @throws IOException
 * @throws MessagingException
 */
private void processAlternativeMultipart(Multipart multipart, StringBuilder sb) throws IOException, MessagingException
{
    Part partToUse = null;
    for (int i = 0, n = multipart.getCount(); i < n; i++)
    {
        Part part = multipart.getBodyPart(i);
        if (part.getContentType().contains(MimetypeMap.MIMETYPE_TEXT_PLAIN))
        {
            partToUse = part;
            break;
        }
        else if  (part.getContentType().contains(MimetypeMap.MIMETYPE_HTML))
        {
            partToUse = part;
        } 
        else if (part.getContentType().contains(MimetypeMap.MIMETYPE_MULTIPART_ALTERNATIVE))
        {
            if (part.getContent() instanceof Multipart)
            {
                processAlternativeMultipart((Multipart) part.getContent(), sb);
            }
        }
    }
    if (partToUse != null)
    {
        processPart(partToUse, sb);
    }
}