ContactExtractor extends GenericMailet implements Mailet { @VisibleForTesting Optional<String> extractContacts(Mail mail) throws MessagingException { ImmutableList<String> allRecipients = getAllRecipients(mail.getMessage()); if (hasRecipient(allRecipients)) { return mail.getMaybeSender().asOptional() .map(MailAddress::asString) .map(sender -> new ExtractedContacts(sender, allRecipients)) .map(Throwing.function(extractedContacts -> objectMapper.writeValueAsString(extractedContacts))); } return Optional.empty(); }  @Override void init(); @Override String getMailetInfo(); @Override void service(Mail mail);  }
@Test public void extractContactsShouldNotThrowWhenNoRecipient() throws Exception { MimeMessageBuilder message = MimeMessageBuilder.mimeMessageBuilder() .setSender(SENDER) .setSubject("Contact collection Rocks") .setText("This is my email"); FakeMail mail = FakeMail.builder() .name("mail") .mimeMessage(message) .sender(SENDER) .build(); mailet.extractContacts(mail); }