javax.mail.internet.MimeMultipart.addBodyPart()方法的使用及代码示例

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

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

MimeMultipart.addBodyPart介绍

[英]Adds a Part to the multipart. The BodyPart is appended to the list of existing Parts.
[中]将部件添加到多部件。车身零件将附加到现有零件列表中。

代码示例

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

/**
 * Set the given plain text and HTML text as alternatives, offering
 * both options to the email client. Requires multipart mode.
 * <p><b>NOTE:</b> Invoke {@link #addInline} <i>after</i> {@code setText};
 * else, mail readers might not be able to resolve inline references correctly.
 * @param plainText the plain text for the message
 * @param htmlText the HTML text for the message
 * @throws MessagingException in case of errors
 */
public void setText(String plainText, String htmlText) throws MessagingException {
  Assert.notNull(plainText, "Plain text must not be null");
  Assert.notNull(htmlText, "HTML text must not be null");
  MimeMultipart messageBody = new MimeMultipart(MULTIPART_SUBTYPE_ALTERNATIVE);
  getMainPart().setContent(messageBody, CONTENT_TYPE_ALTERNATIVE);
  // Create the plain text part of the message.
  MimeBodyPart plainTextPart = new MimeBodyPart();
  setPlainTextToMimePart(plainTextPart, plainText);
  messageBody.addBodyPart(plainTextPart);
  // Create the HTML text part of the message.
  MimeBodyPart htmlTextPart = new MimeBodyPart();
  setHtmlTextToMimePart(htmlTextPart, htmlText);
  messageBody.addBodyPart(htmlTextPart);
}

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

/**
 * Add an inline element to the MimeMessage, taking the content from a
 * {@code javax.activation.DataSource}.
 * <p>Note that the InputStream returned by the DataSource implementation
 * needs to be a <i>fresh one on each call</i>, as JavaMail will invoke
 * {@code getInputStream()} multiple times.
 * <p><b>NOTE:</b> Invoke {@code addInline} <i>after</i> {@link #setText};
 * else, mail readers might not be able to resolve inline references correctly.
 * @param contentId the content ID to use. Will end up as "Content-ID" header
 * in the body part, surrounded by angle brackets: e.g. "myId" -> "&lt;myId&gt;".
 * Can be referenced in HTML source via src="cid:myId" expressions.
 * @param dataSource the {@code javax.activation.DataSource} to take
 * the content from, determining the InputStream and the content type
 * @throws MessagingException in case of errors
 * @see #addInline(String, java.io.File)
 * @see #addInline(String, org.springframework.core.io.Resource)
 */
public void addInline(String contentId, DataSource dataSource) throws MessagingException {
  Assert.notNull(contentId, "Content ID must not be null");
  Assert.notNull(dataSource, "DataSource must not be null");
  MimeBodyPart mimeBodyPart = new MimeBodyPart();
  mimeBodyPart.setDisposition(MimeBodyPart.INLINE);
  // We're using setHeader here to remain compatible with JavaMail 1.2,
  // rather than JavaMail 1.3's setContentID.
  mimeBodyPart.setHeader(HEADER_CONTENT_ID, "<" + contentId + ">");
  mimeBodyPart.setDataHandler(new DataHandler(dataSource));
  getMimeMultipart().addBodyPart(mimeBodyPart);
}

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

/**
 * Adds message data and attachments.
 *
 * @param emailWithData {@link Email} with data
 * @param msgToSet      {@link MimeMessage} to set data into.
 * @throws MessagingException if there is a failure.
 */
private void addBodyData(final Email emailWithData, final MimeMessage msgToSet) throws MessagingException {
  final List<EmailMessage> messages = emailWithData.messages();
  final int totalMessages = messages.size();
  // Need to use new list since filterEmbeddedAttachments(List) removes attachments from the source List
  final List<EmailAttachment<? extends DataSource>> attachments = new ArrayList<>(emailWithData.attachments());
  if (attachments.isEmpty() && totalMessages == 1) {
    // special case: no attachments and just one content
    setContent(messages.get(0), msgToSet);
  } else {
    final MimeMultipart multipart = new MimeMultipart();
    final MimeMultipart msgMultipart = new MimeMultipart(ALTERNATIVE);
    multipart.addBodyPart(getBaseBodyPart(msgMultipart));
    for (final EmailMessage emailMessage : messages) {
      msgMultipart.addBodyPart(getBodyPart(emailMessage, attachments));
    }
    addAnyAttachments(attachments, multipart);
    msgToSet.setContent(multipart);
  }
}

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

