diff --git a/spring-cloud-aws-ses/src/main/java/io/awspring/cloud/ses/SimpleEmailServiceMailSender.java b/spring-cloud-aws-ses/src/main/java/io/awspring/cloud/ses/SimpleEmailServiceMailSender.java index c757d575a..a89537199 100644 --- a/spring-cloud-aws-ses/src/main/java/io/awspring/cloud/ses/SimpleEmailServiceMailSender.java +++ b/spring-cloud-aws-ses/src/main/java/io/awspring/cloud/ses/SimpleEmailServiceMailSender.java @@ -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()); diff --git a/spring-cloud-aws-ses/src/test/java/io/awspring/cloud/ses/SimpleEmailServiceMailSenderTest.java b/spring-cloud-aws-ses/src/test/java/io/awspring/cloud/ses/SimpleEmailServiceMailSenderTest.java index b0fe71bac..a51c34b0c 100644 --- a/spring-cloud-aws-ses/src/test/java/io/awspring/cloud/ses/SimpleEmailServiceMailSenderTest.java +++ b/spring-cloud-aws-ses/src/test/java/io/awspring/cloud/ses/SimpleEmailServiceMailSenderTest.java @@ -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 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);