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

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

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

MimeBodyPart.setContentID介绍

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

代码示例

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

  1. String fileName = new File(attachmentUrl.getFile()).getName();
  2. attachementBodyPart.setFileName(fileName);
  3. attachementBodyPart.setContentID("<"+fileName+">");

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

  1. mbp.setFileName(name);
  2. mbp.setDisposition(EmailAttachment.INLINE);
  3. mbp.setContentID("<" + encodedCid + ">");

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

  1. // first part (the html)
  2. MimeBodyPart messageBodyPart = new MimeBodyPart();
  3. String htmlText = "<H1>This is the image: </H1><img src=\"cid:image\">";
  4. messageBodyPart.setText(htmlText, null, "html");
  5. multipart.addBodyPart(messageBodyPart);
  6. // second part (the image)
  7. messageBodyPart = new MimeBodyPart();
  8. String filePath = "C:/image.png";
  9. messageBodyPart.attachFile(filePath, "image/png", "base64");
  10. messageBodyPart.setContentID("<image>");
  11. // add image to the multipart
  12. multipart.addBodyPart(messageBodyPart);

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

  1. Multipart multipart = new MimeMultipart("related");
  2. MimeBodyPart htmlPart = new MimeBodyPart();
  3. // messageBody contains html that references image
  4. // using something like <img src="cid:XXX"> where
  5. // "XXX" is an identifier that you make up to refer
  6. // to the image
  7. htmlPart.setText(messageBody, "utf-8", "html");
  8. multipart.addBodyPart(htmlPart);
  9. MimeBodyPart imgPart = new MimeBodyPart();
  10. // imageFile is the file containing the image
  11. imgPart.attachFile(imageFile);
  12. // or, if the image is in a byte array in memory, use
  13. // imgPart.setDataHandler(new DataHandler(
  14. // new ByteArrayDataSource(bytes, "image/whatever")));
  15. // "XXX" below matches "XXX" above in html code
  16. imgPart.setContentID("<XXX>");
  17. multipart.addBodyPart(imgPart);
  18. message.setContent(multipart);

代码示例来源:origin: CloudSlang/cs-actions

  1. private MimeBodyPart getImageMimeBodyPart(Map<String, String> base64ImagesMap, String contentId)
  2. throws MessagingException {
  3. MimeBodyPart imagePart = new MimeBodyPart();
  4. imagePart.setContentID(contentId);
  5. imagePart.setHeader("Content-Transfer-Encoding", "base64");
  6. imagePart.setDataHandler(new DataHandler(Base64.decode(base64ImagesMap.get(contentId)), "image/png;"));
  7. return imagePart;
  8. }

代码示例来源:origin: hf-hf/mail-micro-service

  1. /**
  2. * 追加内嵌图片
  3. * @author hf-hf
  4. * @date 2018/12/27 16:53
  5. * @param images
  6. * @param multipart
  7. * @throws MessagingException
  8. */
  9. private void addImages(File[] images, MimeMultipart multipart) throws MessagingException {
  10. if (null != images && images.length > 0) {
  11. for (int i = 0; i < images.length; i++) {
  12. MimeBodyPart imagePart = new MimeBodyPart();
  13. DataHandler dataHandler = new DataHandler(new FileDataSource(images[i]));
  14. imagePart.setDataHandler(dataHandler);
  15. imagePart.setContentID(images[i].getName());
  16. multipart.addBodyPart(imagePart);
  17. }
  18. }
  19. }

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

  1. MimeBodyPart imgBodyPart = new MimeBodyPart();
  2. imgBodyPart.attachFile("Image.png");
  3. imgBodyPart.setContentID('<'+"i01@example.com"+'>');
  4. imgBodyPart.setDisposition(MimeBodyPart.INLINE);
  5. imgBodyPart.setHeader("Content-Type", "image/png");
  6. multipart.addBodyPart(imgBodyPart);

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

  1. String cid = generateCID();
  2. MimeBodyPart textPart = new MimeBodyPart();
  3. textPart.setText("<html><head>"
  4. + "<title>This is not usually displayed</title>"
  5. + "</head>n"
  6. + "<body><div><strong>Hi there!</strong></div>"
  7. + "<div>Sending HTML in email is so <em>cool!</em> </div>n"
  8. + "<div>And here's an image: <img src=\"cid:\"" + cid + " /></div>"
  9. + "<div>I hope you like it!</div></body></html>",
  10. "US-ASCII", "html");
  11. content.addBodyPart(textPart);
  12. MimeBodyPart imagePart = new MimeBodyPart();
  13. imagePart.attachFile("resources/teapot.jpg");
  14. imagePart.setContentID("<" + cid + ">");
  15. imagePart.setDisposition(MimeBodyPart.INLINE);
  16. content.addBodyPart(imagePart);

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

  1. img.setContentID(ei.getFilename());
  2. img.setFileName(ei.getFilename());
  3. ByteArrayDataSource bads = new ByteArrayDataSource(ei.getImageData(), ei.getMimeType());