break;
case MULTIPART_MODE_MIXED:
  MimeMultipart mixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED);
  mimeMessage.setContent(mixedMultipart);
  setMimeMultiparts(mixedMultipart, mixedMultipart);
  break;
case MULTIPART_MODE_RELATED:
  MimeMultipart relatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED);
  mimeMessage.setContent(relatedMultipart);
  setMimeMultiparts(relatedMultipart, relatedMultipart);
  break;
case MULTIPART_MODE_MIXED_RELATED:
  MimeMultipart rootMixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED);
  mimeMessage.setContent(rootMixedMultipart);
  MimeMultipart nestedRelatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED);
  MimeBodyPart relatedBodyPart = new MimeBodyPart();
  relatedBodyPart.setContent(nestedRelatedMultipart);
  rootMixedMultipart.addBodyPart(relatedBodyPart);
  setMimeMultiparts(rootMixedMultipart, nestedRelatedMultipart);
  break;

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

private MimeBodyPart getMainPart() throws MessagingException {
  MimeMultipart mimeMultipart = getMimeMultipart();
  MimeBodyPart bodyPart = null;
  for (int i = 0; i < mimeMultipart.getCount(); i++) {
    BodyPart bp = mimeMultipart.getBodyPart(i);
    if (bp.getFileName() == null) {
      bodyPart = (MimeBodyPart) bp;
    }
  }
  if (bodyPart == null) {
    MimeBodyPart mimeBodyPart = new MimeBodyPart();
    mimeMultipart.addBodyPart(mimeBodyPart);
    bodyPart = mimeBodyPart;
  }
  return bodyPart;
}

代码示例来源:origin: psi-probe/psi-probe

MimeMultipart content = new MimeMultipart("related");
for (DataSource attachment : attachments) {
 MimeBodyPart attachmentPart = createAttachmentPart(attachment);
 content.addBodyPart(attachmentPart);
content.addBodyPart(bodyPart);

代码示例来源:origin: org.springframework/spring-context-support

/**
 * Set the given plain text and HTML text as alternatives, offering
 * both options to the email client. Requires multipart mode.
 * <p><b>NOTE:</b> Invoke {@link #addInline} <i>after</i> {@code setText};
 * else, mail readers might not be able to resolve inline references correctly.
 * @param plainText the plain text for the message
 * @param htmlText the HTML text for the message
 * @throws MessagingException in case of errors
 */
public void setText(String plainText, String htmlText) throws MessagingException {
  Assert.notNull(plainText, "Plain text must not be null");
  Assert.notNull(htmlText, "HTML text must not be null");
  MimeMultipart messageBody = new MimeMultipart(MULTIPART_SUBTYPE_ALTERNATIVE);
  getMainPart().setContent(messageBody, CONTENT_TYPE_ALTERNATIVE);
  // Create the plain text part of the message.
  MimeBodyPart plainTextPart = new MimeBodyPart();
  setPlainTextToMimePart(plainTextPart, plainText);
  messageBody.addBodyPart(plainTextPart);
  // Create the HTML text part of the message.
  MimeBodyPart htmlTextPart = new MimeBodyPart();
  setHtmlTextToMimePart(htmlTextPart, htmlText);
  messageBody.addBodyPart(htmlTextPart);
}

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

/**
 * Add an attachment to the MimeMessage, taking the content from a
 * {@code javax.activation.DataSource}.
 * <p>Note that the InputStream returned by the DataSource implementation
 * needs to be a <i>fresh one on each call</i>, as JavaMail will invoke
 * {@code getInputStream()} multiple times.
 * @param attachmentFilename the name of the attachment as it will
 * appear in the mail (the content type will be determined by this)
 * @param dataSource the {@code javax.activation.DataSource} to take
 * the content from, determining the InputStream and the content type
 * @throws MessagingException in case of errors
 * @see #addAttachment(String, org.springframework.core.io.InputStreamSource)
 * @see #addAttachment(String, java.io.File)
 */
public void addAttachment(String attachmentFilename, DataSource dataSource) throws MessagingException {
  Assert.notNull(attachmentFilename, "Attachment filename must not be null");
  Assert.notNull(dataSource, "DataSource must not be null");
  try {
    MimeBodyPart mimeBodyPart = new MimeBodyPart();
    mimeBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
    mimeBodyPart.setFileName(MimeUtility.encodeText(attachmentFilename));
    mimeBodyPart.setDataHandler(new DataHandler(dataSource));
    getRootMimeMultipart().addBodyPart(mimeBodyPart);
  }
  catch (UnsupportedEncodingException ex) {
    throw new MessagingException("Failed to encode attachment filename", ex);
  }
}

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

+ subjectType + '.');
setContent(body, toMsgString(t), "text/plain");
final MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(body);
msg.setContent(multipart);
msg.setDescription(msgDesc);

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

