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

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

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

MimeBodyPart.setDataHandler介绍

[英]This method provides the mechanism to set this body part's content. The given DataHandler object should wrap the actual content.
[中]此方法提供了设置此主体部分内容的机制。给定的DataHandler对象应该包装实际内容。

代码示例

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

if (arrayInputStream != null && arrayInputStream instanceof ByteArrayInputStream) {
   // create the second message part with the attachment from a OutputStrean
   MimeBodyPart attachment= new MimeBodyPart();
   ByteArrayDataSource ds = new ByteArrayDataSource(arrayInputStream, "application/pdf"); 
   attachment.setDataHandler(new DataHandler(ds));
   attachment.setFileName("Report.pdf");
   mimeMultipart.addBodyPart(attachment);
 }

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

Multipart multipart = new MimeMultipart("mixed");
for (String str : attachment_PathList) {
  MimeBodyPart messageBodyPart = new MimeBodyPart();
  DataSource source = new FileDataSource(str);
  messageBodyPart.setDataHandler(new DataHandler(source));
  messageBodyPart.setFileName(source.getName());
  multipart.addBodyPart(messageBodyPart);
}
msg.setContent(multipart);
Transport.send(msg);

代码示例来源: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: camunda/camunda-bpm-platform

/**
 * Use the specified file to provide the data for this part.
 * The simple file name is used as the file name for this
 * part and the data in the file is used as the data for this
 * part.  The encoding will be chosen appropriately for the
 * file data.  The disposition of this part is set to
 * {@link Part#ATTACHMENT Part.ATTACHMENT}.
 *
 * @param        file        the File object to attach
 * @exception    IOException    errors related to accessing the file
 * @exception    MessagingException    message related errors
 * @since        JavaMail 1.4
 */
public void attachFile(File file) throws IOException, MessagingException {
FileDataSource fds = new FileDataSource(file);   	
this.setDataHandler(new DataHandler(fds));
this.setFileName(fds.getName());
this.setDisposition(ATTACHMENT);
}

代码示例来源:origin: apache/nifi

@Override
  public void process(final InputStream stream) throws IOException {
    try {
      mimeFile.setDataHandler(new DataHandler(new ByteArrayDataSource(stream, "application/octet-stream")));
    } catch (final Exception e) {
      throw new IOException(e);
    }
  }
});

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

SimpleEmail email = new SimpleEmail();

MimeMultipart mmpa = new MimeMultipart("alternative");

//Calendar
MimeBodyPart calendarPart = new MimeBodyPart();
calendarPart.setHeader("Content-Type", "text/calendar; charset=UTF-8; method=REQUEST");
ByteArrayDataSource dsCalendario = new ByteArrayDataSource(str,"text/calendar;method=REQUEST");
DataHandler dhCalendario = new DataHandler(dsCalendario);
calendarPart.setDataHandler(dhCalendario);
mmpa.addBodyPart(calendarPart);

email.setContent(mmpa);

代码示例来源:origin: blynkkk/blynk-server

private void attachCSV(Multipart multipart, QrHolder[] attachmentData) throws Exception {
  StringBuilder sb = new StringBuilder();
  for (QrHolder qrHolder : attachmentData) {
    sb.append(qrHolder.token)
    .append(",")
    .append(qrHolder.deviceId)
    .append(",")
    .append(qrHolder.dashId)
    .append("\n");
  }
  MimeBodyPart attachmentsPart = new MimeBodyPart();
  ByteArrayDataSource source = new ByteArrayDataSource(sb.toString(), "text/csv");
  attachmentsPart.setDataHandler(new DataHandler(source));
  attachmentsPart.setFileName("tokens.csv");
  multipart.addBodyPart(attachmentsPart);
}

代码示例来源: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: com.sun.mail/javax.mail

/**
 * Use the specified file to provide the data for this part.
 * The simple file name is used as the file name for this
 * part and the data in the file is used as the data for this
 * part.  The encoding will be chosen appropriately for the
 * file data.  The disposition of this part is set to
 * {@link Part#ATTACHMENT Part.ATTACHMENT}.
 *
 * @param        file        the File object to attach
 * @exception    IOException    errors related to accessing the file
 * @exception    MessagingException    message related errors
 * @since        JavaMail 1.4
 */
public void attachFile(File file) throws IOException, MessagingException {
FileDataSource fds = new FileDataSource(file);   	
this.setDataHandler(new DataHandler(fds));
this.setFileName(fds.getName());
this.setDisposition(ATTACHMENT);
}

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

/**
 * A convenience method for setting this body part's content. <p>
 *
 * The content is wrapped in a DataHandler object. Note that a
 * DataContentHandler class for the specified type should be
 * available to the JavaMail implementation for this to work right.
 * That is, to do <code>setContent(foobar, "application/x-foobar")</code>,
 * a DataContentHandler for "application/x-foobar" should be installed.
 * Refer to the Java Activation Framework for more information.
 *
 * @param    o    the content object
 * @param    type    Mime type of the object
 * @exception       IllegalWriteException if the underlying
 *            implementation does not support modification of
 *            existing values
 * @exception    IllegalStateException if this body part is
 *            obtained from a READ_ONLY folder.
 */
