/** * Uses {@code null} contentType. * * @see #content(byte[], String) */ public EmailAttachmentBuilder content(final byte[] bytes) { return content(bytes, null); }
@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); }
/** * @param fileName String representing file name. * @return this * @see #content(File) */ public EmailAttachmentBuilder content(final String fileName) { return content(new File(fileName)); }
@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(); } }
/** * Creates new {@link FileDataSource} and then calls {@link #content(DataSource)} * * @param file {@link File} * @return this * @see #content(DataSource) */ public EmailAttachmentBuilder content(final File file) { return content(new FileDataSource(file)); }
/** * 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))); }
/** * 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))); }
/** * 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); }
/** * 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()); }
@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 testHtmlAndOneAttachment() throws MessagingException, IOException { Email email = Email.create() .from("inf0@jodd.org") .to("ig0r@gmail.com") .subject("test6") .textMessage("Hello!") .attachment(EmailAttachment.with().content(BYTES_11_15, APPLICATION_ZIP)); Message message = createMessage(email); // wrapper final MimeMultipart multipart = (MimeMultipart) message.getContent(); assertEquals(2, multipart.getCount()); // inner content #1 MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(0); final MimeMultipart mimeMultipart = (MimeMultipart) mimeBodyPart.getContent(); assertEquals(1, mimeMultipart.getCount()); MimeBodyPart bodyPart = (MimeBodyPart) mimeMultipart.getBodyPart(0); assertEquals("Hello!", bodyPart.getContent()); }
.name("a.jpg") .setContentIdFromNameIfMissing() .content(new byte[]{'X', 'Z', 'X'}));
.to(GREEN_MAIL_COM) .textMessage("Hello " + count) .attachment(EmailAttachment.with().content(new byte[]{(byte) count}));
/** * Uses {@code null} contentType. * * @see #content(byte[], String) */ public EmailAttachmentBuilder content(final byte[] bytes) { return content(bytes, null); }
/** * Creates new {@link FileDataSource} and then calls {@link #content(DataSource)} * * @param file {@link File} * @return this * @see #content(DataSource) */ public EmailAttachmentBuilder content(final File file) { return content(new FileDataSource(file)); }
/** * @param fileName String representing file name. * @return this * @see #content(File) */ public EmailAttachmentBuilder content(final String fileName) { return content(new File(fileName)); }
/** * 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))); }
/** * 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))); }
/** * 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); }