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

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

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

MimeBodyPart.setDisposition介绍

[英]Set the disposition in the "Content-Disposition" header field of this body part. If the disposition is null, any existing "Content-Disposition" header field is removed.
[中]在此正文部分的“内容处置”标题字段中设置处置。如果处置为空,则删除任何现有的“内容处置”标题字段。

代码示例

代码示例来源: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: 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: 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: org.springframework/spring-context-support

  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: igniterealtime/Openfire

  1. text.setDisposition(Part.INLINE);
  2. content.addBodyPart(text);
  3. html.setDisposition(Part.INLINE);
  4. html.setHeader("Content-Transfer-Encoding", "8bit");
  5. content.addBodyPart(html);
  6. MimeBodyPart bPart = new MimeBodyPart();
  7. bPart.setText(textBody, encoding);
  8. bPart.setDisposition(Part.INLINE);
  9. bPart.setHeader("Content-Transfer-Encoding", "8bit");
  10. MimeMultipart mPart = new MimeMultipart();
  11. MimeBodyPart bPart = new MimeBodyPart();
  12. bPart.setContent(htmlBody, "text/html; charset=UTF-8");
  13. bPart.setDisposition(Part.INLINE);
  14. bPart.setHeader("Content-Transfer-Encoding", "8bit");
  15. MimeMultipart mPart = new MimeMultipart();

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

  1. /**
  2. * Creates attachment body part. Handles regular and inline attachments.
  3. *
  4. * @param attachment Body part {@link EmailAttachment}.
  5. * @return {@link MimeBodyPart} which represents body part attachment.
  6. * @throws MessagingException if there is a failure.
  7. */
  8. protected MimeBodyPart createAttachmentBodyPart(final EmailAttachment<? extends DataSource> attachment) throws MessagingException {
  9. final MimeBodyPart part = new MimeBodyPart();
  10. final String attachmentName = attachment.getEncodedName();
  11. if (attachmentName != null) {
  12. part.setFileName(attachmentName);
  13. }
  14. part.setDataHandler(new DataHandler(attachment.getDataSource()));
  15. if (attachment.getContentId() != null) {
  16. part.setContentID(StringPool.LEFT_CHEV + attachment.getContentId() + StringPool.RIGHT_CHEV);
  17. }
  18. if (attachment.isInline()) {
  19. part.setDisposition(INLINE);
  20. }
  21. return part;
  22. }

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

  1. /**
  2. * Set the disposition in the "Content-Disposition" header field
  3. * of this body part. If the disposition is null, any existing
  4. * "Content-Disposition" header field is removed.
  5. *
  6. * @exception IllegalWriteException if the underlying
  7. * implementation does not support modification
  8. * @exception IllegalStateException if this message is
  9. * obtained from a READ_ONLY folder.
  10. * @exception MessagingException for other failures
  11. */
  12. @Override
  13. public void setDisposition(String disposition) throws MessagingException {
  14. MimeBodyPart.setDisposition(this, disposition);
  15. }

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

  1. /**
  2. * Set the disposition in the "Content-Disposition" header field
  3. * of this body part. If the disposition is null, any existing
  4. * "Content-Disposition" header field is removed.
  5. *
  6. * @exception IllegalWriteException if the underlying
  7. * implementation does not support modification
  8. * @exception IllegalStateException if this body part is
  9. * obtained from a READ_ONLY folder.
  10. * @exception MessagingException for other failures
  11. */
  12. public void setDisposition(String disposition) throws MessagingException {
  13. setDisposition(this, disposition);
  14. }

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

  1. /**
  2. * Set the disposition in the "Content-Disposition" header field
  3. * of this body part. If the disposition is null, any existing
  4. * "Content-Disposition" header field is removed.
  5. *
  6. * @exception IllegalWriteException if the underlying
  7. * implementation does not support modification
  8. * @exception IllegalStateException if this message is
  9. * obtained from a READ_ONLY folder.
  10. * @exception MessagingException for other failures
  11. */
  12. public void setDisposition(String disposition) throws MessagingException {
  13. MimeBodyPart.setDisposition(this, disposition);
  14. }

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

  1. /**
  2. * Set the disposition in the "Content-Disposition" header field
  3. * of this body part. If the disposition is null, any existing
  4. * "Content-Disposition" header field is removed.
  5. *
  6. * @exception IllegalWriteException if the underlying
  7. * implementation does not support modification
  8. * @exception IllegalStateException if this body part is
  9. * obtained from a READ_ONLY folder.
  10. * @exception MessagingException for other failures
  11. */
  12. @Override
  13. public void setDisposition(String disposition) throws MessagingException {
  14. setDisposition(this, disposition);
  15. }

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

  1. MimeBodyPart imagePart = new MimeBodyPart();
  2. imagePart.attachFile("resources/teapot.jpg");
  3. imagePart.setContentID("<" + cid + ">");
  4. imagePart.setDisposition(MimeBodyPart.INLINE);
  5. content.addBodyPart(imagePart);

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

  1. /**
  2. * Creates the attachment part.
  3. *
  4. * @param attachment the attachment
  5. * @return the mime body part
  6. * @throws MessagingException the messaging exception
  7. */
  8. private static MimeBodyPart createAttachmentPart(DataSource attachment)
  9. throws MessagingException {
  10. MimeBodyPart attachmentPart = new MimeBodyPart();
  11. attachmentPart.setDataHandler(new DataHandler(attachment));
  12. attachmentPart.setDisposition(Part.ATTACHMENT);
  13. attachmentPart.setFileName(attachment.getName());
  14. return attachmentPart;
  15. }

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

  1. /**
  2. * Factory to create the attachment body part.
  3. * @param index the attachment index.
  4. * @return a body part with default headers set.
  5. * @throws MessagingException if there is a problem.
  6. * @throws IndexOutOfBoundsException if the given index is not an valid
  7. * attachment index.
  8. */
  9. private MimeBodyPart createBodyPart(int index) throws MessagingException {
  10. assert Thread.holdsLock(this);
  11. final MimeBodyPart part = new MimeBodyPart();
  12. part.setDisposition(Part.ATTACHMENT);
  13. part.setDescription(descriptionFrom(
  14. attachmentFormatters[index],
  15. attachmentFilters[index],
  16. attachmentNames[index]));
  17. setAcceptLang(part);
  18. return part;
  19. }

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

  1. /**
  2. * Factory to create the attachment body part.
  3. * @param index the attachment index.
  4. * @return a body part with default headers set.
  5. * @throws MessagingException if there is a problem.
  6. * @throws IndexOutOfBoundsException if the given index is not an valid
  7. * attachment index.
  8. */
  9. private MimeBodyPart createBodyPart(int index) throws MessagingException {
  10. assert Thread.holdsLock(this);
  11. final MimeBodyPart part = new MimeBodyPart();
  12. part.setDisposition(Part.ATTACHMENT);
  13. part.setDescription(descriptionFrom(
  14. attachmentFormatters[index],
  15. attachmentFilters[index],
  16. attachmentNames[index]));
  17. setAcceptLang(part);
  18. return part;
  19. }

