/** * 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; }
/** * 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); } }
/** * Filters out the {@link List} of embedded {@link EmailAttachment}s for given {@link EmailMessage}. * This will remove the embedded attachments from the {@link List} and return them in a new {@link List}. * * @param attachments {@link List} of attachments to search for in emailMessage. * @param emailMessage {@link EmailMessage} to see if attachment is embedded into. * @return {@link List} of embedded {@link EmailAttachment}s; otherwise, returns empty {@link List}. */ protected List<EmailAttachment<? extends DataSource>> filterEmbeddedAttachments(final List<EmailAttachment<? extends DataSource>> attachments, final EmailMessage emailMessage) { final List<EmailAttachment<? extends DataSource>> embeddedAttachments = new ArrayList<>(); if (attachments == null || attachments.isEmpty() || emailMessage == null) { return embeddedAttachments; } final Iterator<EmailAttachment<? extends DataSource>> iterator = attachments.iterator(); while (iterator.hasNext()) { final EmailAttachment<? extends DataSource> emailAttachment = iterator.next(); if (emailAttachment.isEmbeddedInto(emailMessage)) { embeddedAttachments.add(emailAttachment); iterator.remove(); } } return embeddedAttachments; }
@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); }
/** * Embed {@link EmailAttachment} to last message. No header is changed. * * @param attachment {@link EmailAttachment} * @return this * @see #storeAttachment(EmailAttachment) */ public T embeddedAttachment(final EmailAttachment<? extends DataSource> attachment) { storeAttachment(attachment); final List<EmailMessage> messages = messages(); final int size = messages.size(); if (size > 1) { // Add to last message final int lastMessagePos = size - 1; final EmailMessage lastMessage = messages.get(lastMessagePos); attachment.setEmbeddedMessage(lastMessage); } return _this(); }
private void checkInline(final boolean isInline, final EmailAttachment<? extends DataSource> attachment) { assertEquals(isInline, attachment.isInline()); } }
/** * Saves attachment to a file. * * @param destination The destination file to be written. */ public void writeToFile(final File destination) { InputStream input = null; final OutputStream output; try { input = getDataSource().getInputStream(); output = new FileOutputStream(destination); StreamUtil.copy(input, output); } catch (final IOException ioex) { throw new MailException(ioex); } finally { StreamUtil.close(input); } }
private void checkAttachments(final List<EmailAttachment<? extends DataSource>> sentAttachments, final List<EmailAttachment<? extends DataSource>> receivedAttachments) { checkSize(sentAttachments); checkSize(receivedAttachments); String name = ZERO; byte[] data = BYTES_7_8_9; checkAttachmentInfo(sentAttachments, 0, receivedAttachments, 2, name, data, false, false); name = ONE; data = BYTES_4_5_6; checkAttachmentInfo(sentAttachments, 1, receivedAttachments, 3, name, data, false, false); name = TWO; data = BYTES_1_2_3; checkAttachmentInfo(sentAttachments, 2, receivedAttachments, 4, name, data, false, false); //assertEquals(CID_1, sentAttachments.get(ndx).getContentId()); //assertEquals("<CID1>", receivedAttachments.get(ndx).getContentId()); // These are null because used storeAttachment method instead of embedAttachment method. assertNull(sentAttachments.get(2).getContentId()); assertNull(receivedAttachments.get(4).getContentId()); /**/ name = null; data = BYTES_0_1_0; checkAttachmentInfo(sentAttachments, 3, receivedAttachments, 0, name, data, true, true); name = THREE; data = BYTES_10_11_12; checkAttachmentInfo(sentAttachments, 4, receivedAttachments, 1, name, data, true, true); }
/** * Adds {@link EmailAttachment}. Content ID will be set to {@code null}. * * @param attachment {@link EmailAttachment} to add. * @return this */ public T attachment(final EmailAttachment<? extends DataSource> attachment) { attachment.setContentId(null); return storeAttachment(attachment); }
/** * Returns byte content of the attachment. * * @return byte array with content of the attachment. */ public byte[] toByteArray() { final FastByteArrayOutputStream out; if (size != -1) { out = new FastByteArrayOutputStream(size); } else { out = new FastByteArrayOutputStream(); } writeToStream(out); return out.toByteArray(); }
/** * 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); }
/** * Embed {@link EmailAttachment} to last message. No header is changed. * * @param attachment {@link EmailAttachment} * @return this * @see #storeAttachment(EmailAttachment) */ public T embeddedAttachment(final EmailAttachment<? extends DataSource> attachment) { storeAttachment(attachment); final List<EmailMessage> messages = messages(); final int size = messages.size(); if (size > 1) { // Add to last message final int lastMessagePos = size - 1; final EmailMessage lastMessage = messages.get(lastMessagePos); attachment.setEmbeddedMessage(lastMessage); } return _this(); }
/** * Saves attachment to the output stream. * * @param out OutputStream where attachment should be copied to. */ public void writeToStream(final OutputStream out) { InputStream input = null; try { input = getDataSource().getInputStream(); StreamUtil.copy(input, out); } catch (final IOException ioex) { throw new MailException(ioex); } finally { StreamUtil.close(input); } }
/** * Adds {@link EmailAttachment}. Content ID will be set to {@code null}. * * @param attachment {@link EmailAttachment} to add. * @return this */ public T attachment(final EmailAttachment<? extends DataSource> attachment) { attachment.setContentId(null); return storeAttachment(attachment); }
/** * Returns byte content of the attachment. * * @return byte array with content of the attachment. */ public byte[] toByteArray() { final FastByteArrayOutputStream out; if (size != -1) { out = new FastByteArrayOutputStream(size); } else { out = new FastByteArrayOutputStream(); } writeToStream(out); return out.toByteArray(); }
/** * 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); }
/** * 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; }
/** * Creates {@link EmailAttachment}. * * @return {@link EmailAttachment}. * @throws MailException if issue with {@link DataSource}. */ public EmailAttachment<ByteArrayDataSource> buildByteArrayDataSource() throws MailException { try { final ByteArrayDataSource bads; if (dataSource instanceof ByteArrayDataSource) { bads = (ByteArrayDataSource) dataSource; } else { bads = new ByteArrayDataSource(dataSource.getInputStream(), dataSource.getContentType()); } checkDataSource(); return new EmailAttachment<>(name, contentId, isInline, bads).setEmbeddedMessage(targetMessage); } catch (final IOException ioexc) { throw new MailException(ioexc); } }
/** * Saves attachment to a file. * * @param destination The destination file to be written. */ public void writeToFile(final File destination) { InputStream input = null; final OutputStream output; try { input = getDataSource().getInputStream(); output = new FileOutputStream(destination); StreamUtil.copy(input, output); } catch (final IOException ioex) { throw new MailException(ioex); } finally { StreamUtil.close(input); } }
/** * Filters out the {@link List} of embedded {@link EmailAttachment}s for given {@link EmailMessage}. * This will remove the embedded attachments from the {@link List} and return them in a new {@link List}. * * @param attachments {@link List} of attachments to search for in emailMessage. * @param emailMessage {@link EmailMessage} to see if attachment is embedded into. * @return {@link List} of embedded {@link EmailAttachment}s; otherwise, returns empty {@link List}. */ protected List<EmailAttachment<? extends DataSource>> filterEmbeddedAttachments(final List<EmailAttachment<? extends DataSource>> attachments, final EmailMessage emailMessage) { final List<EmailAttachment<? extends DataSource>> embeddedAttachments = new ArrayList<>(); if (attachments == null || attachments.isEmpty() || emailMessage == null) { return embeddedAttachments; } final Iterator<EmailAttachment<? extends DataSource>> iterator = attachments.iterator(); while (iterator.hasNext()) { final EmailAttachment<? extends DataSource> emailAttachment = iterator.next(); if (emailAttachment.isEmbeddedInto(emailMessage)) { embeddedAttachments.add(emailAttachment); iterator.remove(); } } return embeddedAttachments; }