public void setContent(Object o, String type) 
  throws MessagingException {
if (o instanceof Multipart) {
  setContent((Multipart)o);
} else {
  setDataHandler(new DataHandler(o, type));
}
}

代码示例来源:origin: blynkkk/blynk-server

private void attachQRs(Multipart multipart, QrHolder[] attachmentData) throws Exception {
  for (QrHolder qrHolder : attachmentData) {
    MimeBodyPart attachmentsPart = new MimeBodyPart();
    ByteArrayDataSource source = new ByteArrayDataSource(qrHolder.data, "image/jpeg");
    attachmentsPart.setDataHandler(new DataHandler(source));
    attachmentsPart.setFileName(qrHolder.makeQRFilename());
    multipart.addBodyPart(attachmentsPart);
  }
}

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

MimeBodyPart mbp = new MimeBodyPart();
String data = "any ASCII data";
DataSource ds = new ByteArrayDataSource(data, "application/x-any");
mbp.setDataHandler(new DataHandler(ds));

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

/**
 * Use the specified file with the specified Content-Type and
 * Content-Transfer-Encoding to provide the data for this part.
 * If contentType or encoding are null, appropriate values will
 * be chosen.
 * The simple file name is used as the file name for this
 * part and the data in the file is used as the data for this
 * part.  The disposition of this part is set to
 * {@link Part#ATTACHMENT Part.ATTACHMENT}.
 *
 * @param        file        the File object to attach
 * @param        contentType    the Content-Type, or null
 * @param        encoding    the Content-Transfer-Encoding, or null
 * @exception    IOException    errors related to accessing the file
 * @exception    MessagingException    message related errors
 * @since        JavaMail 1.5
 */
public void attachFile(File file, String contentType, String encoding)
      throws IOException, MessagingException {
DataSource fds = new EncodedFileDataSource(file, contentType, encoding);
this.setDataHandler(new DataHandler(fds));
this.setFileName(fds.getName());
this.setDisposition(ATTACHMENT);
}

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

/**
 * A convenience method for setting this body part's content. <p>
 *
 * The content is wrapped in a DataHandler object. Note that a
 * DataContentHandler class for the specified type should be
 * available to the JavaMail implementation for this to work right.
 * That is, to do <code>setContent(foobar, "application/x-foobar")</code>,
 * a DataContentHandler for "application/x-foobar" should be installed.
 * Refer to the Java Activation Framework for more information.
 *
 * @param    o    the content object
 * @param    type    Mime type of the object
 * @exception       IllegalWriteException if the underlying
 *            implementation does not support modification of
 *            existing values
 * @exception    IllegalStateException if this body part is
 *            obtained from a READ_ONLY folder.
 */
@Override
public void setContent(Object o, String type) 
  throws MessagingException {
if (o instanceof Multipart) {
  setContent((Multipart)o);
} else {
  setDataHandler(new DataHandler(o, type));
}
}

代码示例来源: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: pentaho/pentaho-kettle

MimeBodyPart imagePart = new MimeBodyPart();
imagePart.setDataHandler( new DataHandler( fds ) );

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

/**
 * Use the specified file with the specified Content-Type and
 * Content-Transfer-Encoding to provide the data for this part.
 * If contentType or encoding are null, appropriate values will
 * be chosen.
 * The simple file name is used as the file name for this
 * part and the data in the file is used as the data for this
 * part.  The disposition of this part is set to
 * {@link Part#ATTACHMENT Part.ATTACHMENT}.
 *
 * @param        file        the File object to attach
 * @param        contentType    the Content-Type, or null
 * @param        encoding    the Content-Transfer-Encoding, or null
 * @exception    IOException    errors related to accessing the file
 * @exception    MessagingException    message related errors
 * @since        JavaMail 1.5
 */
public void attachFile(File file, String contentType, String encoding)
      throws IOException, MessagingException {
DataSource fds = new EncodedFileDataSource(file, contentType, encoding);
this.setDataHandler(new DataHandler(fds));
this.setFileName(fds.getName());
this.setDisposition(ATTACHMENT);
}

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

/**
 * This method sets the body part's content to a Multipart object.
 *
 * @param  mp          The multipart object that is the Message's content
 * @exception       IllegalWriteException if the underlying
 *            implementation does not support modification of
 *            existing values.
 * @exception    IllegalStateException if this body part is
 *            obtained from a READ_ONLY folder.
 */
public void setContent(Multipart mp) throws MessagingException {
setDataHandler(new DataHandler(mp, mp.getContentType()));
mp.setParent(this);
}

代码示例来源: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

/**
 * This method sets the body part's content to a Multipart object.
 *
 * @param  mp          The multipart object that is the Message's content
 * @exception       IllegalWriteException if the underlying
 *            implementation does not support modification of
 *            existing values.
 * @exception    IllegalStateException if this body part is
 *            obtained from a READ_ONLY folder.
 */
@Override
public void setContent(Multipart mp) throws MessagingException {
setDataHandler(new DataHandler(mp, mp.getContentType()));
mp.setParent(this);
}

相关文章