代码示例来源: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. * Factory to create the in-line body part.
  3. * @return a body part with default headers set.
  4. * @throws MessagingException if there is a problem.
  5. */
  6. private MimeBodyPart createBodyPart() throws MessagingException {
  7. assert Thread.holdsLock(this);
  8. final MimeBodyPart part = new MimeBodyPart();
  9. part.setDisposition(Part.INLINE);
  10. part.setDescription(descriptionFrom(getFormatter(),
  11. getFilter(), subjectFormatter));
  12. setAcceptLang(part);
  13. return part;
  14. }

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

  1. /**
  2. * Factory to create the in-line body part.
  3. * @return a body part with default headers set.
  4. * @throws MessagingException if there is a problem.
  5. */
  6. private MimeBodyPart createBodyPart() throws MessagingException {
  7. assert Thread.holdsLock(this);
  8. final MimeBodyPart part = new MimeBodyPart();
  9. part.setDisposition(Part.INLINE);
  10. part.setDescription(descriptionFrom(getFormatter(),
  11. getFilter(), subjectFormatter));
  12. setAcceptLang(part);
  13. return part;
  14. }

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

  1. MimeMultipart rootContainer = new MimeMultipart();
  2. rootContainer.setSubType("related");
  3. rootContainer.addBodyPart(alternativeMultiPartWithPlainTextAndHtml); // not in focus here
  4. rootContainer.addBodyPart(createInlineImagePart(base64EncodedImageContentByteArray));
  5. ...
  6. message.setContent(rootContainer);
  7. message.setHeader("MIME-Version", "1.0");
  8. message.setHeader("Content-Type", rootContainer.getContentType());
  9. ...
  10. BodyPart createInlineImagePart(byte[] base64EncodedImageContentByteArray) throws MessagingException {
  11. InternetHeaders headers = new InternetHeaders();
  12. headers.addHeader("Content-Type", "image/jpeg");
  13. headers.addHeader("Content-Transfer-Encoding", "base64");
  14. MimeBodyPart imagePart = new MimeBodyPart(headers, base64EncodedImageContentByteArray);
  15. imagePart.setDisposition(MimeBodyPart.INLINE);
  16. imagePart.setContentID("&lt;image&gt;");
  17. imagePart.setFileName("image.jpg");
  18. return imagePart;

代码示例来源:origin: webx/citrus

  1. private void renderInlineResource(Multipart multipart, InlineResource inlineResource, Set<String> fileNames)
  2. throws MessagingException {
  3. assertNotNull(resourceLoader, "no resourceLoader");
  4. String resourceName = inlineResource.getResourceName();
  5. Resource resource = resourceLoader.getResource(resourceName);
  6. if (!resource.exists()) {
  7. throw new MailBuilderException("Could not find resource \"" + resourceName + "\"");
  8. }
  9. DataSource ds;
  10. try {
  11. ds = new URLDataSource(resource.getURL());
  12. } catch (IOException e) {
  13. ds = new ResourceDataSource(resource);
  14. }
  15. MimeBodyPart bodyPart = new MimeBodyPart();
  16. bodyPart.setDataHandler(new DataHandler(ds));
  17. bodyPart.setHeader(CONTENT_ID, "<" + inlineResource.getContentId() + ">");
  18. bodyPart.setFileName(inlineResource.getUniqueFilename(fileNames));
  19. bodyPart.setDisposition("inline");
  20. multipart.addBodyPart(bodyPart);
  21. }

相关文章