javax.mail.Multipart.getBodyPart()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(10.0k)|赞(0)|评价(0)|浏览(163)

本文整理了Java中javax.mail.Multipart.getBodyPart()方法的一些代码示例,展示了Multipart.getBodyPart()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Multipart.getBodyPart()方法的具体详情如下:
包路径:javax.mail.Multipart
类名称:Multipart
方法名:getBodyPart

Multipart.getBodyPart介绍

[英]Get the specified Part. Parts are numbered starting at 0.
[中]获取指定的部分。零件编号从0开始。

代码示例

代码示例来源:origin: oblac/jodd

/**
 * Process the {@link Multipart}.
 *
 * @param mp {@link Multipart}
 * @throws MessagingException if there is a failure.
 * @throws IOException        if there is an issue with the {@link Multipart}.
 */
private void processMultipart(final Multipart mp) throws MessagingException, IOException {
  final int count = mp.getCount();
  for (int i = 0; i < count; i++) {
    final Part innerPart = mp.getBodyPart(i);
    processPart(innerPart);
  }
}

代码示例来源:origin: stackoverflow.com

List<File> attachments = new ArrayList<File>();
for (Message message : temp) {
  Multipart multipart = (Multipart) message.getContent();
  // System.out.println(multipart.getCount());

  for (int i = 0; i < multipart.getCount(); i++) {
    BodyPart bodyPart = multipart.getBodyPart(i);
    if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()) &&
        !StringUtils.isNotBlank(bodyPart.getFileName())) {
      continue; // dealing with attachments only
    } 
    InputStream is = bodyPart.getInputStream();
    File f = new File("/tmp/" + bodyPart.getFileName());
    FileOutputStream fos = new FileOutputStream(f);
    byte[] buf = new byte[4096];
    int bytesRead;
    while((bytesRead = is.read(buf))!=-1) {
      fos.write(buf, 0, bytesRead);
    }
    fos.close();
    attachments.add(f);
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

private void handleMultipart( String foldername, Multipart multipart, Pattern pattern ) throws KettleException {
 try {
  for ( int i = 0, n = multipart.getCount(); i < n; i++ ) {
   handlePart( foldername, multipart.getBodyPart( i ), pattern );
  }
 } catch ( Exception e ) {
  throw new KettleException( e );
 }
}

代码示例来源:origin: spring-projects/spring-integration-samples

bp = multipart.getBodyPart(i);

代码示例来源:origin: pentaho/pentaho-kettle

private String getMessageBodyOrContentType( Part p, final boolean returnContentType ) throws MessagingException,
 IOException {
 if ( p.isMimeType( "text/*" ) ) {
  String s = (String) p.getContent();
  return returnContentType ? p.getContentType() : s;
 }
 if ( p.isMimeType( "multipart/alternative" ) ) {
  // prefer html text over plain text
  Multipart mp = (Multipart) p.getContent();
  String text = null;
  for ( int i = 0; i < mp.getCount(); i++ ) {
   Part bp = mp.getBodyPart( i );
   if ( bp.isMimeType( "text/plain" ) ) {
    if ( text == null ) {
     text = getMessageBodyOrContentType( bp, returnContentType );
    }
   }
  }
  return text;
 } else if ( p.isMimeType( "multipart/*" ) ) {
  Multipart mp = (Multipart) p.getContent();
  for ( int i = 0; i < mp.getCount(); i++ ) {
   String s = getMessageBodyOrContentType( mp.getBodyPart( i ), returnContentType );
   if ( s != null ) {
    return s;
   }
  }
 }
 return null;
}

代码示例来源:origin: pentaho/pentaho-kettle