/**
 * @param emailMessage {@link EmailMessage} with data.
 * @param attachments  {@link List} of {@link EmailAttachment}s.
 * @return new {@link MimeBodyPart} with data from emailMessage and attachments.
 * @throws MessagingException if there is a failure.
 */
private MimeBodyPart getBodyPart(final EmailMessage emailMessage, final List<EmailAttachment<? extends DataSource>> attachments) throws MessagingException {
  final MimeBodyPart bodyPart = new MimeBodyPart();
  // detect embedded attachments
  final List<EmailAttachment<? extends DataSource>> embeddedAttachments = filterEmbeddedAttachments(attachments, emailMessage);
  if (embeddedAttachments.isEmpty()) {
    // no embedded attachments, just add message
    setContent(emailMessage, bodyPart);
  } else {
    attachments.removeAll(embeddedAttachments);
    // embedded attachments detected, join them as related
    final MimeMultipart relatedMultipart = new MimeMultipart(RELATED);
    final MimeBodyPart messageData = new MimeBodyPart();
    setContent(emailMessage, messageData);
    relatedMultipart.addBodyPart(messageData);
    addAnyAttachments(embeddedAttachments, relatedMultipart);
    bodyPart.setContent(relatedMultipart);
  }
  return bodyPart;
}

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

private void addAttachedContent( String filename, String fileContent ) throws Exception {
 // create a data source
 MimeBodyPart mbp = new MimeBodyPart();
 // get a data Handler to manipulate this file type;
 mbp.setDataHandler( new DataHandler( new ByteArrayDataSource( fileContent.getBytes(), "application/x-any" ) ) );
 // include the file in the data source
 mbp.setFileName( filename );
 // add the part with the file in the BodyPart();
 data.parts.addBodyPart( mbp );
}

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

+ subjectType + '.');
setContent(body, toMsgString(t), "text/plain");
final MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(body);
msg.setContent(multipart);
msg.setDescription(msgDesc);

代码示例来源:origin: org.springframework/spring-context-support

break;
case MULTIPART_MODE_MIXED:
  MimeMultipart mixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED);
  mimeMessage.setContent(mixedMultipart);
  setMimeMultiparts(mixedMultipart, mixedMultipart);
  break;
case MULTIPART_MODE_RELATED:
  MimeMultipart relatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED);
  mimeMessage.setContent(relatedMultipart);
  setMimeMultiparts(relatedMultipart, relatedMultipart);
  break;
case MULTIPART_MODE_MIXED_RELATED:
  MimeMultipart rootMixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED);
  mimeMessage.setContent(rootMixedMultipart);
  MimeMultipart nestedRelatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED);
  MimeBodyPart relatedBodyPart = new MimeBodyPart();
  relatedBodyPart.setContent(nestedRelatedMultipart);
  rootMixedMultipart.addBodyPart(relatedBodyPart);
  setMimeMultiparts(rootMixedMultipart, nestedRelatedMultipart);
  break;

代码示例来源:origin: org.springframework/spring-context-support

/**
 * Add an inline element to the MimeMessage, taking the content from a
 * {@code javax.activation.DataSource}.
 * <p>Note that the InputStream returned by the DataSource implementation
 * needs to be a <i>fresh one on each call</i>, as JavaMail will invoke
 * {@code getInputStream()} multiple times.
 * <p><b>NOTE:</b> Invoke {@code addInline} <i>after</i> {@link #setText};
 * else, mail readers might not be able to resolve inline references correctly.
 * @param contentId the content ID to use. Will end up as "Content-ID" header
 * in the body part, surrounded by angle brackets: e.g. "myId" -> "&lt;myId&gt;".
 * Can be referenced in HTML source via src="cid:myId" expressions.
 * @param dataSource the {@code javax.activation.DataSource} to take
 * the content from, determining the InputStream and the content type
 * @throws MessagingException in case of errors
 * @see #addInline(String, java.io.File)
 * @see #addInline(String, org.springframework.core.io.Resource)
 */
