EmailSenderImpl implements EmailSender { @Override public void sendEmail( String fromEmail, String toEmail, String subject, String body, List<Pair<String, InputStream>> attachments) throws IOException { notNull(fromEmail, "fromEmail must be non-null"); notNull(toEmail, "toEmail must be non-null"); notNull(subject, "subject must be non-null"); notNull(body, "body must be non-null"); notNull(attachments, "attachments must be non-null"); if (StringUtils.isBlank(mailHost)) { throw new IOException("the mail server hostname has not been configured"); } List<File> tempFiles = new LinkedList<>(); try { InternetAddress emailAddr = new InternetAddress(toEmail); emailAddr.validate(); Properties properties = createSessionProperties(); Session session = Session.getDefaultInstance(properties); MimeMessage mimeMessage = new MimeMessage(session); mimeMessage.setFrom(new InternetAddress(fromEmail)); mimeMessage.addRecipient(Message.RecipientType.TO, emailAddr); mimeMessage.setSubject(subject); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText(body); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); Holder<Long> bytesWritten = new Holder<>(0L); for (Pair<String, InputStream> attachment : attachments) { messageBodyPart = new MimeBodyPart(); File file = File.createTempFile("email-sender-", ".dat"); tempFiles.add(file); copyDataToTempFile(file, attachment.getValue(), bytesWritten); messageBodyPart.setDataHandler(new DataHandler(new FileDataSource(file))); messageBodyPart.setFileName(attachment.getKey()); multipart.addBodyPart(messageBodyPart); } mimeMessage.setContent(multipart); send(mimeMessage); LOGGER.debug("Email sent to " + toEmail); } catch (AddressException e) { throw new IOException("invalid email address: email=" + toEmail, e); } catch (MessagingException e) { throw new IOException("message error occurred on send", e); } finally { tempFiles.forEach( file -> { if (!file.delete()) { LOGGER.debug("unable to delete tmp file: path={}", file); } }); } }  void setMailPort(int mailPort); void setMailHost(String mailHost); void setMaxAttachmentSize(long maxAttachmentSize); @Override void sendEmail(
      String fromEmail,
      String toEmail,
      String subject,
      String body,
      List<Pair<String, InputStream>> attachments); @Override void sendEmail(String fromEmail, String toEmail, String subject, String body); @Override String toString();  }
@Test public void testSendEmailSubject() throws IOException, MessagingException { emailSender.sendEmail(FROM_ADDR, TO_ADDR, SUBJECT_LINE, BODY, Collections.emptyList()); assertThat(capturedMessage.value.getSubject(), is(SUBJECT_LINE)); }
@Test public void testBody() throws IOException, MessagingException { emailSender.sendEmail(FROM_ADDR, TO_ADDR, SUBJECT_LINE, BODY, Collections.emptyList()); MimeMultipart mimeMultipart = (MimeMultipart) capturedMessage.value.getContent(); Object content = mimeMultipart.getBodyPart(0).getContent(); assertThat(content, is(BODY)); }