Multipart multipart = (Multipart) content;
for ( int i = 0, n = multipart.getCount(); i < n; i++ ) {
 Part part = multipart.getBodyPart( i );
 String disposition = part.getDisposition();

代码示例来源:origin: stackoverflow.com

Multipart mp = (Multipart)messages[i].getContent();  
Object p = mp.getBodyPart(i).getContent();

代码示例来源:origin: camunda/camunda-bpm-platform

/**
 * Get the specified BodyPart.  BodyParts are numbered starting at 0.
 *
 * @param index    the index of the desired BodyPart
 * @return        the Part
 * @exception       MessagingException if no such BodyPart exists
 */
public synchronized BodyPart getBodyPart(int index) 
    throws MessagingException {
parse();
return super.getBodyPart(index);
}

代码示例来源:origin: com.sun.mail/javax.mail

/**
 * Get the specified BodyPart.  BodyParts are numbered starting at 0.
 *
 * @param index    the index of the desired BodyPart
 * @return        the Part
 * @exception       MessagingException if no such BodyPart exists
 */
@Override
public synchronized BodyPart getBodyPart(int index) 
    throws MessagingException {
parse();
return super.getBodyPart(index);
}

代码示例来源:origin: TEAMMATES/teammates

private String getTextFromMultiPartDigest(Multipart multipart) throws IOException, MessagingException {
  StringBuilder textBuilder = new StringBuilder();
  for (int i = 0; i < multipart.getCount(); i++) {
    BodyPart bodyPart = multipart.getBodyPart(i);
    if (bodyPart.isMimeType("message/rfc822")) {
      String text = getTextFromPart(bodyPart);
      if (text != null) {
        textBuilder.append(text);
      }
    }
  }
  String text = textBuilder.toString();
  if (text.isEmpty()) {
    return null;
  }
  return text;
}

代码示例来源:origin: TEAMMATES/teammates

private String getTextFromMultiPartMixed(Multipart multipart) throws IOException, MessagingException {
  StringBuilder textBuilder = new StringBuilder();
  for (int i = 0; i < multipart.getCount(); i++) {
    BodyPart bodyPart = multipart.getBodyPart(i);
    if (bodyPart.isMimeType("text/*")) {
      textBuilder.append((String) bodyPart.getContent());
    } else if (bodyPart.isMimeType("multipart/*")) {
      String text = getTextFromPart(bodyPart);
      if (text != null) {
        textBuilder.append(text);
      }
    }
  }
  String text = textBuilder.toString();
  if (text.isEmpty()) {
    return null;
  }
  return text;
}

代码示例来源:origin: spring-projects/spring-integration

@Test
public void testAttachmentsWithMapping() throws Exception {
  final ImapMailReceiver receiver = new ImapMailReceiver("imap://foo");
  receiver.setHeaderMapper(new DefaultMailHeaderMapper());
  receiver.setEmbeddedPartsAsBytes(false);
  testAttachmentsGuts(receiver);
  org.springframework.messaging.Message<?>[] messages =
      (org.springframework.messaging.Message<?>[]) receiver.receive();
  Object content = messages[0].getPayload();
  assertThat(content, instanceOf(Multipart.class));
  assertEquals("bar", ((Multipart) content).getBodyPart(0).getContent().toString().trim());
  assertEquals("foo", ((Multipart) content).getBodyPart(1).getContent().toString().trim());
}

代码示例来源:origin: spring-projects/spring-integration

@Test
public void testAttachments() throws Exception {
  final ImapMailReceiver receiver = new ImapMailReceiver("imap://foo");
  Folder folder = testAttachmentsGuts(receiver);
  Message[] messages = (Message[]) receiver.receive();
  Object content = messages[0].getContent();
  assertEquals("bar", ((Multipart) content).getBodyPart(0).getContent().toString().trim());
  assertEquals("foo", ((Multipart) content).getBodyPart(1).getContent().toString().trim());
  assertSame(folder, messages[0].getFolder());
}

代码示例来源:origin: camunda/camunda-bpm-platform

parse(mp, (MimeBodyPart) mp.getBodyPart(i));

代码示例来源:origin: spring-projects/spring-integration

@Test
public void byteArrayMessage() throws Exception {
  byte[] payload = {1, 2, 3};
  org.springframework.messaging.Message<byte[]> message =
      MessageBuilder.withPayload(payload)
      .setHeader(MailHeaders.ATTACHMENT_FILENAME, "attachment.txt")
      .setHeader(MailHeaders.TO, MailTestsHelper.TO)
      .build();
  this.handler.handleMessage(message);
  byte[] buffer = new byte[1024];
  MimeMessage mimeMessage = this.mailSender.getSentMimeMessages().get(0);
  assertTrue("message must be multipart", mimeMessage.getContent() instanceof Multipart);
  int size = new DataInputStream(((Multipart) mimeMessage.getContent()).getBodyPart(0).getInputStream()).read(buffer);
  assertEquals("buffer size does not match", payload.length, size);
  byte[] messageContent = new byte[size];
  System.arraycopy(buffer, 0, messageContent, 0, payload.length);
  assertArrayEquals("buffer content does not match", payload, messageContent);
  assertEquals(mimeMessage.getRecipients(Message.RecipientType.TO).length, MailTestsHelper.TO.length);
}

代码示例来源:origin: spring-projects/spring-integration

@Test
public void byteArrayMessage() throws Exception {
  byte[] payload = {1, 2, 3};
  org.springframework.messaging.Message<?> message =
      MessageBuilder.withPayload(payload)
      .setHeader(MailHeaders.ATTACHMENT_FILENAME, "attachment.txt")
      .setHeader(MailHeaders.TO, MailTestsHelper.TO)
      .build();
  this.handler.handleMessage(message);
  assertEquals("no mime message should have been sent",
      1, this.mailSender.getSentMimeMessages().size());
  assertEquals("only one simple message must be sent",
      0, this.mailSender.getSentSimpleMailMessages().size());
  byte[] buffer = new byte[1024];
  MimeMessage mimeMessage = this.mailSender.getSentMimeMessages().get(0);
  assertTrue("message must be multipart", mimeMessage.getContent() instanceof Multipart);
  int size = new DataInputStream(((Multipart) mimeMessage.getContent()).getBodyPart(0).getInputStream()).read(buffer);
  assertEquals("buffer size does not match", payload.length, size);
  byte[] messageContent = new byte[size];
  System.arraycopy(buffer, 0, messageContent, 0, payload.length);
  assertArrayEquals("buffer content does not match", payload, messageContent);
  assertEquals(mimeMessage.getRecipients(Message.RecipientType.TO).length, MailTestsHelper.TO.length);
}

代码示例来源:origin: camunda/camunda-bpm-platform

int count = mp.getCount();
for (int i = 0; i < count; i++)
  if (matchPart(mp.getBodyPart(i)))
  return true;
} else if (p.isMimeType("message/rfc822")) {

代码示例来源:origin: com.sun.mail/javax.mail

int count = mp.getCount();
for (int i = 0; i < count; i++)
  if (matchPart(mp.getBodyPart(i)))
  return true;
} else if (p.isMimeType("message/rfc822")) {

代码示例来源:origin: apache/james-project

public static List<BodyPart> retrieveBodyParts(Multipart multipart) throws MessagingException {
    ImmutableList.Builder<BodyPart> builder = ImmutableList.builder();
    for (int i = 0; i < multipart.getCount(); i++) {
      builder.add(multipart.getBodyPart(i));
    }
    return builder.build();
  }
}

代码示例来源:origin: TEAMMATES/teammates

/**
 * Returns the text from multipart/alternative, the type of text returned follows the preference of the sending agent.
 */
private String getTextFromMultiPartAlternative(Multipart multipart) throws IOException, MessagingException {
  // search in reverse order as a multipart/alternative should have their most preferred format last
  for (int i = multipart.getCount() - 1; i >= 0; i--) {
    BodyPart bodyPart = multipart.getBodyPart(i);
    if (bodyPart.isMimeType("text/html")) {
      return (String) bodyPart.getContent();
    } else if (bodyPart.isMimeType("text/plain")) {
      // Since we are looking in reverse order, if we did not encounter a text/html first we can return the plain
      // text because that is the best preferred format that we understand. If a text/html comes along later it
      // means the agent sending the email did not set the html text as preferable or did not set their preferred
      // order correctly, and in that case we do not handle that.
      return (String) bodyPart.getContent();
    } else if (bodyPart.isMimeType("multipart/*") || bodyPart.isMimeType("message/rfc822")) {
      String text = getTextFromPart(bodyPart);
      if (text != null) {
        return text;
      }
    }
  }
  // we do not know how to handle the text in the multipart or there is no text
  return null;
}

相关文章