Skip to content

Commit

Permalink
Don't assume all messages have a to address in SES. (awspring#1174)
Browse files Browse the repository at this point in the history
It's valid to send an email message through SES that doesn't have a to address (just cc or bcc).

Fixes: awspring#1173
  • Loading branch information
buckett authored Sep 13, 2024
1 parent a7a07d5 commit 4fceb0f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ protected String getConfigurationSetName() {
private SendEmailRequest prepareMessage(SimpleMailMessage simpleMailMessage) {
Assert.notNull(simpleMailMessage, "simpleMailMessage are required");
Destination.Builder destinationBuilder = Destination.builder();
destinationBuilder.toAddresses(simpleMailMessage.getTo());
if (simpleMailMessage.getTo() != null) {
destinationBuilder.toAddresses(simpleMailMessage.getTo());
}

if (simpleMailMessage.getCc() != null) {
destinationBuilder.ccAddresses(simpleMailMessage.getCc());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,32 @@ void testSendSimpleMailWithCCandBCC() {
.isEqualTo(Objects.requireNonNull(simpleMailMessage.getBcc())[0]);
}

@Test
void testSendSimpleMailWithNoTo() {
SesClient emailService = mock(SesClient.class);
SimpleEmailServiceMailSender mailSender = new SimpleEmailServiceMailSender(emailService);

// Not using createSimpleMailMessage as we don't want the to address set.
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom("sender@domain.com");
simpleMailMessage.setSubject("message subject");
simpleMailMessage.setText("message body");

simpleMailMessage.setBcc("bcc@domain.com");

ArgumentCaptor<SendEmailRequest> request = ArgumentCaptor.forClass(SendEmailRequest.class);
when(emailService.sendEmail(request.capture()))
.thenReturn(SendEmailResponse.builder().messageId("123").build());

mailSender.send(simpleMailMessage);

SendEmailRequest sendEmailRequest = request.getValue();
assertThat(sendEmailRequest.message().subject().data()).isEqualTo(simpleMailMessage.getSubject());
assertThat(sendEmailRequest.message().body().text().data()).isEqualTo(simpleMailMessage.getText());
assertThat(sendEmailRequest.destination().bccAddresses().get(0))
.isEqualTo(Objects.requireNonNull(simpleMailMessage.getBcc())[0]);
}

@Test
void testSendMultipleMails() {
SesClient emailService = mock(SesClient.class);
Expand Down

0 comments on commit 4fceb0f

Please sign in to comment.