@Test public void shouldIgnoreNullBcc() throws Exception { when(javaMailSender.createMimeMessage()).thenReturn(new MimeMessage((Session) null)); final DefaultEmail email = DefaultEmail.builder() .from(getCiceroMainMailAddress()) .replyTo(getCiceroSecondayMailAddress()) .to(Lists.newArrayList(new InternetAddress("roberto.trunfio@gmail.com", "titus@de-rerum.natura", "Pomponius Attĭcus"))) .cc(Lists.newArrayList(new InternetAddress("tito55@de-rerum.natura", "Titus Lucretius Carus"), new InternetAddress("info@de-rerum.natura", "Info Best Seller"))) .depositionNotificationTo(new InternetAddress("caligola@urbs.aeterna", "Gaius Iulius Caesar Augustus Germanicus")) .receiptTo(new InternetAddress("caligola@urbs.aeterna", "Gaius Iulius Caesar Augustus Germanicus")) .subject("Laelius de amicitia") .body("Firmamentum autem stabilitatis constantiaeque eius, quam in amicitia quaerimus, fides est.") .customHeaders(CUSTOM_HEADERS) .build(); MimeMessage message = emailToMimeMessage.apply(email); assertions.assertThat(message.getRecipients(Message.RecipientType.BCC)).isNullOrEmpty(); }
@Override public MimeMessage apply(final Email email) { final MimeMessage mimeMessage = javaMailSender.createMimeMessage(); final boolean isMultipart = nonNull(email.getAttachments()) && !email.getAttachments().isEmpty(); try { final MimeMessageHelperExt messageHelper = new MimeMessageHelperExt(mimeMessage, isMultipart, fromNullable(email.getEncoding()).or(StandardCharsets.UTF_8.name())); messageHelper.setFrom(email.getFrom()); if (nonNull(email.getReplyTo())) { messageHelper.setReplyTo(email.getReplyTo()); } if (nonNull(email.getTo())) { for (final InternetAddress address : email.getTo()) { messageHelper.addTo(address); } } if (nonNull(email.getCc())) { for (final InternetAddress address : email.getCc()) { messageHelper.addCc(address); } } if (nonNull(email.getBcc())) { for (final InternetAddress address : email.getBcc()) { messageHelper.addBcc(address); } } if (isMultipart) { for (final EmailAttachment attachment : email.getAttachments()) { messageHelper.addAttachment(attachment.getAttachmentName(), attachment.getInputStream()); } } messageHelper.setSubject(ofNullable(email.getSubject()).orElse(EMPTY)); messageHelper.setText(ofNullable(email.getBody()).orElse(EMPTY)); if (nonNull(email.getSentAt())) { messageHelper.setSentDate(email.getSentAt()); } if (nonNull(email.getReceiptTo())) { messageHelper.setHeaderReturnReceipt(email.getReceiptTo().getAddress()); } if (nonNull(email.getDepositionNotificationTo())) { messageHelper.setHeaderDepositionNotificationTo(email.getDepositionNotificationTo().getAddress()); } if (nonNull(email.getCustomHeaders()) && !email.getCustomHeaders().isEmpty()) { setCustomHeaders(email, mimeMessage); } mimeMessage.saveChanges(); } catch (MessagingException e) { log.error("Error while converting DefaultEmail to MimeMessage"); throw new EmailConversionException(e); } return mimeMessage; }
EmailToMimeMessage implements Function<Email, MimeMessage> { @Override public MimeMessage apply(final Email email) { final MimeMessage mimeMessage = javaMailSender.createMimeMessage(); final boolean isMultipart = nonNull(email.getAttachments()) && !email.getAttachments().isEmpty(); try { final MimeMessageHelperExt messageHelper = new MimeMessageHelperExt(mimeMessage, isMultipart, fromNullable(email.getEncoding()).or(StandardCharsets.UTF_8.name())); messageHelper.setFrom(email.getFrom()); if (nonNull(email.getReplyTo())) { messageHelper.setReplyTo(email.getReplyTo()); } if (nonNull(email.getTo())) { for (final InternetAddress address : email.getTo()) { messageHelper.addTo(address); } } if (nonNull(email.getCc())) { for (final InternetAddress address : email.getCc()) { messageHelper.addCc(address); } } if (nonNull(email.getBcc())) { for (final InternetAddress address : email.getBcc()) { messageHelper.addBcc(address); } } if (isMultipart) { for (final EmailAttachment attachment : email.getAttachments()) { messageHelper.addAttachment(attachment.getAttachmentName(), attachment.getInputStream()); } } messageHelper.setSubject(ofNullable(email.getSubject()).orElse(EMPTY)); messageHelper.setText(ofNullable(email.getBody()).orElse(EMPTY)); if (nonNull(email.getSentAt())) { messageHelper.setSentDate(email.getSentAt()); } if (nonNull(email.getReceiptTo())) { messageHelper.setHeaderReturnReceipt(email.getReceiptTo().getAddress()); } if (nonNull(email.getDepositionNotificationTo())) { messageHelper.setHeaderDepositionNotificationTo(email.getDepositionNotificationTo().getAddress()); } if (nonNull(email.getCustomHeaders()) && !email.getCustomHeaders().isEmpty()) { setCustomHeaders(email, mimeMessage); } mimeMessage.saveChanges(); } catch (MessagingException e) { log.error("Error while converting DefaultEmail to MimeMessage"); throw new EmailConversionException(e); } return mimeMessage; } }
EmailToMimeMessage implements Function<Email, MimeMessage> { @Override public MimeMessage apply(final Email email) { final MimeMessage mimeMessage = javaMailSender.createMimeMessage(); final boolean isMultipart = nonNull(email.getAttachments()) && !email.getAttachments().isEmpty(); try { final MimeMessageHelperExt messageHelper = new MimeMessageHelperExt(mimeMessage, isMultipart, fromNullable(email.getEncoding()).or(StandardCharsets.UTF_8.name())); messageHelper.setFrom(email.getFrom()); if (nonNull(email.getReplyTo())) { messageHelper.setReplyTo(email.getReplyTo()); } if (nonNull(email.getTo())) { for (final InternetAddress address : email.getTo()) { messageHelper.addTo(address); } } if (nonNull(email.getCc())) { for (final InternetAddress address : email.getCc()) { messageHelper.addCc(address); } } if (nonNull(email.getBcc())) { for (final InternetAddress address : email.getBcc()) { messageHelper.addBcc(address); } } if (isMultipart) { for (final EmailAttachment attachment : email.getAttachments()) { messageHelper.addAttachment(attachment.getAttachmentName(), attachment.getInputStream()); } } messageHelper.setSubject(ofNullable(email.getSubject()).orElse(EMPTY)); messageHelper.setText(ofNullable(email.getBody()).orElse(EMPTY)); if (nonNull(email.getSentAt())) { messageHelper.setSentDate(email.getSentAt()); } if (nonNull(email.getReceiptTo())) { messageHelper.setHeaderReturnReceipt(email.getReceiptTo().getAddress()); } if (nonNull(email.getDepositionNotificationTo())) { messageHelper.setHeaderDepositionNotificationTo(email.getDepositionNotificationTo().getAddress()); } if (nonNull(email.getCustomHeaders()) && !email.getCustomHeaders().isEmpty()) { setCustomHeaders(email, mimeMessage); } mimeMessage.saveChanges(); } catch (MessagingException e) { log.error("Error while converting DefaultEmail to MimeMessage"); throw new EmailConversionException(e); } return mimeMessage; } @Autowired  EmailToMimeMessage(final JavaMailSender javaMailSender); }
EmailToMimeMessage implements Function<Email, MimeMessage> { @Override public MimeMessage apply(final Email email) { final MimeMessage mimeMessage = javaMailSender.createMimeMessage(); final boolean isMultipart = nonNull(email.getAttachments()) && !email.getAttachments().isEmpty(); try { final MimeMessageHelperExt messageHelper = new MimeMessageHelperExt(mimeMessage, isMultipart, fromNullable(email.getEncoding()).or(StandardCharsets.UTF_8.name())); messageHelper.setFrom(email.getFrom()); if (nonNull(email.getReplyTo())) { messageHelper.setReplyTo(email.getReplyTo()); } if (nonNull(email.getTo())) { for (final InternetAddress address : email.getTo()) { messageHelper.addTo(address); } } if (nonNull(email.getCc())) { for (final InternetAddress address : email.getCc()) { messageHelper.addCc(address); } } if (nonNull(email.getBcc())) { for (final InternetAddress address : email.getBcc()) { messageHelper.addBcc(address); } } if (isMultipart) { for (final EmailAttachment attachment : email.getAttachments()) { messageHelper.addAttachment(attachment.getAttachmentName(), attachment.getInputStream()); } } messageHelper.setSubject(ofNullable(email.getSubject()).orElse(EMPTY)); messageHelper.setText(ofNullable(email.getBody()).orElse(EMPTY)); if (nonNull(email.getSentAt())) { messageHelper.setSentDate(email.getSentAt()); } if (nonNull(email.getReceiptTo())) { messageHelper.setHeaderReturnReceipt(email.getReceiptTo().getAddress()); } if (nonNull(email.getDepositionNotificationTo())) { messageHelper.setHeaderDepositionNotificationTo(email.getDepositionNotificationTo().getAddress()); } if (nonNull(email.getCustomHeaders()) && !email.getCustomHeaders().isEmpty()) { setCustomHeaders(email, mimeMessage); } mimeMessage.saveChanges(); } catch (MessagingException e) { log.error("Error while converting DefaultEmail to MimeMessage"); throw new EmailConversionException(e); } return mimeMessage; } @Autowired  EmailToMimeMessage(final JavaMailSender javaMailSender); @Override MimeMessage apply(final Email email); }
EmailToMimeMessage implements Function<Email, MimeMessage> { @Override public MimeMessage apply(final Email email) { final MimeMessage mimeMessage = javaMailSender.createMimeMessage(); final boolean isMultipart = nonNull(email.getAttachments()) && !email.getAttachments().isEmpty(); try { final MimeMessageHelperExt messageHelper = new MimeMessageHelperExt(mimeMessage, isMultipart, fromNullable(email.getEncoding()).or(StandardCharsets.UTF_8.name())); messageHelper.setFrom(email.getFrom()); if (nonNull(email.getReplyTo())) { messageHelper.setReplyTo(email.getReplyTo()); } if (nonNull(email.getTo())) { for (final InternetAddress address : email.getTo()) { messageHelper.addTo(address); } } if (nonNull(email.getCc())) { for (final InternetAddress address : email.getCc()) { messageHelper.addCc(address); } } if (nonNull(email.getBcc())) { for (final InternetAddress address : email.getBcc()) { messageHelper.addBcc(address); } } if (isMultipart) { for (final EmailAttachment attachment : email.getAttachments()) { messageHelper.addAttachment(attachment.getAttachmentName(), attachment.getInputStream()); } } messageHelper.setSubject(ofNullable(email.getSubject()).orElse(EMPTY)); messageHelper.setText(ofNullable(email.getBody()).orElse(EMPTY)); if (nonNull(email.getSentAt())) { messageHelper.setSentDate(email.getSentAt()); } if (nonNull(email.getReceiptTo())) { messageHelper.setHeaderReturnReceipt(email.getReceiptTo().getAddress()); } if (nonNull(email.getDepositionNotificationTo())) { messageHelper.setHeaderDepositionNotificationTo(email.getDepositionNotificationTo().getAddress()); } if (nonNull(email.getCustomHeaders()) && !email.getCustomHeaders().isEmpty()) { setCustomHeaders(email, mimeMessage); } mimeMessage.saveChanges(); } catch (MessagingException e) { log.error("Error while converting DefaultEmail to MimeMessage"); throw new EmailConversionException(e); } return mimeMessage; } @Autowired  EmailToMimeMessage(final JavaMailSender javaMailSender); @Override MimeMessage apply(final Email email);  }