AmazonSES sending emails to multiple people when only one person is included in the Destination field

Viewed 32

I'm working on sending transactional emails with AmazonSES. I'm noticing that when two SendEmailRequests are made within a couple of minutes from each other, AmazonSES sends the first email correctly. But the second email includes both email addresses in the to field.

For example, if I'm sending two emails, one to janeDoe@gmail.com and another to jamesSmith@gmail.com. In the email sent to janeDoe@gmail.com she will see that the email has been sent just to her. But jamesSmith@gmail.com will see that the email has been sent to him and janeDoe@gmail.com.

In other words, Jane (the first receiver) is receiving an email that was meant only for James (the second receiver) to see.

Code for email sending

sendEmailRequest
        .withDestination(
                destination.withToAddresses(to))
        .withMessage(message
                .withBody(body
                        .withHtml(new Content()
                                .withCharset("UTF-8").withData(htmlBody))
                        .withText(new Content()
                                .withCharset("UTF-8").withData(textBody)))
                .withSubject(new Content()
                        .withCharset("UTF-8").withData(subject)))
        .withSource(from);

amazonSimpleEmailServiceClient.sendEmail(sendEmailRequest);

Does anyone know why this is happening? Is there a glitch in AmazonSES?

1 Answers

The root cause of the problem

  • The main cause of the problem was that I seemed to be re-using instances of Destination object instead of instantiating new instances of Destination object for every sendEmailRequest

Why was I re-using instances?

  • Well I made a @Configuration module that provides a bean for Destination object with spring.
  • I autowired the Destination object so Spring will handle the dependancy injection for me. What im guessing is that from each sendEmailRequest spring is injecting the same instane of the Destination object. This resulted in my problem and which was why I thought it was a problem on AmazonSES's side.

All in all

DONT do this

@Service
public class EmailService {

    Logger logger = LoggerFactory.getLogger(EmailService.class);

    @Autowired
    AmazonSimpleEmailService amazonSimpleEmailServiceClient;

    @Autowired
    SendEmailRequest sendEmailRequest;

    @Autowired
    Destination destination;

    @Autowired
    Message message;

    @Autowired
    Body body;

    public void sendEmail(String from, String to, String subject, String htmlBody, String textBody) throws CouldNotSendEmailException {
        try {
            sendEmailRequest
                    .withDestination(
                            destination.withToAddresses(to))
                    .withMessage(message
                            .withBody(body
                                    .withHtml(new Content()
                                            .withCharset("UTF-8").withData(htmlBody))
                                    .withText(new Content()
                                            .withCharset("UTF-8").withData(textBody)))
                            .withSubject(new Content()
                                    .withCharset("UTF-8").withData(subject)))
                    .withSource(from);

            amazonSimpleEmailServiceClient.sendEmail(sendEmailRequest);
        } catch (Exception ex) {
            throw new CouldNotSendEmailException(ex.getMessage());
        }
    }

 
}

Do this instead (notice the "new Destination()")

@Service
public class EmailService {

    Logger logger = LoggerFactory.getLogger(EmailService.class);

    @Autowired
    AmazonSimpleEmailService amazonSimpleEmailServiceClient;

    @Autowired
    SendEmailRequest sendEmailRequest;

    @Autowired
    Message message;

    @Autowired
    Body body;

    public void sendEmail(String from, String to, String subject, String htmlBody, String textBody) throws CouldNotSendEmailException {
        try {
            sendEmailRequest
                    .withDestination(
                            new Destination().withToAddresses(to))
                    .withMessage(message
                            .withBody(body
                                    .withHtml(new Content()
                                            .withCharset("UTF-8").withData(htmlBody))
                                    .withText(new Content()
                                            .withCharset("UTF-8").withData(textBody)))
                            .withSubject(new Content()
                                    .withCharset("UTF-8").withData(subject)))
                    .withSource(from);

            amazonSimpleEmailServiceClient.sendEmail(sendEmailRequest);
        } catch (Exception ex) {
            throw new CouldNotSendEmailException(ex.getMessage());
        }
    }

 
}
Related