/** * Sets the {@link DataSource}. Common {@link DataSource}s include {@link ByteArrayDataSource} and * {@link FileDataSource}. * * @param dataSource {@link DataSource} * @return this */ public <T extends DataSource> EmailAttachmentBuilder content(final T dataSource) { this.dataSource = dataSource; name(dataSource.getName()); return this; }
/** * Creates {@link EmailAttachmentBuilder} from {@link Part} and sets Content ID, inline and name. * * @param part {@link Part}. * @return this * @see #attachment(EmailAttachment) */ private static EmailAttachmentBuilder addAttachmentInfo(final Part part) throws MessagingException { final String fileName = EmailUtil.resolveFileName(part); final String contentId = parseContentId(part); final boolean isInline = parseInline(part); return new EmailAttachmentBuilder() .name(fileName) .contentId(contentId) .inline(isInline); }
@Test void testTextHtmlEmbedAttach1() throws MessagingException, IOException { final Email email = Email.create() .from(FROM_EXAMPLE_COM) .to(TO_EXAMPLE_COM) .subject(SUB) .textMessage(HELLO) .htmlMessage("<html><body><h1>Hey!</h1><img src='cid:c.png'></body></html>") .embeddedAttachment(EmailAttachment.with().name(C_PNG).content(BYTES_1_7)) .attachment(EmailAttachment.with().name(FILE_ZIP).content(BYTES_11_15)); assertEmail(email); }
@Test void testTextHtmlEmbedAttach2() throws MessagingException, IOException { final Email email = new Email(); email.from(FROM_EXAMPLE_COM); email.to(TO_EXAMPLE_COM); email.subject(SUB); final EmailMessage testMessage = new EmailMessage(HELLO, MimeTypes.MIME_TEXT_PLAIN); email.message(testMessage); final EmailMessage htmlMessage = new EmailMessage( "<html><body><h1>Hey!</h1><img src='cid:c.png'></body></html>", MimeTypes.MIME_TEXT_HTML); email.message(htmlMessage); final EmailAttachment<ByteArrayDataSource> embeddedAttachment = EmailAttachment.with() .content(BYTES_1_7, IMAGE_PNG) .name(C_PNG) .contentId(C_PNG) .inline(true) .buildByteArrayDataSource(); embeddedAttachment.setEmbeddedMessage(htmlMessage); email.embeddedAttachment(embeddedAttachment); final EmailAttachmentBuilder attachmentBuilder = EmailAttachment.with() .content(BYTES_11_15, APPLICATION_ZIP) .name(FILE_ZIP) .contentId(FILE_ZIP); email.attachment(attachmentBuilder); assertEmail(email); }
.attachment(EmailAttachment .with() .name("a.jpg") .setContentIdFromNameIfMissing() .content(new byte[]{'X', 'Z', 'X'}));
/** * Sets the {@link DataSource}. Common {@link DataSource}s include {@link ByteArrayDataSource} and * {@link FileDataSource}. * * @param dataSource {@link DataSource} * @return this */ public <T extends DataSource> EmailAttachmentBuilder content(final T dataSource) { this.dataSource = dataSource; name(dataSource.getName()); return this; }
/** * Creates {@link EmailAttachmentBuilder} from {@link Part} and sets Content ID, inline and name. * * @param part {@link Part}. * @return this * @see #attachment(EmailAttachment) */ private static EmailAttachmentBuilder addAttachmentInfo(final Part part) throws MessagingException { final String fileName = EmailUtil.resolveFileName(part); final String contentId = parseContentId(part); final boolean isInline = parseInline(part); return new EmailAttachmentBuilder() .name(fileName) .contentId(contentId) .inline(isInline); }