javax.activation.DataHandler.<init>()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(12.8k)|赞(0)|评价(0)|浏览(323)

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

DataHandler.<init>介绍

[英]Create a DataHandler instance representing an object of this MIME type. This constructor is used when the application already has an in-memory representation of the data in the form of a Java Object.
[中]

代码示例

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

_auth = true; // smtp authentication - default on
multipart = new MimeMultipart();
  BodyPart messageBodyPart = new MimeBodyPart();
  messageBodyPart.setText(body);
  multipart.addBodyPart(messageBodyPart);
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(new File(filename).getName());

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

private Multipart _multipart; 
_multipart = new MimeMultipart(); 

public void addAttachment(String filename,String subject) throws Exception { 
  BodyPart messageBodyPart = new MimeBodyPart(); 
  DataSource source = new FileDataSource(filename); 
  messageBodyPart.setDataHandler(new DataHandler(source)); 
  messageBodyPart.setFileName(filename); 
  _multipart.addBodyPart(messageBodyPart);

  BodyPart messageBodyPart2 = new MimeBodyPart(); 
  messageBodyPart2.setText(subject); 

  _multipart.addBodyPart(messageBodyPart2); 
} 


message.setContent(_multipart);

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

public EmailMessage addAttachment(final String attachmentName, final File file)
  throws MessagingException {
 this._totalAttachmentSizeSoFar += file.length();
 if (this._totalAttachmentSizeSoFar > _totalAttachmentMaxSizeInByte) {
  throw new MessageAttachmentExceededMaximumSizeException(
    "Adding attachment '" + attachmentName
      + "' will exceed the allowed maximum size of "
      + _totalAttachmentMaxSizeInByte);
 }
 final BodyPart attachmentPart = new MimeBodyPart();
 final DataSource fileDataSource = new FileDataSource(file);
 attachmentPart.setDataHandler(new DataHandler(fileDataSource));
 attachmentPart.setFileName(attachmentName);
 this._attachments.add(attachmentPart);
 return this;
}

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

_auth = true; // smtp authentication - default on 
_multipart = new MimeMultipart(); 
 DataHandler handler = new DataHandler(new ByteArrayDataSource(_body.getBytes(), "text/plain")); 
 MimeMessage msg = new MimeMessage(session);

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

MimeMultipart messageContent = new MimeMultipart();

BodyPart bodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(yourFile);
bodyPart.setDataHandler(new DataHandler(source);
bodyPart.setFileName("MyFile.ext");
bodyPart.setDisposition(Part.ATTACHMENT);

// Then add to your message:
messageContent.addBodyPart(bodyPart);

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

private static void addAttachment(Multipart multipart, String filename)
{
  DataSource source = new FileDataSource(filename);
  BodyPart messageBodyPart = new MimeBodyPart();        
  messageBodyPart.setDataHandler(new DataHandler(source));
  messageBodyPart.setFileName(filename);
  multipart.addBodyPart(messageBodyPart);
}

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

// create a multipart leg for a specific attach
MimeMultipart part = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler (new DataHandler(new ByteArrayDataSource(attachFull.getBytes(), "text/plain")));
messageBodyPart.removeHeader("Content-Transfer-Encoding");
messageBodyPart.addHeader("Content-Transfer-Encoding", "base64");
part.addBodyPart(messageBodyPart);
email.addPart(part);

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

private static void addAttachment(Multipart multipart, String filename)
{
  DataSource source = new FileDataSource(filename);
  BodyPart messageBodyPart = new MimeBodyPart();        
  messageBodyPart.setDataHandler(new DataHandler(source));
  messageBodyPart.setFileName(filename);
  multipart.addBodyPart(messageBodyPart);
}

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

相关文章