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

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

本文整理了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

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

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

  1. Multipart multipart = new MimeMultipart("mixed");
  2. for (String str : attachment_PathList) {
  3. MimeBodyPart messageBodyPart = new MimeBodyPart();
  4. DataSource source = new FileDataSource(str);
  5. messageBodyPart.setDataHandler(new DataHandler(source));
  6. messageBodyPart.setFileName(source.getName());
  7. multipart.addBodyPart(messageBodyPart);
  8. }
  9. msg.setContent(multipart);
  10. Transport.send(msg);

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

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

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

  1. /**
  2. * Use the specified file to provide the data for this part.
  3. * The simple file name is used as the file name for this
  4. * part and the data in the file is used as the data for this
  5. * part. The encoding will be chosen appropriately for the
  6. * file data. The disposition of this part is set to
  7. * {@link Part#ATTACHMENT Part.ATTACHMENT}.
  8. *
  9. * @param file the File object to attach
  10. * @exception IOException errors related to accessing the file
  11. * @exception MessagingException message related errors
  12. * @since JavaMail 1.4
  13. */
  14. public void attachFile(File file) throws IOException, MessagingException {
  15. FileDataSource fds = new FileDataSource(file);
  16. this.setDataHandler(new DataHandler(fds));
  17. this.setFileName(fds.getName());
  18. this.setDisposition(ATTACHMENT);
  19. }

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

  1. @Override
  2. public void process(final InputStream stream) throws IOException {
  3. try {
  4. mimeFile.setDataHandler(new DataHandler(new ByteArrayDataSource(stream, "application/octet-stream")));
  5. } catch (final Exception e) {
  6. throw new IOException(e);
  7. }
  8. }
  9. });

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

  1. SimpleEmail email = new SimpleEmail();
  2. MimeMultipart mmpa = new MimeMultipart("alternative");
  3. //Calendar
  4. MimeBodyPart calendarPart = new MimeBodyPart();
  5. calendarPart.setHeader("Content-Type", "text/calendar; charset=UTF-8; method=REQUEST");
  6. ByteArrayDataSource dsCalendario = new ByteArrayDataSource(str,"text/calendar;method=REQUEST");
  7. DataHandler dhCalendario = new DataHandler(dsCalendario);
  8. calendarPart.setDataHandler(dhCalendario);
  9. mmpa.addBodyPart(calendarPart);
  10. email.setContent(mmpa);

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

  1. private void attachCSV(Multipart multipart, QrHolder[] attachmentData) throws Exception {
  2. StringBuilder sb = new StringBuilder();
  3. for (QrHolder qrHolder : attachmentData) {
  4. sb.append(qrHolder.token)
  5. .append(",")
  6. .append(qrHolder.deviceId)
  7. .append(",")
  8. .append(qrHolder.dashId)
  9. .append("\n");
  10. }
  11. MimeBodyPart attachmentsPart = new MimeBodyPart();
  12. ByteArrayDataSource source = new ByteArrayDataSource(sb.toString(), "text/csv");
  13. attachmentsPart.setDataHandler(new DataHandler(source));
  14. attachmentsPart.setFileName("tokens.csv");
  15. multipart.addBodyPart(attachmentsPart);
  16. }

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

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

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

  1. /**
  2. * Use the specified file to provide the data for this part.
  3. * The simple file name is used as the file name for this
  4. * part and the data in the file is used as the data for this
  5. * part. The encoding will be chosen appropriately for the
  6. * file data. The disposition of this part is set to
  7. * {@link Part#ATTACHMENT Part.ATTACHMENT}.
  8. *
  9. * @param file the File object to attach
  10. * @exception IOException errors related to accessing the file
  11. * @exception MessagingException message related errors
  12. * @since JavaMail 1.4
  13. */
  14. public void attachFile(File file) throws IOException, MessagingException {
  15. FileDataSource fds = new FileDataSource(file);
  16. this.setDataHandler(new DataHandler(fds));
  17. this.setFileName(fds.getName());
  18. this.setDisposition(ATTACHMENT);
  19. }

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

  1. /**
  2. * A convenience method for setting this body part's content. <p>
  3. *
  4. * The content is wrapped in a DataHandler object. Note that a
  5. * DataContentHandler class for the specified type should be
  6. * available to the JavaMail implementation for this to work right.
  7. * That is, to do <code>setContent(foobar, "application/x-foobar")</code>,
  8. * a DataContentHandler for "application/x-foobar" should be installed.
  9. * Refer to the Java Activation Framework for more information.
  10. *
  11. * @param o the content object
  12. * @param type Mime type of the object
  13. * @exception IllegalWriteException if the underlying
  14. * implementation does not support modification of
  15. * existing values
  16. * @exception IllegalStateException if this body part is
  17. * obtained from a READ_ONLY folder.
  18. */
  19. public void setContent(Object o, String type)
  20. throws MessagingException {
  21. if (o instanceof Multipart) {
  22. setContent((Multipart)o);
  23. } else {
  24. setDataHandler(new DataHandler(o, type));
  25. }
  26. }

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

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

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

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

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

  1. /**
  2. * Use the specified file with the specified Content-Type and
  3. * Content-Transfer-Encoding to provide the data for this part.
  4. * If contentType or encoding are null, appropriate values will
  5. * be chosen.
  6. * The simple file name is used as the file name for this
  7. * part and the data in the file is used as the data for this
  8. * part. The disposition of this part is set to
  9. * {@link Part#ATTACHMENT Part.ATTACHMENT}.
  10. *
  11. * @param file the File object to attach
  12. * @param contentType the Content-Type, or null
  13. * @param encoding the Content-Transfer-Encoding, or null
  14. * @exception IOException errors related to accessing the file
  15. * @exception MessagingException message related errors
  16. * @since JavaMail 1.5
  17. */
  18. public void attachFile(File file, String contentType, String encoding)
  19. throws IOException, MessagingException {
  20. DataSource fds = new EncodedFileDataSource(file, contentType, encoding);
  21. this.setDataHandler(new DataHandler(fds));
  22. this.setFileName(fds.getName());
  23. this.setDisposition(ATTACHMENT);
  24. }

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

  1. /**
  2. * A convenience method for setting this body part's content. <p>
  3. *
  4. * The content is wrapped in a DataHandler object. Note that a
  5. * DataContentHandler class for the specified type should be
  6. * available to the JavaMail implementation for this to work right.
  7. * That is, to do <code>setContent(foobar, "application/x-foobar")</code>,
  8. * a DataContentHandler for "application/x-foobar" should be installed.
  9. * Refer to the Java Activation Framework for more information.
  10. *
  11. * @param o the content object
  12. * @param type Mime type of the object
  13. * @exception IllegalWriteException if the underlying
  14. * implementation does not support modification of
  15. * existing values
  16. * @exception IllegalStateException if this body part is
  17. * obtained from a READ_ONLY folder.
  18. */
  19. @Override
  20. public void setContent(Object o, String type)
  21. throws MessagingException {
  22. if (o instanceof Multipart) {
  23. setContent((Multipart)o);
  24. } else {
  25. setDataHandler(new DataHandler(o, type));
  26. }
  27. }

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

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

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

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

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

  1. /**
  2. * Use the specified file with the specified Content-Type and
  3. * Content-Transfer-Encoding to provide the data for this part.
  4. * If contentType or encoding are null, appropriate values will
  5. * be chosen.
  6. * The simple file name is used as the file name for this
  7. * part and the data in the file is used as the data for this
  8. * part. The disposition of this part is set to
  9. * {@link Part#ATTACHMENT Part.ATTACHMENT}.
  10. *
  11. * @param file the File object to attach
  12. * @param contentType the Content-Type, or null
  13. * @param encoding the Content-Transfer-Encoding, or null
  14. * @exception IOException errors related to accessing the file
  15. * @exception MessagingException message related errors
  16. * @since JavaMail 1.5
  17. */
  18. public void attachFile(File file, String contentType, String encoding)
  19. throws IOException, MessagingException {
  20. DataSource fds = new EncodedFileDataSource(file, contentType, encoding);
  21. this.setDataHandler(new DataHandler(fds));
  22. this.setFileName(fds.getName());
  23. this.setDisposition(ATTACHMENT);
  24. }

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

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

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

  1. /**
  2. * Add an attachment to the MimeMessage, taking the content from a
  3. * {@code javax.activation.DataSource}.
  4. * <p>Note that the InputStream returned by the DataSource implementation
  5. * needs to be a <i>fresh one on each call</i>, as JavaMail will invoke
  6. * {@code getInputStream()} multiple times.
  7. * @param attachmentFilename the name of the attachment as it will
  8. * appear in the mail (the content type will be determined by this)
  9. * @param dataSource the {@code javax.activation.DataSource} to take
  10. * the content from, determining the InputStream and the content type
  11. * @throws MessagingException in case of errors
  12. * @see #addAttachment(String, org.springframework.core.io.InputStreamSource)
  13. * @see #addAttachment(String, java.io.File)
  14. */
  15. public void addAttachment(String attachmentFilename, DataSource dataSource) throws MessagingException {
  16. Assert.notNull(attachmentFilename, "Attachment filename must not be null");
  17. Assert.notNull(dataSource, "DataSource must not be null");
  18. try {
  19. MimeBodyPart mimeBodyPart = new MimeBodyPart();
  20. mimeBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
  21. mimeBodyPart.setFileName(MimeUtility.encodeText(attachmentFilename));
  22. mimeBodyPart.setDataHandler(new DataHandler(dataSource));
  23. getRootMimeMultipart().addBodyPart(mimeBodyPart);
  24. }
  25. catch (UnsupportedEncodingException ex) {
  26. throw new MessagingException("Failed to encode attachment filename", ex);
  27. }
  28. }

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

  1. /**
  2. * This method sets the body part's content to a Multipart object.
  3. *
  4. * @param mp The multipart object that is the Message's content
  5. * @exception IllegalWriteException if the underlying
  6. * implementation does not support modification of
  7. * existing values.
  8. * @exception IllegalStateException if this body part is
  9. * obtained from a READ_ONLY folder.
  10. */
  11. @Override
  12. public void setContent(Multipart mp) throws MessagingException {
  13. setDataHandler(new DataHandler(mp, mp.getContentType()));
  14. mp.setParent(this);
  15. }

相关文章