UseHeaderRecipients extends GenericMailet { @Override public void service(Mail mail) throws MessagingException { MimeMessage message = mail.getMessage(); mail.setRecipients(headersAddresses(message)); if (isDebug) { LOGGER.debug("All recipients = {}", mail.getRecipients()); LOGGER.debug("Reprocessing mail using recipients in message headers"); } getMailetContext().sendMail(mail.duplicate()); mail.setState(Mail.GHOST); }  @Override void init(); @Override void service(Mail mail); Collection<MailAddress> headersAddresses(MimeMessage mimeMessage); @Override String getMailetInfo();  }
@Test void serviceShouldSetMimeMessageRecipients() throws Exception { FakeMail fakeMail = FakeMail.builder() .name("mail") .recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.ANY_AT_JAMES2) .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() .addToRecipient(RECIPIENT1, RECIPIENT2)) .build(); testee.service(fakeMail); assertThat(fakeMail.getRecipients()) .containsOnly(mailAddress1, mailAddress2); }
@Test void serviceShouldSetToCcAndBccSpecifiedInTheMimeMessage() throws Exception { FakeMail fakeMail = FakeMail.builder() .name("mail") .recipients(MailAddressFixture.ANY_AT_JAMES) .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() .addToRecipient(RECIPIENT1) .addCcRecipient(RECIPIENT2) .addBccRecipient(RECIPIENT3)) .build(); testee.service(fakeMail); assertThat(fakeMail.getRecipients()) .containsOnly(mailAddress1, mailAddress2, mailAddress3); }
@Test void serviceShouldSetEmptyRecipientWhenNoRecipientsInTheMimeMessage() throws Exception { FakeMail fakeMail = FakeMail.builder() .name("mail") .recipients(MailAddressFixture.ANY_AT_JAMES) .mimeMessage(MimeMessageUtil.defaultMimeMessage()) .build(); testee.service(fakeMail); assertThat(fakeMail.getRecipients()) .isEmpty(); }
@Test void serviceShouldGhostEmail() throws Exception { FakeMail fakeMail = FakeMail.builder() .name("mail") .recipients(MailAddressFixture.ANY_AT_JAMES) .mimeMessage(MimeMessageUtil.defaultMimeMessage()) .build(); testee.service(fakeMail); assertThat(fakeMail.getState()) .isEqualTo(Mail.GHOST); }
@Test void serviceShouldResendTheEmail() throws Exception { FakeMail fakeMail = FakeMail.builder() .name("mail") .recipients(MailAddressFixture.ANY_AT_JAMES) .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() .addToRecipient(RECIPIENT1) .addCcRecipient(RECIPIENT2) .addBccRecipient(RECIPIENT3)) .build(); testee.service(fakeMail); assertThat(mailetContext.getSentMails()) .containsOnly(FakeMailContext.sentMailBuilder() .recipients(mailAddress1, mailAddress2, mailAddress3) .fromMailet() .build()); }
@Test void serviceShouldThrowOnInvalidMailAddress() throws Exception { FakeMail fakeMail = FakeMail.builder() .name("mail") .recipients(mailAddress1) .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() .addToRecipient("invalid")) .build(); assertThatThrownBy(() -> testee.service(fakeMail)).isInstanceOf(RuntimeException.class); }
@Test void serviceShouldSupportAddressList() throws Exception { FakeMail fakeMail = FakeMail.builder() .name("mail") .recipients() .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() .addToRecipient(RECIPIENT1, RECIPIENT2)) .build(); testee.service(fakeMail); assertThat(fakeMail.getRecipients()) .containsOnly(mailAddress1, mailAddress2); }
@Test void serviceShouldSupportMailboxes() throws Exception { FakeMail fakeMail = FakeMail.builder() .name("mail") .recipients() .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() .addToRecipient("APACHE" + "<" + UseHeaderRecipientsTest.RECIPIENT1 + ">")) .build(); testee.service(fakeMail); assertThat(fakeMail.getRecipients()) .containsOnly(mailAddress1); }