@Override public Email clone() { return create() // from / reply-to .from(from()) .replyTo(replyTo()) // recipients .to(to()) .cc(cc()) .bcc(bcc()) // subject .subject(subject(), subjectEncoding()) // dates .sentDate(sentDate()) // headers - includes priority .headers(headers()) // content / attachments .storeAttachments(attachments()) .message(messages()); }
/** * Static constructor for fluent interface. */ public static Email create() { return new Email(); }
/** * Appends one or more BCC addresses. * * @param bccs vararg of {@link EmailAddress}es to set. * @return this */ public Email bcc(final EmailAddress... bccs) { this.bcc = ArraysUtil.join(this.bcc, valueOrEmptyArray(bccs)); return _this(); }
/** * Sets subject in msgToSet from subject in emailWithData. * * @param emailWithData {@link Email} with data * @param msgToSet {@link MimeMessage} to set data into. * @throws MessagingException if there is a failure */ private void setSubject(final Email emailWithData, final MimeMessage msgToSet) throws MessagingException { if (emailWithData.subjectEncoding() != null) { msgToSet.setSubject(emailWithData.subject(), emailWithData.subjectEncoding()); } else { msgToSet.setSubject(emailWithData.subject()); } }
@Test void testSimpleText() throws MessagingException, IOException { final Email email = Email.create() .from(FROM_EXAMPLE_COM) .to(TO_EXAMPLE_COM) .subject(SUB) .textMessage(HELLO); final Message message = createMessage(email); final String content = (String) message.getContent(); assertEquals(HELLO, content); assertTrue(message.getDataHandler().getContentType().contains("text/plain")); }
@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); }
@Test void testFromToBccCc() throws MessagingException { final Email email = Email.create() .from(FROM_EXAMPLE_COM) .to(TO1_EXAMPLE_COM).to("Major Tom", "to2@example.com") .cc(CC1_EXAMPLE_COM).cc("Major Carson", "cc2@example.com") .bcc("Major Ben", "bcc1@example.com").bcc(BCC2_EXAMPLE_COM); final Message message = createMessage(email); assertEquals(1, message.getFrom().length); assertEquals(FROM_EXAMPLE_COM, message.getFrom()[0].toString()); assertEquals(6, message.getAllRecipients().length); assertEquals(2, message.getRecipients(RecipientType.TO).length); assertEquals(TO1_EXAMPLE_COM, message.getRecipients(RecipientType.TO)[0].toString()); assertEquals("Major Tom <to2@example.com>", message.getRecipients(RecipientType.TO)[1].toString()); assertEquals(2, message.getRecipients(RecipientType.CC).length); assertEquals(CC1_EXAMPLE_COM, message.getRecipients(RecipientType.CC)[0].toString()); assertEquals("Major Carson <cc2@example.com>", message.getRecipients(RecipientType.CC)[1].toString()); assertEquals(2, message.getRecipients(RecipientType.BCC).length); assertEquals("Major Ben <bcc1@example.com>", message.getRecipients(RecipientType.BCC)[0].toString()); assertEquals(BCC2_EXAMPLE_COM, message.getRecipients(RecipientType.BCC)[1].toString()); }
final Email sentEmail = Email.create() .from("Jodd", "jodd@use.me") .to(GREEN_MAIL_COM) .textMessage("Hello") .htmlMessage("Hi!") .attachment(EmailAttachment.with() .name(ZERO) .content(BYTES_7_8_9) .inline(false)) .attachment(EmailAttachment.with() .name(ONE) .content(BYTES_4_5_6) .inline(false)) .attachment(EmailAttachment.with() .name(TWO) .content(BYTES_1_2_3) .contentId(CID_1)) .embeddedAttachment(EmailAttachment.with() .content(BYTES_0_1_0) .inline(true)) .embeddedAttachment(EmailAttachment.with() .content(BYTES_10_11_12) .name(THREE)
@SuppressWarnings("rawtypes") private static boolean sendMail(int messageType, String host, String user, String passwd, String subject, String message, String... to) { try { Email mail = Email.create() .from(user).to(to) .subject(subject); switch (messageType) { case HTML_TYPE: mail.addHtml(message); break; case TEXT_TYPE: default: mail.addText(message); break; } SmtpServer smtpServer = SmtpServer.create(host).authenticateWith(user, passwd); SendMailSession session = smtpServer.createSession(); session.open(); session.sendMail(mail); session.close(); return true; } catch (Exception e) { logger.warn(e.getMessage(), e); return false; } }
/** * Sets TO, CC and BCC in msgToSet with TO, CC and BCC from emailWithData. * * @param emailWithData {@link Email} with data * @param msgToSet {@link MimeMessage} to set data into. * @throws MessagingException if there is a failure. */ private void setRecipients(final Email emailWithData, final MimeMessage msgToSet) throws MessagingException { // TO final InternetAddress[] to = EmailAddress.convert(emailWithData.to()); if (to.length > 0) { msgToSet.setRecipients(RecipientType.TO, to); } // CC final InternetAddress[] cc = EmailAddress.convert(emailWithData.cc()); if (cc.length > 0) { msgToSet.setRecipients(RecipientType.CC, cc); } // BCC final InternetAddress[] bcc = EmailAddress.convert(emailWithData.bcc()); if (bcc.length > 0) { msgToSet.setRecipients(RecipientType.BCC, bcc); } }
/** * Appends BCC address. * * @param bcc Address may be specified with personal name like this: {@code Jenny Doe <email@foo.com>}. * @return this * @see #bcc(EmailAddress) */ public Email bcc(final String bcc) { return bcc(EmailAddress.of(bcc)); }
/** * Sets FROM, REPLY-TO and recipients. * * @param emailWithData {@link Email} with data * @param msgToSet {@link MimeMessage} to set data into. * @throws MessagingException if there is a failure */ private void setPeople(final Email emailWithData, final MimeMessage msgToSet) throws MessagingException { msgToSet.setFrom(emailWithData.from().toInternetAddress()); msgToSet.setReplyTo(EmailAddress.convert(emailWithData.replyTo())); setRecipients(emailWithData, msgToSet); }
/** * Adds message data and attachments. * * @param emailWithData {@link Email} with data * @param msgToSet {@link MimeMessage} to set data into. * @throws MessagingException if there is a failure. */ private void addBodyData(final Email emailWithData, final MimeMessage msgToSet) throws MessagingException { final List<EmailMessage> messages = emailWithData.messages(); final int totalMessages = messages.size(); // Need to use new list since filterEmbeddedAttachments(List) removes attachments from the source List final List<EmailAttachment<? extends DataSource>> attachments = new ArrayList<>(emailWithData.attachments()); if (attachments.isEmpty() && totalMessages == 1) { // special case: no attachments and just one content setContent(messages.get(0), msgToSet); } else { final MimeMultipart multipart = new MimeMultipart(); final MimeMultipart msgMultipart = new MimeMultipart(ALTERNATIVE); multipart.addBodyPart(getBaseBodyPart(msgMultipart)); for (final EmailMessage emailMessage : messages) { msgMultipart.addBodyPart(getBodyPart(emailMessage, attachments)); } addAnyAttachments(attachments, multipart); msgToSet.setContent(multipart); } }
/** * Resets BCC addresses. */ public Email resetBcc() { this.bcc = EmailAddress.EMPTY_ARRAY; return _this(); }
@Test void testWriteSimpleEmail() { final Email email = Email.create().from("Joe@example.com").to("Pig@example.com").textMessage(HELLO); final String eml = EMLComposer.create().compose(email); assertTrue(eml.contains("From: Joe@example.com\r\n")); assertTrue(eml.contains("To: Pig@example.com\r\n")); assertTrue(eml.contains(HELLO)); }
@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(); } }
/** * Sets TO, CC and BCC in msgToSet with TO, CC and BCC from emailWithData. * * @param emailWithData {@link Email} with data * @param msgToSet {@link MimeMessage} to set data into. * @throws MessagingException if there is a failure. */ private void setRecipients(final Email emailWithData, final MimeMessage msgToSet) throws MessagingException { // TO final InternetAddress[] to = EmailAddress.convert(emailWithData.to()); if (to.length > 0) { msgToSet.setRecipients(RecipientType.TO, to); } // CC final InternetAddress[] cc = EmailAddress.convert(emailWithData.cc()); if (cc.length > 0) { msgToSet.setRecipients(RecipientType.CC, cc); } // BCC final InternetAddress[] bcc = EmailAddress.convert(emailWithData.bcc()); if (bcc.length > 0) { msgToSet.setRecipients(RecipientType.BCC, bcc); } }
/** * Appends BCC address. * * @param bcc {@link Address} to add. * @return this * @see #bcc(EmailAddress) */ public Email bcc(final Address bcc) { return bcc(EmailAddress.of(bcc)); }
/** * Sets subject in msgToSet from subject in emailWithData. * * @param emailWithData {@link Email} with data * @param msgToSet {@link MimeMessage} to set data into. * @throws MessagingException if there is a failure */ private void setSubject(final Email emailWithData, final MimeMessage msgToSet) throws MessagingException { if (emailWithData.subjectEncoding() != null) { msgToSet.setSubject(emailWithData.subject(), emailWithData.subjectEncoding()); } else { msgToSet.setSubject(emailWithData.subject()); } }