代码示例来源:origin: com.xpn.xwiki.platform.plugins/xwiki-plugin-mailsender

  1. /**
  2. * Add attachments to a multipart message
  3. *
  4. * @param multipart Multipart message
  5. * @param attachments List of attachments
  6. */
  7. public MimeBodyPart createAttachmentBodyPart(Attachment attachment, XWikiContext context) throws XWikiException,
  8. IOException, MessagingException
  9. {
  10. String name = attachment.getFilename();
  11. byte[] stream = attachment.getContent();
  12. File temp = File.createTempFile("tmpfile", ".tmp");
  13. FileOutputStream fos = new FileOutputStream(temp);
  14. fos.write(stream);
  15. fos.close();
  16. DataSource source = new FileDataSource(temp);
  17. MimeBodyPart part = new MimeBodyPart();
  18. String mimeType = MimeTypesUtil.getMimeTypeFromFilename(name);
  19. part.setDataHandler(new DataHandler(source));
  20. part.setHeader("Content-Type", mimeType);
  21. part.setFileName(name);
  22. part.setContentID("<" + name + ">");
  23. part.setDisposition("inline");
  24. temp.deleteOnExit();
  25. return part;
  26. }

代码示例来源:origin: org.xwiki.platform/xwiki-platform-mailsender

  1. /**
  2. * Add attachments to a multipart message
  3. *
  4. * @param attachment the attachment to create the body part for.
  5. * @param context the XWiki context.
  6. * @return the body part for the given attachment.
  7. */
  8. public MimeBodyPart createAttachmentBodyPart(Attachment attachment, XWikiContext context) throws XWikiException,
  9. IOException, MessagingException
  10. {
  11. String name = attachment.getFilename();
  12. byte[] stream = attachment.getContent();
  13. File temp = new TemporaryFile(File.createTempFile("tmpfile", ".tmp"));
  14. FileOutputStream fos = new FileOutputStream(temp);
  15. fos.write(stream);
  16. fos.close();
  17. DataSource source = new FileDataSource(temp);
  18. MimeBodyPart part = new MimeBodyPart();
  19. String mimeType = MimeTypesUtil.getMimeTypeFromFilename(name);
  20. part.setDataHandler(new DataHandler(source));
  21. part.setHeader("Content-Type", mimeType);
  22. part.setFileName(name);
  23. part.setContentID("<" + name + ">");
  24. part.setDisposition("inline");
  25. return part;
  26. }

代码示例来源:origin: com.charlyghislain.dispatcher/dispatcher

  1. private BodyPart createEmbeddedResourcePart(ReferencedResource referencedResource) throws DispatcherException {
  2. String resourceId = referencedResource.getId();
  3. String mimeType = referencedResource.getMimeType();
  4. InputStream resourceStream = messageResourcesService.streamReferencedResource(referencedResource);
  5. try {
  6. DataSource dataSource = new ByteArrayDataSource(resourceStream, mimeType);
  7. DataHandler dataHandler = new DataHandler(dataSource);
  8. MimeBodyPart bodyPart = new MimeBodyPart();
  9. bodyPart.setDataHandler(dataHandler);
  10. bodyPart.setContentID("<" + resourceId + ">");
  11. bodyPart.setDisposition(Part.INLINE);
  12. return bodyPart;
  13. } catch (MessagingException | IOException e) {
  14. throw new DispatcherException("Error while creating embedded resource body part for resource id " + resourceId, e);
  15. }
  16. }

