/** * {@inheritDoc} */ @Override public EmailResponse sendEmail(EmailRequest emailRequest) { try { SendEmailResult result = this.asyncSES.sendEmail(new SendEmailRequest() .withSource(this.emailConfig.from()) .withDestination(new Destination() .withToAddresses(emailRequest.getRecipientToList()) .withCcAddresses(emailRequest.getRecipientCcList()) .withBccAddresses(emailRequest.getRecipientBccList())) .withMessage(new Message() .withSubject(new Content().withData(emailRequest.getSubject())) .withBody(new Body().withHtml(new Content().withData(emailRequest.getBody()))))); return new EmailResponse(result.getMessageId(), result.getSdkHttpMetadata().getHttpStatusCode(), result.getSdkHttpMetadata().getHttpHeaders()); } catch (Exception ex) { LOGGER.error("Exception while sending email!!", ex); throw new AwsException(ex.getMessage(), ex); } }
protected Void doInBackground(String...messages) { if( messages.length == 0 ) return null; // build the message and destination objects Content subject = new Content( "OpenCaption" ); Body body = new Body( new Content( messages[0] ) ); Message message = new Message( subject, body ); Destination destination = new Destination().withToAddresses( toAddress ); // send out the email SendEmailRequest request = new SendEmailRequest( fromVerifiedAddress, destination, message ); // END:asynctask SendEmailResult result = // START:asynctask sesClient.sendEmail( request ); // END:asynctask Log.d( "glass.opencaption", "AWS SES resp message id:" + result.getMessageId() ); // START:asynctask return null; }
@SuppressWarnings("OverloadedVarargsMethod") @Override public void send(SimpleMailMessage... simpleMailMessages) throws MailException { Map<Object, Exception> failedMessages = new HashMap<>(); for (SimpleMailMessage simpleMessage : simpleMailMessages) { try { SendEmailResult sendEmailResult = getEmailService().sendEmail(prepareMessage(simpleMessage)); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Message with id: {} successfully send", sendEmailResult.getMessageId()); } } catch (AmazonClientException e) { //Ignore Exception because we are collecting and throwing all if any //noinspection ThrowableResultOfMethodCallIgnored failedMessages.put(simpleMessage, e); } } if (!failedMessages.isEmpty()) { throw new MailSendException(failedMessages); } }
@Test public void testSendSimpleMailWithMinimalProperties() throws Exception { AmazonSimpleEmailService emailService = mock(AmazonSimpleEmailService.class); SimpleEmailServiceMailSender mailSender = new SimpleEmailServiceMailSender(emailService); SimpleMailMessage simpleMailMessage = createSimpleMailMessage(); ArgumentCaptor<SendEmailRequest> request = ArgumentCaptor.forClass(SendEmailRequest.class); when(emailService.sendEmail(request.capture())).thenReturn(new SendEmailResult().withMessageId("123")); mailSender.send(simpleMailMessage); SendEmailRequest sendEmailRequest = request.getValue(); assertEquals(simpleMailMessage.getFrom(), sendEmailRequest.getSource()); assertEquals(simpleMailMessage.getTo()[0], sendEmailRequest.getDestination().getToAddresses().get(0)); assertEquals(simpleMailMessage.getSubject(), sendEmailRequest.getMessage().getSubject().getData()); assertEquals(simpleMailMessage.getText(), sendEmailRequest.getMessage().getBody().getText().getData()); assertEquals(0, sendEmailRequest.getDestination().getCcAddresses().size()); assertEquals(0, sendEmailRequest.getDestination().getBccAddresses().size()); }
@Test public void send() throws IOException, TemplateException { Mailer mailer = mock(Mailer.class); mailer.setTemplateConfiguration(LinkGeneratorLambdaHandler.templateConfiguration); doReturn("Content").when(mailer).createBody(); doCallRealMethod().when(mailer).send(); AmazonSimpleEmailService client = mock(AmazonSimpleEmailService.class); doReturn(mock(SendEmailResult.class)).when(client).sendEmail(any()); doReturn(client).when(mailer).createClient(); mailer.send(); verify(client, times(1)).sendEmail(any()); }
@Override default void onSuccess(SendEmailRequest request, SendEmailResult result) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Email sent to: {}", request.getDestination().getToAddresses()); LOGGER.debug("SES SendEmailResult messageId: [{}]", result.getMessageId()); } }
@Override public SendEmailResult sendEmail(SendEmailRequest sendEmailRequest) throws AmazonServiceException, AmazonClientException { this.sendEmailRequest = sendEmailRequest; SendEmailResult result = new SendEmailResult(); result.setMessageId("1"); return result; }
@Test public void testSendMultipleMails() throws Exception { AmazonSimpleEmailService emailService = mock(AmazonSimpleEmailService.class); SimpleEmailServiceMailSender mailSender = new SimpleEmailServiceMailSender(emailService); ArgumentCaptor<SendEmailRequest> request = ArgumentCaptor.forClass(SendEmailRequest.class); when(emailService.sendEmail(request.capture())).thenReturn(new SendEmailResult().withMessageId("123")); mailSender.send(createSimpleMailMessage(), createSimpleMailMessage()); verify(emailService, times(2)).sendEmail(ArgumentMatchers.any(SendEmailRequest.class)); }
/** * Method to send a text email. * @param m email message to be sent. * @return id of message that has been sent. * @throws MailNotSentException if mail couldn't be sent. */ private String sendTextEmail(AWSTextEmailMessage m) throws MailNotSentException { String messageId; long currentTimestamp = System.currentTimeMillis(); prepareClient(); if (!mEnabled) { //don't send message if not enabled return null; } try { synchronized (this) { //prevents throttling checkQuota(currentTimestamp); Destination destination = new Destination(m.getTo()); if (m.getBCC() != null && !m.getBCC().isEmpty()) { destination.setBccAddresses(m.getBCC()); } if (m.getCC() != null && !m.getCC().isEmpty()) { destination.setCcAddresses(m.getCC()); } //if no subject, set to empty string to avoid errors if (m.getSubject() == null) { m.setSubject(""); } Message message = new Message(); m.buildContent(message); SendEmailResult result = mClient.sendEmail(new SendEmailRequest(mMailFromAddress, destination, message)); messageId = result.getMessageId(); //update timestamp of last sent email mLastSentMailTimestamp = System.currentTimeMillis(); //wait to avoid throwttling exceptions to avoid making any //further requests this.wait(mWaitIntervalMillis); } } catch (Throwable t) { throw new MailNotSentException(t); } return messageId; }
@Override public CompletableFuture<Boolean> send(Mail mailMessage) throws MailException { Message message = new Message(); message.setSubject(new Content(mailMessage.getSubject()).withCharset(Charsets.UTF_8.toString())); message.setBody(new Body(new Content(mailMessage.getText()).withCharset(Charsets.UTF_8.toString()))); Destination destination = new Destination(asList(mailMessage.getTo())); Optional.ofNullable(mailMessage.getCc()) .filter(cc -> cc.length > 0) .ifPresent(cc -> destination.setCcAddresses(asList(cc))); Optional.ofNullable(mailMessage.getBcc()) .filter(cc -> cc.length > 0) .ifPresent(cc -> destination.setBccAddresses(asList(cc))); SendEmailRequest sendEmailRequest = new SendEmailRequest(composeSource(mailMessage).toString(), destination, message); Optional.ofNullable(mailMessage.getReplyTo()) .ifPresent(r -> sendEmailRequest.setReplyToAddresses(asList(r))); return CompletableFuture.supplyAsync(() -> { double totalWait = rateLimiter.acquire(); if (totalWait > 5) { logger.warn("rate limit wait too long: " + totalWait + " seconds"); } SendEmailResult emailResult = client.sendEmail(sendEmailRequest); if (logger.isDebugEnabled()) { logger.debug("sent mail messageId:{}, body:\n{}", emailResult.getMessageId(), mailMessage); } else { logger.info("sent mail to {}, messageId:{}", destination, emailResult.getMessageId()); } return true; }, executor).handle((result, e) -> { if (e != null) { logger.warn("fail send mail to " + destination + ", error:" + e.getMessage()); return false; } return true; }); }