DefaultEmailSenderAdapter implements EmailSenderAdapter { @Override public void send(final Email email) { if (!enabled) { return; } Assert.notNull(email, "Email object cannot be null"); try { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, ENCODING); messageHelper.setFrom(email.getSender()); messageHelper.setTo(getRecipientsArray(email.getRecipients())); messageHelper.setCc(getRecipientsArray(email.getCcRecipients())); messageHelper.setSubject(email.getTopic()); messageHelper.setText(email.getContent(), email.isHtml()); for (Attachment attachment : email.getAttachments()) { messageHelper.addAttachment(attachment.getFileName(), new ByteArrayResource(IOUtils.toByteArray(attachment.getInputStream()))); } mailSender.send(message); } catch (Exception e) { throw new MailException(EXCEPTION_MESSAGE, e); } }  @Override void send(final Email email); void setEnabled(final Boolean enabled);  }
@Test public void testSend() throws Exception { Attachment attachment1 = new Attachment(); String fileName1 = "filename1"; InputStream inputStream1 = new ByteArrayInputStream("SomeContent1".getBytes()); attachment1.setFileName(fileName1); attachment1.setInputStream(inputStream1); Attachment attachment2 = new Attachment(); String fileName2 = "filename2"; InputStream inputStream2 = new ByteArrayInputStream("SomeContent2".getBytes()); attachment2.setFileName(fileName2); attachment2.setInputStream(inputStream2); List<Attachment> attachments = Arrays.asList(attachment1, attachment2); String email1 = "email1@email.email"; Recipient recipient1 = new Recipient(email1); Recipient recipient2 = new Recipient("email2@email.email"); List<Recipient> ccRecipients = Arrays.asList(recipient1, recipient2); List<Recipient> recipients = Arrays.asList(recipient1, recipient2); String content = "Simple content"; boolean isHtml = false; String sender = "sender@mail.com"; String topic = "Mail topic"; Email email = new Email(); email.setAttachments(attachments); email.setCcRecipients(ccRecipients); email.setContent(content); email.setHtml(isHtml); email.setRecipients(recipients); email.setSender(sender); email.setTopic(topic); when(mailSender.createMimeMessage()).thenReturn(mimeMessage); unit.send(email); verify(mimeMessage).setFrom(any(InternetAddress.class)); verify(mimeMessage).setRecipients(eq(Message.RecipientType.TO), any(InternetAddress[].class)); verify(mimeMessage).setRecipients(eq(Message.RecipientType.CC), any(InternetAddress[].class)); verify(mimeMessage).setSubject(topic, "UTF-8"); verify(mailSender).send(mimeMessage); }
@Test public void testSendWithEmptyAttachments() throws Exception { String email1 = "email1@email.email"; Recipient recipient1 = new Recipient(email1); Recipient recipient2 = new Recipient("email2@email.email"); List<Recipient> ccRecipients = Arrays.asList(recipient1, recipient2); List<Attachment> attachments = Collections.emptyList(); List<Recipient> recipients = Arrays.asList(recipient1, recipient2); String content = "Simple content"; boolean isHtml = false; String sender = "sender@mail.com"; String topic = "Mail topic"; Email email = new Email(); email.setAttachments(attachments); email.setCcRecipients(ccRecipients); email.setContent(content); email.setHtml(isHtml); email.setRecipients(recipients); email.setSender(sender); email.setTopic(topic); when(mailSender.createMimeMessage()).thenReturn(mimeMessage); unit.send(email); verify(mimeMessage).setFrom(any(InternetAddress.class)); verify(mimeMessage).setRecipients(eq(Message.RecipientType.TO), any(InternetAddress[].class)); verify(mimeMessage).setRecipients(eq(Message.RecipientType.CC), any(InternetAddress[].class)); verify(mimeMessage).setSubject(topic, "UTF-8"); verify(mailSender).send(mimeMessage); }
@Test(expected = MailException.class) public void testSendWithException() throws Exception { Attachment attachment1 = new Attachment(); String fileName1 = "filename1"; InputStream inputStream1 = new ByteArrayInputStream("SomeContent1".getBytes()); attachment1.setFileName(fileName1); attachment1.setInputStream(inputStream1); Attachment attachment2 = new Attachment(); String fileName2 = "filename2"; InputStream inputStream2 = new ByteArrayInputStream("SomeContent2".getBytes()); attachment2.setFileName(fileName2); attachment2.setInputStream(inputStream2); List<Attachment> attachments = Arrays.asList(attachment1, attachment2); String email1 = "email1@email.email"; Recipient recipient1 = new Recipient(email1); Recipient recipient2 = new Recipient("email2@email.email"); List<Recipient> ccRecipients = Arrays.asList(recipient1, recipient2); List<Recipient> recipients = Arrays.asList(recipient1, recipient2); String content = "Simple content"; boolean isHtml = false; String sender = "sender@mail.com"; String topic = "Mail topic"; Email email = new Email(); email.setAttachments(attachments); email.setCcRecipients(ccRecipients); email.setContent(content); email.setHtml(isHtml); email.setRecipients(recipients); email.setSender(sender); email.setTopic(topic); when(mailSender.createMimeMessage()).thenReturn(mimeMessage); doThrow(Exception.class).when(mailSender).send(any(MimeMessage.class)); unit.send(email); verify(mimeMessage).setFrom(any(InternetAddress.class)); verify(mimeMessage).setRecipients(eq(Message.RecipientType.TO), any(InternetAddress[].class)); verify(mimeMessage).setRecipients(eq(Message.RecipientType.CC), any(InternetAddress[].class)); verify(mimeMessage).setSubject(topic, "UTF-8"); verify(mailSender).send(mimeMessage); }