/** * 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()); }
/** * 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 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(); } }
@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); }
/** * 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()); }
/** * 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); }