/** * 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" -> "<myId>". * 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); }
/** * Add an attachment 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. * @param attachmentFilename the name of the attachment as it will * appear in the mail (the content type will be determined by this) * @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 #addAttachment(String, org.springframework.core.io.InputStreamSource) * @see #addAttachment(String, java.io.File) */ public void addAttachment(String attachmentFilename, DataSource dataSource) throws MessagingException { Assert.notNull(attachmentFilename, "Attachment filename must not be null"); Assert.notNull(dataSource, "DataSource must not be null"); try { MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setDisposition(MimeBodyPart.ATTACHMENT); mimeBodyPart.setFileName(MimeUtility.encodeText(attachmentFilename)); mimeBodyPart.setDataHandler(new DataHandler(dataSource)); getRootMimeMultipart().addBodyPart(mimeBodyPart); } catch (UnsupportedEncodingException ex) { throw new MessagingException("Failed to encode attachment filename", ex); } }
/** * 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" -> "<myId>". * 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); }
/** * Add an attachment 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. * @param attachmentFilename the name of the attachment as it will * appear in the mail (the content type will be determined by this) * @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 #addAttachment(String, org.springframework.core.io.InputStreamSource) * @see #addAttachment(String, java.io.File) */ public void addAttachment(String attachmentFilename, DataSource dataSource) throws MessagingException { Assert.notNull(attachmentFilename, "Attachment filename must not be null"); Assert.notNull(dataSource, "DataSource must not be null"); try { MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setDisposition(MimeBodyPart.ATTACHMENT); mimeBodyPart.setFileName(MimeUtility.encodeText(attachmentFilename)); mimeBodyPart.setDataHandler(new DataHandler(dataSource)); getRootMimeMultipart().addBodyPart(mimeBodyPart); } catch (UnsupportedEncodingException ex) { throw new MessagingException("Failed to encode attachment filename", ex); } }
text.setDisposition(Part.INLINE); content.addBodyPart(text); html.setDisposition(Part.INLINE); html.setHeader("Content-Transfer-Encoding", "8bit"); content.addBodyPart(html); MimeBodyPart bPart = new MimeBodyPart(); bPart.setText(textBody, encoding); bPart.setDisposition(Part.INLINE); bPart.setHeader("Content-Transfer-Encoding", "8bit"); MimeMultipart mPart = new MimeMultipart(); MimeBodyPart bPart = new MimeBodyPart(); bPart.setContent(htmlBody, "text/html; charset=UTF-8"); bPart.setDisposition(Part.INLINE); bPart.setHeader("Content-Transfer-Encoding", "8bit"); MimeMultipart mPart = new MimeMultipart();
/** * 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; }
/** * 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. * * @exception IllegalWriteException if the underlying * implementation does not support modification * @exception IllegalStateException if this message is * obtained from a READ_ONLY folder. * @exception MessagingException for other failures */ @Override public void setDisposition(String disposition) throws MessagingException { MimeBodyPart.setDisposition(this, disposition); }
/** * 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. * * @exception IllegalWriteException if the underlying * implementation does not support modification * @exception IllegalStateException if this body part is * obtained from a READ_ONLY folder. * @exception MessagingException for other failures */ public void setDisposition(String disposition) throws MessagingException { setDisposition(this, disposition); }
/** * 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. * * @exception IllegalWriteException if the underlying * implementation does not support modification * @exception IllegalStateException if this message is * obtained from a READ_ONLY folder. * @exception MessagingException for other failures */ public void setDisposition(String disposition) throws MessagingException { MimeBodyPart.setDisposition(this, disposition); }
/** * 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. * * @exception IllegalWriteException if the underlying * implementation does not support modification * @exception IllegalStateException if this body part is * obtained from a READ_ONLY folder. * @exception MessagingException for other failures */ @Override public void setDisposition(String disposition) throws MessagingException { setDisposition(this, disposition); }
MimeBodyPart imagePart = new MimeBodyPart(); imagePart.attachFile("resources/teapot.jpg"); imagePart.setContentID("<" + cid + ">"); imagePart.setDisposition(MimeBodyPart.INLINE); content.addBodyPart(imagePart);
/** * Creates the attachment part. * * @param attachment the attachment * @return the mime body part * @throws MessagingException the messaging exception */ private static MimeBodyPart createAttachmentPart(DataSource attachment) throws MessagingException { MimeBodyPart attachmentPart = new MimeBodyPart(); attachmentPart.setDataHandler(new DataHandler(attachment)); attachmentPart.setDisposition(Part.ATTACHMENT); attachmentPart.setFileName(attachment.getName()); return attachmentPart; }
/** * Factory to create the attachment body part. * @param index the attachment index. * @return a body part with default headers set. * @throws MessagingException if there is a problem. * @throws IndexOutOfBoundsException if the given index is not an valid * attachment index. */ private MimeBodyPart createBodyPart(int index) throws MessagingException { assert Thread.holdsLock(this); final MimeBodyPart part = new MimeBodyPart(); part.setDisposition(Part.ATTACHMENT); part.setDescription(descriptionFrom( attachmentFormatters[index], attachmentFilters[index], attachmentNames[index])); setAcceptLang(part); return part; }
/** * 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); }
/** * Factory to create the attachment body part. * @param index the attachment index. * @return a body part with default headers set. * @throws MessagingException if there is a problem. * @throws IndexOutOfBoundsException if the given index is not an valid * attachment index. */ private MimeBodyPart createBodyPart(int index) throws MessagingException { assert Thread.holdsLock(this); final MimeBodyPart part = new MimeBodyPart(); part.setDisposition(Part.ATTACHMENT); part.setDescription(descriptionFrom( attachmentFormatters[index], attachmentFilters[index], attachmentNames[index])); setAcceptLang(part); return part; }
/** * 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); }
/** * Factory to create the in-line body part. * @return a body part with default headers set. * @throws MessagingException if there is a problem. */ private MimeBodyPart createBodyPart() throws MessagingException { assert Thread.holdsLock(this); final MimeBodyPart part = new MimeBodyPart(); part.setDisposition(Part.INLINE); part.setDescription(descriptionFrom(getFormatter(), getFilter(), subjectFormatter)); setAcceptLang(part); return part; }
/** * Factory to create the in-line body part. * @return a body part with default headers set. * @throws MessagingException if there is a problem. */ private MimeBodyPart createBodyPart() throws MessagingException { assert Thread.holdsLock(this); final MimeBodyPart part = new MimeBodyPart(); part.setDisposition(Part.INLINE); part.setDescription(descriptionFrom(getFormatter(), getFilter(), subjectFormatter)); setAcceptLang(part); return part; }
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;
private void renderInlineResource(Multipart multipart, InlineResource inlineResource, Set<String> fileNames) throws MessagingException { assertNotNull(resourceLoader, "no resourceLoader"); String resourceName = inlineResource.getResourceName(); Resource resource = resourceLoader.getResource(resourceName); if (!resource.exists()) { throw new MailBuilderException("Could not find resource \"" + resourceName + "\""); } DataSource ds; try { ds = new URLDataSource(resource.getURL()); } catch (IOException e) { ds = new ResourceDataSource(resource); } MimeBodyPart bodyPart = new MimeBodyPart(); bodyPart.setDataHandler(new DataHandler(ds)); bodyPart.setHeader(CONTENT_ID, "<" + inlineResource.getContentId() + ">"); bodyPart.setFileName(inlineResource.getUniqueFilename(fileNames)); bodyPart.setDisposition("inline"); multipart.addBodyPart(bodyPart); }