Мне нужно отправить несколько электронных писем с шаблонами, используя amazon ses.
public void sendSESMessage(EmailBatch emailBatch) {
try {
/*loadClient*/
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
AmazonSimpleEmailService amazonSES = AmazonSimpleEmailServiceClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(Regions.AP_SOUTH_1).build();
List<EmailMessage> emailMessages = emailMessageRepository.findAllByEmailBatchAndStatus(emailBatch,MessageStatus.PENDING);
List<Pair<String, String>> contacts = new ArrayList<Pair<String, String>>();
for(EmailMessage emailMessage: emailMessages){
String recepientName = emailContactRepository.findAllByEmailId(emailMessage.getToAddress()).get(0).getFirstName();
contacts.add(new Pair<String, String>(emailMessage.getToAddress(), recepientName));
}
/*createTemplate*/
Template template = new Template()
.withTemplateName("ContactNameTemplate")
.withHtmlPart(emailBatch.getEmailSchedule().getSubject())
.withSubjectPart(emailBatch.getEmailSchedule().getMessage());
CreateTemplateRequest createTemplateRequest = new CreateTemplateRequest().withTemplate(template);
CreateTemplateResult result = amazonSES.createTemplate(createTemplateRequest);
/*sendBulkTemplatedEmail*/
List<BulkEmailDestination> bulkEmailDestinations = new ArrayList<BulkEmailDestination>();
for (Pair<String, String> contact : contacts) {
Destination destination = new Destination();
List<String> toAddresses = new ArrayList<>();
toAddresses.add(contact.getKey());
destination.setToAddresses(toAddresses);
BulkEmailDestination bulkEmailDestination = new BulkEmailDestination();
bulkEmailDestination.setDestination(destination);
bulkEmailDestination.setReplacementTemplateData(String.format("{ \"user\":\"%s\" }", contact.getValue()));
bulkEmailDestinations.add(bulkEmailDestination);
}
SendBulkTemplatedEmailRequest bulkTemplatedEmailRequest = new SendBulkTemplatedEmailRequest();
VerifyEmailAddressRequest verifyEmailAddressRequest = new VerifyEmailAddressRequest().withEmailAddress(emailBatch.getFromAddress());
bulkTemplatedEmailRequest.withDestinations(bulkEmailDestinations);
bulkTemplatedEmailRequest.withTemplate("ContactNameTemplate");
bulkTemplatedEmailRequest.withDefaultTemplateData(String.format("{ \"user\":\"%s\" }", "UserNameNotAvailable"));
bulkTemplatedEmailRequest.withSource(emailBatch.getFromAddress());
SendBulkTemplatedEmailResult sendBulkTemplatedEmailResult = amazonSES.sendBulkTemplatedEmail(bulkTemplatedEmailRequest);
} catch (Exception e) {
log.error("The email was not sent. Error message: "+ e.getMessage(),e);
}
}
Наряду с этим я хотел бы получить уведомление об обратной связи по электронной почте, отправлено ли оно / открыто / доставлено / отклонено ... Могу ли я использовать amazon SNS для этой цели?Как я могу реализовать Java SDK для этой цели?Могу ли я получить простой пример кода для этого?