代码示例来源:origin: org.jodd/jodd-mail

  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: org.jboss.ws.native/jbossws-native-core

  1. public void encodeMultipartRelatedMessage() throws SOAPException, MessagingException
  2. {
  3. ParameterList p = new ParameterList();
  4. p.set("type", MimeConstants.TYPE_TEXT_XML);
  5. p.set("start", MimeConstants.ROOTPART_CID);
  6. MimeMultipart multipart = new MimeMultipart("related" + p);
  7. MimeBodyPart rootPart = new MimeBodyPart();
  8. /*
  9. * TODO - For now we build the root part content from a serialized string of the
  10. * DOM tree, in the future, this should utilize a DataHandler, and a DataContentHandler
  11. * to marshall the message. In this way the root part can be lazily written to the output
  12. * stream.
  13. */
  14. SOAPEnvelope soapEnv = soapMessage.getSOAPPart().getEnvelope();
  15. String envStr = SOAPElementWriter.writeElement(soapEnv, false);
  16. rootPart.setText(envStr, "UTF-8");
  17. rootPart.setContentID(MimeConstants.ROOTPART_CID);
  18. rootPart.setHeader(MimeConstants.CONTENT_TYPE, MimeConstants.TYPE_XML_UTF8);
  19. rootPart.setHeader(MimeConstants.CONTENT_TRANSFER_ENCODING, MimeConstants.TEXT_8BIT_ENCODING);
  20. multipart.addBodyPart(rootPart);
  21. addAttachmentParts(multipart);
  22. this.multipart = multipart;
  23. }
  24. }

代码示例来源:origin: com.threewks.thundr/thundr

  1. private void addAttachments(Multipart multipart, List<Attachment> attachments) throws MessagingException {
  2. for (Attachment attachment : attachments) {
  3. InMemoryResponse renderedResult = render(attachment.view());
  4. byte[] base64Encoded = Base64.encodeToByte(renderedResult.getBodyAsBytes());
  5. InternetHeaders headers = new InternetHeaders();
  6. headers.addHeader(Header.ContentType, renderedResult.getContentTypeString());
  7. headers.addHeader(Header.ContentTransferEncoding, "base64");
  8. MimeBodyPart part = new MimeBodyPart(headers, base64Encoded);
  9. part.setFileName(attachment.name());
  10. part.setDisposition(attachment.disposition().value());
  11. if (attachment.isInline()) {
  12. part.setContentID(attachment.contentId());
  13. }
  14. multipart.addBodyPart(part);
  15. }
  16. }

代码示例来源:origin: 3wks/thundr

  1. private void addAttachments(Multipart multipart, List<Attachment> attachments) throws MessagingException {
  2. for (Attachment attachment : attachments) {
  3. InMemoryResponse renderedResult = render(attachment.view());
  4. byte[] base64Encoded = Base64.encodeToByte(renderedResult.getBodyAsBytes());
  5. InternetHeaders headers = new InternetHeaders();
  6. headers.addHeader(Header.ContentType, renderedResult.getContentTypeString());
  7. headers.addHeader(Header.ContentTransferEncoding, "base64");
  8. MimeBodyPart part = new MimeBodyPart(headers, base64Encoded);
  9. part.setFileName(attachment.name());
  10. part.setDisposition(attachment.disposition().value());
  11. if (attachment.isInline()) {
  12. part.setContentID(attachment.contentId());
  13. }
  14. multipart.addBodyPart(part);
  15. }
  16. }

代码示例来源:origin: fr.sii.ogham/ogham-email-javamail

  1. /**
  2. * Add an attachment on the mime message.
  3. *
  4. * @param multipart
  5. * the mime message to fill
  6. * @param attachment
  7. * the attachment to add
  8. * @throws AttachmentResourceHandlerException
  9. * when the attachment couldn't be attached
  10. */
  11. private void addAttachment(Multipart multipart, Attachment attachment) throws AttachmentResourceHandlerException {
  12. MimeBodyPart part = new MimeBodyPart();
  13. try {
  14. part.setFileName(attachment.getResource().getName());
  15. part.setDisposition(attachment.getDisposition());
  16. part.setDescription(attachment.getDescription());
  17. part.setContentID(attachment.getContentId());
  18. attachmentHandler.setData(part, attachment.getResource(), attachment);
  19. multipart.addBodyPart(part);
  20. } catch (MessagingException e) {
  21. throw new AttachmentResourceHandlerException("Failed to attach " + attachment.getResource().getName(), attachment, e);
  22. }
  23. }

相关文章