public void addInline(String contentId, DataSource dataSource) throws MessagingException {
  Assert.notNull(contentId, "Content ID must not be null");
  Assert.notNull(dataSource, "DataSource must not be null");
  MimeBodyPart mimeBodyPart = new MimeBodyPart();
  mimeBodyPart.setDisposition(MimeBodyPart.INLINE);
  // We're using setHeader here to remain compatible with JavaMail 1.2,
  // rather than JavaMail 1.3's setContentID.
  mimeBodyPart.setHeader(HEADER_CONTENT_ID, "<" + contentId + ">");
  mimeBodyPart.setDataHandler(new DataHandler(dataSource));
  getMimeMultipart().addBodyPart(mimeBodyPart);
}

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

MimeMultipart multipart = new MimeMultipart();
String altType = getContentType(bodyFormat.getClass().getName());
setContent(body, buf, altType == null ? contentType : altType);
multipart.addBodyPart(body);
    multipart.addBodyPart(parts[i]);

代码示例来源:origin: azkaban/azkaban

this._enableAttachementEmbedment ? new MimeMultipart("related")
    : new MimeMultipart();
final BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(this._body.toString(), this._mimeType);
multipart.addBodyPart(messageBodyPart);
 multipart.addBodyPart(part);

代码示例来源:origin: org.springframework/spring-context-support

private MimeBodyPart getMainPart() throws MessagingException {
  MimeMultipart mimeMultipart = getMimeMultipart();
  MimeBodyPart bodyPart = null;
  for (int i = 0; i < mimeMultipart.getCount(); i++) {
    BodyPart bp = mimeMultipart.getBodyPart(i);
    if (bp.getFileName() == null) {
      bodyPart = (MimeBodyPart) bp;
    }
  }
  if (bodyPart == null) {
    MimeBodyPart mimeBodyPart = new MimeBodyPart();
    mimeMultipart.addBodyPart(mimeBodyPart);
    bodyPart = mimeBodyPart;
  }
  return bodyPart;
}

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

setContent(body, buf, altType == null ? contentType : altType);
if (body != msg) {
  final MimeMultipart multipart = new MimeMultipart();
  multipart.addBodyPart((BodyPart) body);
      multipart.addBodyPart(parts[i]);

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

private Object createMessageContent() throws IOException, MessagingException {
 MimeMultipart content = new MimeMultipart();
 MimeBodyPart contentText = new MimeBodyPart();
 contentText.setText( "Hello World!" );
 content.addBodyPart( contentText );
 MimeBodyPart contentFile = new MimeBodyPart();
 File testFile = TestUtils.getInputFile( "GetPOP", "txt" );
 FileDataSource fds = new FileDataSource( testFile.getAbsolutePath() );
 contentFile.setDataHandler( new DataHandler( fds ) );
 contentFile.setFileName( testFile.getName() );
 content.addBodyPart( contentFile );
 return content;
}

代码示例来源:origin: org.springframework/spring-context-support

/**
 * Add an attachment to the MimeMessage, taking the content from a
 * {@code javax.activation.DataSource}.
 * <p>Note that the InputStream returned by the DataSource implementation
 * needs to be a <i>fresh one on each call</i>, as JavaMail will invoke
 * {@code getInputStream()} multiple times.
 * @param attachmentFilename the name of the attachment as it will
 * appear in the mail (the content type will be determined by this)
 * @param dataSource the {@code javax.activation.DataSource} to take
 * the content from, determining the InputStream and the content type
 * @throws MessagingException in case of errors
 * @see #addAttachment(String, org.springframework.core.io.InputStreamSource)
 * @see #addAttachment(String, java.io.File)
 */
public void addAttachment(String attachmentFilename, DataSource dataSource) throws MessagingException {
  Assert.notNull(attachmentFilename, "Attachment filename must not be null");
  Assert.notNull(dataSource, "DataSource must not be null");
  try {
    MimeBodyPart mimeBodyPart = new MimeBodyPart();
    mimeBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
    mimeBodyPart.setFileName(MimeUtility.encodeText(attachmentFilename));
    mimeBodyPart.setDataHandler(new DataHandler(dataSource));
    getRootMimeMultipart().addBodyPart(mimeBodyPart);
  }
  catch (UnsupportedEncodingException ex) {
    throw new MessagingException("Failed to encode attachment filename", ex);
  }
}

相关文章