/** * Attaches the embedded attachment: Content ID will be set if missing from attachment's file name. * * @param builder {@link EmailAttachmentBuilder} * @return this * @see #embeddedAttachment(EmailAttachment) */ public T embeddedAttachment(final EmailAttachmentBuilder builder) { builder.setContentIdFromNameIfMissing(); // https://github.com/oblac/jodd/issues/546 // https://github.com/oblac/jodd/issues/404#issuecomment-297011351 // content disposition will be set to "inline" builder.inline(true); return embeddedAttachment(builder.buildByteArrayDataSource()); }
/** * Adds received attachment. * * @param part {@link Part}. * @param content Content as {@link InputStream}. * @return this * @see #attachment(EmailAttachment) */ private ReceivedEmail addAttachment(final Part part, final InputStream content, final File attachmentStorage) throws MessagingException, IOException { final EmailAttachmentBuilder builder = addAttachmentInfo(part); builder.content(content, part.getContentType()); if (attachmentStorage != null) { String name = messageId + "-" + (this.attachments().size() + 1); return storeAttachment(builder.buildFileDataSource(name, attachmentStorage)); } return storeAttachment(builder.buildByteArrayDataSource()); }
/** * 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); }
/** * Creates new {@link ByteArrayDataSource} and then calls {@link #content(DataSource)}. * * @param bytes array of bytes * @param contentType content type from {@link EmailAttachment}. * @return this * @see #content(DataSource) */ public EmailAttachmentBuilder content(final byte[] bytes, final String contentType) { return content(new ByteArrayDataSource(bytes, resolveContentType(contentType))); }
@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); }
@Test void testSendEmailWithVariousAttachaments() { final SmtpServer smtpServer = MailServer.create() .host(HOST) .auth(USERNAME, PASSWORD) .ssl(true) .buildSmtpMailServer(); final SendMailSession session = smtpServer.createSession(); session.open(); final Email email = Email.create() .from("info@jodd.org") .to("igor.spasic@gmail.com") .subject("test-gmail") .textMessage("Hello!") .htmlMessage( "<html><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">" + "<body><h1>Hey!</h1><img src='cid:jodd.png'>" + "<h2>Hay!</h2><img src='cid:jodd2.png'>" + "<h3></h3></body></html>") .embeddedAttachment(EmailAttachment.with().content(new File(PNG)).inline(false)) .embeddedAttachment(EmailAttachment.with().content(new File(PNG)).contentId("jodd2.png").inline(true)) .attachment(EmailAttachment.with().content(PNG)); session.sendMail(email); session.close(); } }
.attachment(EmailAttachment .with() .name("a.jpg") .setContentIdFromNameIfMissing() .content(new byte[]{'X', 'Z', 'X'}));
/** * Uses {@code null} contentType. * * @see #content(byte[], String) */ public EmailAttachmentBuilder content(final byte[] bytes) { return content(bytes, null); }
/** * Adds received attachment. * * @param part {@link Part}. * @param content Content as byte array. * @return this * @see #attachment(EmailAttachment) */ private ReceivedEmail addAttachment(final Part part, final byte[] content) throws MessagingException { final EmailAttachmentBuilder builder = addAttachmentInfo(part); builder.content(content, part.getContentType()); final EmailAttachment<ByteArrayDataSource> attachment = builder.buildByteArrayDataSource(); attachment.setSize(content.length); return storeAttachment(attachment); }
@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); }
/** * @see #attachment(EmailAttachment) */ public T attachment(final EmailAttachmentBuilder builder) { return attachment(builder.buildByteArrayDataSource()); }
/** * 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 EmailAttachment}. * * @return {@link EmailAttachment}. * @throws MailException if issue with {@link DataSource}. */ public EmailAttachment<FileDataSource> buildFileDataSource(final String messageId, final File attachmentStorage) throws MailException { try { final FileDataSource fds; if (dataSource instanceof FileDataSource) { fds = (FileDataSource) dataSource; } else { final File file = new File(attachmentStorage, messageId); FileUtil.writeStream(file, dataSource.getInputStream()); fds = new FileDataSource(file); } checkDataSource(); return new EmailAttachment<>(name, contentId, isInline, fds).setEmbeddedMessage(targetMessage); } catch (final IOException ioexc) { throw new MailException(ioexc); } }
/** * Returns new/empty {@link EmailAttachmentBuilder}. * * @return {@link EmailAttachmentBuilder}. */ public static EmailAttachmentBuilder with() { return new EmailAttachmentBuilder(); }
/** * Creates new {@link ByteArrayDataSource} and then calls {@link #content(DataSource)}. * * @param inputStream {@link InputStream} * @param contentType content type from {@link EmailAttachment}. * @return this * @throws IOException if {@link ByteArrayDataSource} cannot be created from {@link InputStream} * @see #content(DataSource) */ public EmailAttachmentBuilder content(final InputStream inputStream, final String contentType) throws IOException { return content(new ByteArrayDataSource(inputStream, resolveContentType(contentType))); }
/** * @param fileName String representing file name. * @return this * @see #content(File) */ public EmailAttachmentBuilder content(final String fileName) { return content(new File(fileName)); }
/** * Adds received attachment. * * @param part {@link Part}. * @param content Content as byte array. * @return this * @see #attachment(EmailAttachment) */ private ReceivedEmail addAttachment(final Part part, final byte[] content) throws MessagingException { final EmailAttachmentBuilder builder = addAttachmentInfo(part); builder.content(content, part.getContentType()); final EmailAttachment<ByteArrayDataSource> attachment = builder.buildByteArrayDataSource(); attachment.setSize(content.length); return storeAttachment(attachment); }
/** * @see #attachment(EmailAttachment) */ public T attachment(final EmailAttachmentBuilder builder) { return attachment(builder.buildByteArrayDataSource()); }