class 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();
    }

}