本文整理了Java中javax.mail.internet.MimeBodyPart.setContentID()
方法的一些代码示例,展示了MimeBodyPart.setContentID()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MimeBodyPart.setContentID()
方法的具体详情如下:
包路径:javax.mail.internet.MimeBodyPart
类名称: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
/**
* Creates attachment body part. Handles regular and inline attachments.
*
* @param attachment Body part {@link EmailAttachment}.
* @return {@link MimeBodyPart} which represents body part attachment.
* @throws MessagingException if there is a failure.
*/
protected MimeBodyPart createAttachmentBodyPart(final EmailAttachment<? extends DataSource> attachment) throws MessagingException {
final MimeBodyPart part = new MimeBodyPart();
final String attachmentName = attachment.getEncodedName();
if (attachmentName != null) {
part.setFileName(attachmentName);
}
part.setDataHandler(new DataHandler(attachment.getDataSource()));
if (attachment.getContentId() != null) {
part.setContentID(StringPool.LEFT_CHEV + attachment.getContentId() + StringPool.RIGHT_CHEV);
}
if (attachment.isInline()) {
part.setDisposition(INLINE);
}
return part;
}
代码示例来源:origin: kiegroup/jbpm
String fileName = new File(attachmentUrl.getFile()).getName();
attachementBodyPart.setFileName(fileName);
attachementBodyPart.setContentID("<"+fileName+">");
代码示例来源:origin: stackoverflow.com
MimeBodyPart imagePart = new MimeBodyPart();
imagePart.attachFile("resources/teapot.jpg");
imagePart.setContentID("<" + cid + ">");
imagePart.setDisposition(MimeBodyPart.INLINE);
content.addBodyPart(imagePart);
代码示例来源:origin: stackoverflow.com
MimeMultipart rootContainer = new MimeMultipart();
rootContainer.setSubType("related");
rootContainer.addBodyPart(alternativeMultiPartWithPlainTextAndHtml); // not in focus here
rootContainer.addBodyPart(createInlineImagePart(base64EncodedImageContentByteArray));
...
message.setContent(rootContainer);
message.setHeader("MIME-Version", "1.0");
message.setHeader("Content-Type", rootContainer.getContentType());
...
BodyPart createInlineImagePart(byte[] base64EncodedImageContentByteArray) throws MessagingException {
InternetHeaders headers = new InternetHeaders();
headers.addHeader("Content-Type", "image/jpeg");
headers.addHeader("Content-Transfer-Encoding", "base64");
MimeBodyPart imagePart = new MimeBodyPart(headers, base64EncodedImageContentByteArray);
imagePart.setDisposition(MimeBodyPart.INLINE);
imagePart.setContentID("<image>");
imagePart.setFileName("image.jpg");
return imagePart;
代码示例来源:origin: camunda/camunda-bpm-platform
mbp.setFileName(name);
mbp.setDisposition(EmailAttachment.INLINE);
mbp.setContentID("<" + encodedCid + ">");
代码示例来源:origin: stackoverflow.com
// first part (the html)
MimeBodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<H1>This is the image: </H1><img src=\"cid:image\">";
messageBodyPart.setText(htmlText, null, "html");
multipart.addBodyPart(messageBodyPart);
// second part (the image)
messageBodyPart = new MimeBodyPart();
String filePath = "C:/image.png";
messageBodyPart.attachFile(filePath, "image/png", "base64");
messageBodyPart.setContentID("<image>");
// add image to the multipart
multipart.addBodyPart(messageBodyPart);
代码示例来源:origin: stackoverflow.com
Multipart multipart = new MimeMultipart("related");
MimeBodyPart htmlPart = new MimeBodyPart();
// messageBody contains html that references image
// using something like <img src="cid:XXX"> where
// "XXX" is an identifier that you make up to refer
// to the image
htmlPart.setText(messageBody, "utf-8", "html");
multipart.addBodyPart(htmlPart);
MimeBodyPart imgPart = new MimeBodyPart();
// imageFile is the file containing the image
imgPart.attachFile(imageFile);
// or, if the image is in a byte array in memory, use
// imgPart.setDataHandler(new DataHandler(
// new ByteArrayDataSource(bytes, "image/whatever")));
// "XXX" below matches "XXX" above in html code
imgPart.setContentID("<XXX>");
multipart.addBodyPart(imgPart);
message.setContent(multipart);
代码示例来源:origin: CloudSlang/cs-actions
private MimeBodyPart getImageMimeBodyPart(Map<String, String> base64ImagesMap, String contentId)
throws MessagingException {
MimeBodyPart imagePart = new MimeBodyPart();
imagePart.setContentID(contentId);
imagePart.setHeader("Content-Transfer-Encoding", "base64");
imagePart.setDataHandler(new DataHandler(Base64.decode(base64ImagesMap.get(contentId)), "image/png;"));
return imagePart;
}
代码示例来源:origin: hf-hf/mail-micro-service
/**
* 追加内嵌图片
* @author hf-hf
* @date 2018/12/27 16:53
* @param images
* @param multipart
* @throws MessagingException
*/
private void addImages(File[] images, MimeMultipart multipart) throws MessagingException {
if (null != images && images.length > 0) {
for (int i = 0; i < images.length; i++) {
MimeBodyPart imagePart = new MimeBodyPart();
DataHandler dataHandler = new DataHandler(new FileDataSource(images[i]));
imagePart.setDataHandler(dataHandler);
imagePart.setContentID(images[i].getName());
multipart.addBodyPart(imagePart);
}
}
}
代码示例来源:origin: stackoverflow.com
MimeBodyPart imgBodyPart = new MimeBodyPart();
imgBodyPart.attachFile("Image.png");
imgBodyPart.setContentID('<'+"i01@example.com"+'>');
imgBodyPart.setDisposition(MimeBodyPart.INLINE);
imgBodyPart.setHeader("Content-Type", "image/png");
multipart.addBodyPart(imgBodyPart);
代码示例来源:origin: stackoverflow.com
String cid = generateCID();
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText("<html><head>"
+ "<title>This is not usually displayed</title>"
+ "</head>n"
+ "<body><div><strong>Hi there!</strong></div>"
+ "<div>Sending HTML in email is so <em>cool!</em> </div>n"
+ "<div>And here's an image: <img src=\"cid:\"" + cid + " /></div>"
+ "<div>I hope you like it!</div></body></html>",
"US-ASCII", "html");
content.addBodyPart(textPart);
MimeBodyPart imagePart = new MimeBodyPart();
imagePart.attachFile("resources/teapot.jpg");
imagePart.setContentID("<" + cid + ">");
imagePart.setDisposition(MimeBodyPart.INLINE);
content.addBodyPart(imagePart);
代码示例来源:origin: stackoverflow.com
img.setContentID(ei.getFilename());
img.setFileName(ei.getFilename());
ByteArrayDataSource bads = new ByteArrayDataSource(ei.getImageData(), ei.getMimeType());
代码示例来源:origin: com.xpn.xwiki.platform.plugins/xwiki-plugin-mailsender
/**
* Add attachments to a multipart message
*
* @param multipart Multipart message
* @param attachments List of attachments
*/
public MimeBodyPart createAttachmentBodyPart(Attachment attachment, XWikiContext context) throws XWikiException,
IOException, MessagingException
{
String name = attachment.getFilename();
byte[] stream = attachment.getContent();
File temp = File.createTempFile("tmpfile", ".tmp");
FileOutputStream fos = new FileOutputStream(temp);
fos.write(stream);
fos.close();
DataSource source = new FileDataSource(temp);
MimeBodyPart part = new MimeBodyPart();
String mimeType = MimeTypesUtil.getMimeTypeFromFilename(name);
part.setDataHandler(new DataHandler(source));
part.setHeader("Content-Type", mimeType);
part.setFileName(name);
part.setContentID("<" + name + ">");
part.setDisposition("inline");
temp.deleteOnExit();
return part;
}
代码示例来源:origin: org.xwiki.platform/xwiki-platform-mailsender
/**
* Add attachments to a multipart message
*
* @param attachment the attachment to create the body part for.
* @param context the XWiki context.
* @return the body part for the given attachment.
*/
public MimeBodyPart createAttachmentBodyPart(Attachment attachment, XWikiContext context) throws XWikiException,
IOException, MessagingException
{
String name = attachment.getFilename();
byte[] stream = attachment.getContent();
File temp = new TemporaryFile(File.createTempFile("tmpfile", ".tmp"));
FileOutputStream fos = new FileOutputStream(temp);
fos.write(stream);
fos.close();
DataSource source = new FileDataSource(temp);
MimeBodyPart part = new MimeBodyPart();
String mimeType = MimeTypesUtil.getMimeTypeFromFilename(name);
part.setDataHandler(new DataHandler(source));
part.setHeader("Content-Type", mimeType);
part.setFileName(name);
part.setContentID("<" + name + ">");
part.setDisposition("inline");
return part;
}
代码示例来源:origin: com.charlyghislain.dispatcher/dispatcher
private BodyPart createEmbeddedResourcePart(ReferencedResource referencedResource) throws DispatcherException {
String resourceId = referencedResource.getId();
String mimeType = referencedResource.getMimeType();
InputStream resourceStream = messageResourcesService.streamReferencedResource(referencedResource);
try {
DataSource dataSource = new ByteArrayDataSource(resourceStream, mimeType);
DataHandler dataHandler = new DataHandler(dataSource);
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setDataHandler(dataHandler);
bodyPart.setContentID("<" + resourceId + ">");
bodyPart.setDisposition(Part.INLINE);
return bodyPart;
} catch (MessagingException | IOException e) {
throw new DispatcherException("Error while creating embedded resource body part for resource id " + resourceId, e);
}
}
代码示例来源:origin: org.jodd/jodd-mail
/**
* Creates attachment body part. Handles regular and inline attachments.
*
* @param attachment Body part {@link EmailAttachment}.
* @return {@link MimeBodyPart} which represents body part attachment.
* @throws MessagingException if there is a failure.
*/
protected MimeBodyPart createAttachmentBodyPart(final EmailAttachment<? extends DataSource> attachment) throws MessagingException {
final MimeBodyPart part = new MimeBodyPart();
final String attachmentName = attachment.getEncodedName();
if (attachmentName != null) {
part.setFileName(attachmentName);
}
part.setDataHandler(new DataHandler(attachment.getDataSource()));
if (attachment.getContentId() != null) {
part.setContentID(StringPool.LEFT_CHEV + attachment.getContentId() + StringPool.RIGHT_CHEV);
}
if (attachment.isInline()) {
part.setDisposition(INLINE);
}
return part;
}
代码示例来源:origin: org.jboss.ws.native/jbossws-native-core
public void encodeMultipartRelatedMessage() throws SOAPException, MessagingException
{
ParameterList p = new ParameterList();
p.set("type", MimeConstants.TYPE_TEXT_XML);
p.set("start", MimeConstants.ROOTPART_CID);
MimeMultipart multipart = new MimeMultipart("related" + p);
MimeBodyPart rootPart = new MimeBodyPart();
/*
* TODO - For now we build the root part content from a serialized string of the
* DOM tree, in the future, this should utilize a DataHandler, and a DataContentHandler
* to marshall the message. In this way the root part can be lazily written to the output
* stream.
*/
SOAPEnvelope soapEnv = soapMessage.getSOAPPart().getEnvelope();
String envStr = SOAPElementWriter.writeElement(soapEnv, false);
rootPart.setText(envStr, "UTF-8");
rootPart.setContentID(MimeConstants.ROOTPART_CID);
rootPart.setHeader(MimeConstants.CONTENT_TYPE, MimeConstants.TYPE_XML_UTF8);
rootPart.setHeader(MimeConstants.CONTENT_TRANSFER_ENCODING, MimeConstants.TEXT_8BIT_ENCODING);
multipart.addBodyPart(rootPart);
addAttachmentParts(multipart);
this.multipart = multipart;
}
}
代码示例来源:origin: com.threewks.thundr/thundr
private void addAttachments(Multipart multipart, List<Attachment> attachments) throws MessagingException {
for (Attachment attachment : attachments) {
InMemoryResponse renderedResult = render(attachment.view());
byte[] base64Encoded = Base64.encodeToByte(renderedResult.getBodyAsBytes());
InternetHeaders headers = new InternetHeaders();
headers.addHeader(Header.ContentType, renderedResult.getContentTypeString());
headers.addHeader(Header.ContentTransferEncoding, "base64");
MimeBodyPart part = new MimeBodyPart(headers, base64Encoded);
part.setFileName(attachment.name());
part.setDisposition(attachment.disposition().value());
if (attachment.isInline()) {
part.setContentID(attachment.contentId());
}
multipart.addBodyPart(part);
}
}
代码示例来源:origin: 3wks/thundr
private void addAttachments(Multipart multipart, List<Attachment> attachments) throws MessagingException {
for (Attachment attachment : attachments) {
InMemoryResponse renderedResult = render(attachment.view());
byte[] base64Encoded = Base64.encodeToByte(renderedResult.getBodyAsBytes());
InternetHeaders headers = new InternetHeaders();
headers.addHeader(Header.ContentType, renderedResult.getContentTypeString());
headers.addHeader(Header.ContentTransferEncoding, "base64");
MimeBodyPart part = new MimeBodyPart(headers, base64Encoded);
part.setFileName(attachment.name());
part.setDisposition(attachment.disposition().value());
if (attachment.isInline()) {
part.setContentID(attachment.contentId());
}
multipart.addBodyPart(part);
}
}
代码示例来源:origin: fr.sii.ogham/ogham-email-javamail
/**
* Add an attachment on the mime message.
*
* @param multipart
* the mime message to fill
* @param attachment
* the attachment to add
* @throws AttachmentResourceHandlerException
* when the attachment couldn't be attached
*/
private void addAttachment(Multipart multipart, Attachment attachment) throws AttachmentResourceHandlerException {
MimeBodyPart part = new MimeBodyPart();
try {
part.setFileName(attachment.getResource().getName());
part.setDisposition(attachment.getDisposition());
part.setDescription(attachment.getDescription());
part.setContentID(attachment.getContentId());
attachmentHandler.setData(part, attachment.getResource(), attachment);
multipart.addBodyPart(part);
} catch (MessagingException e) {
throw new AttachmentResourceHandlerException("Failed to attach " + attachment.getResource().getName(), attachment, e);
}
}
内容来源于网络,如有侵权,请联系作者删除!