@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 testCreateMimeMessageWithExceptionInInputStream() throws Exception { InputStream inputStream = mock(InputStream.class); AmazonSimpleEmailService emailService = mock(AmazonSimpleEmailService.class); JavaMailSender mailSender = new SimpleEmailServiceJavaMailSender(emailService); IOException ioException = new IOException("error"); when(inputStream.read(ArgumentMatchers.any(byte[].class), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt())).thenThrow(ioException); try { mailSender.createMimeMessage(inputStream); fail("MailPreparationException expected due to error while creating mail"); } catch (MailParseException e) { assertTrue(e.getMessage().startsWith("Could not parse raw MIME content")); assertSame(ioException, e.getCause().getCause()); } }
@Test public void testSendMultipleMailsWithException() throws Exception { AmazonSimpleEmailService emailService = mock(AmazonSimpleEmailService.class); JavaMailSender mailSender = new SimpleEmailServiceJavaMailSender(emailService); MimeMessage failureMail = createMimeMessage(); when(emailService.sendRawEmail(ArgumentMatchers.isA(SendRawEmailRequest.class))). thenReturn(new SendRawEmailResult()). thenThrow(new AmazonClientException("error")). thenReturn(new SendRawEmailResult()); try { mailSender.send(createMimeMessage(), failureMail, createMimeMessage()); fail("Exception expected due to error while sending mail"); } catch (MailSendException e) { assertEquals(1, e.getFailedMessages().size()); assertTrue(e.getFailedMessages().containsKey(failureMail)); } }
@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()); }
@Test public void createClient() throws IOException, TemplateException { Mailer mailer = mock(Mailer.class); doCallRealMethod().when(mailer).createClient(); Object client = mailer.createClient(); assertThat(client, instanceOf(AmazonSimpleEmailService.class)); }
@Override void makeSend(String to, String subject, String msg) { String accessKey = config.readString(ConfigProperty.SES_ACCESS_KEY); String secretKey = config.readString(ConfigProperty.SES_SECRET_KEY); if (!Strings.isNullOrEmpty(accessKey)) { Destination destination = new Destination().withToAddresses(to); Content subj = new Content().withData(subject); Content textBody = new Content().withData(msg); Body body = new Body().withHtml(textBody); Message message = new Message().withSubject(subj).withBody(body); SendEmailRequest req = new SendEmailRequest().withSource(config.readString(ConfigProperty.EMAIL_DEFAULT_FROM_NAME) + " <" + config.readString(ConfigProperty.EMAIL_DEFAULT_FROM) + ">") .withDestination(destination).withMessage(message); AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard() .withRegion(config.readString(ConfigProperty.SES_REGION)) .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey))) .build(); try { client.sendEmail(req); } finally { client.shutdown(); } } else { throw new IllegalStateException("SES Mail provider wasn't configured well."); } }
@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)); }
@Test public void testShutDownOfResources() throws Exception { AmazonSimpleEmailService emailService = mock(AmazonSimpleEmailService.class); SimpleEmailServiceMailSender mailSender = new SimpleEmailServiceMailSender(emailService); mailSender.destroy(); verify(emailService, times(1)).shutdown(); }
@Test public void testSendMimeMessage() throws MessagingException, IOException { AmazonSimpleEmailService emailService = mock(AmazonSimpleEmailService.class); JavaMailSender mailSender = new SimpleEmailServiceJavaMailSender(emailService); ArgumentCaptor<SendRawEmailRequest> request = ArgumentCaptor.forClass(SendRawEmailRequest.class); when(emailService.sendRawEmail(request.capture())).thenReturn(new SendRawEmailResult().withMessageId("123")); MimeMessage mimeMessage = createMimeMessage(); mailSender.send(mimeMessage); SendRawEmailRequest rawEmailRequest = request.getValue(); assertTrue(Arrays.equals(getMimeMessageAsByteArray(mimeMessage), rawEmailRequest.getRawMessage().getData().array())); }
@Test public void testSendMultipleMimeMessages() throws Exception { AmazonSimpleEmailService emailService = mock(AmazonSimpleEmailService.class); JavaMailSender mailSender = new SimpleEmailServiceJavaMailSender(emailService); when(emailService.sendRawEmail(ArgumentMatchers.isA(SendRawEmailRequest.class))).thenReturn(new SendRawEmailResult().withMessageId("123")); mailSender.send(createMimeMessage(), createMimeMessage()); verify(emailService, times(2)).sendRawEmail(ArgumentMatchers.isA(SendRawEmailRequest.class)); }
@Override public Parameters handleRequest(Parameters parameters, Context context) { context.getLogger().log("Input Function [" + context.getFunctionName() + "], Parameters [" + parameters + "]"); try { // Create an empty Mime message and start populating it Session session = Session.getDefaultInstance(new Properties()); MimeMessage message = new MimeMessage(session); message.setSubject(EMAIL_SUBJECT, "UTF-8"); message.setFrom(new InternetAddress(System.getenv("EMAIL_FROM"))); message.setReplyTo(new Address[] { new InternetAddress(System.getenv("EMAIL_FROM")) }); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(System.getenv("EMAIL_RECIPIENT"))); MimeBodyPart wrap = new MimeBodyPart(); MimeMultipart cover = new MimeMultipart("alternative"); MimeBodyPart html = new MimeBodyPart(); cover.addBodyPart(html); wrap.setContent(cover); MimeMultipart content = new MimeMultipart("related"); message.setContent(content); content.addBodyPart(wrap); // Create an S3 URL reference to the snapshot that will be attached to this email URL attachmentURL = createSignedURL(parameters.getS3Bucket(), parameters.getS3Key()); StringBuilder sb = new StringBuilder(); String id = UUID.randomUUID().toString(); sb.append("<img src=\"cid:"); sb.append(id); sb.append("\" alt=\"ATTACHMENT\"/>\n"); // Add the attachment as a part of the message body MimeBodyPart attachment = new MimeBodyPart(); DataSource fds = new URLDataSource(attachmentURL); attachment.setDataHandler(new DataHandler(fds)); attachment.setContentID("<" + id + ">"); attachment.setDisposition(BodyPart.ATTACHMENT); attachment.setFileName(fds.getName()); content.addBodyPart(attachment); // Pretty print the Rekognition Labels as part of the Emails HTML content String prettyPrintLabels = parameters.getRekognitionLabels().toString(); prettyPrintLabels = prettyPrintLabels.replace("{", "").replace("}", ""); prettyPrintLabels = prettyPrintLabels.replace(",", "<br>"); html.setContent("<html><body><h2>Uploaded Filename : " + parameters.getS3Key().replace("upload/", "") + "</h2><p><b>Detected Labels/Confidence</b><br><br>" + prettyPrintLabels + "</p>"+sb+"</body></html>", "text/html"); // Convert the JavaMail message into a raw email request for sending via SES ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); message.writeTo(outputStream); RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray())); SendRawEmailRequest rawEmailRequest = new SendRawEmailRequest(rawMessage); // Send the email using the AWS SES Service AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.defaultClient(); client.sendRawEmail(rawEmailRequest); } catch (MessagingException | IOException e) { // Convert Checked Exceptions to RuntimeExceptions to ensure that // they get picked up by the Step Function infrastructure throw new AmazonServiceException("Error in ["+context.getFunctionName()+"]", e); } context.getLogger().log("Output Function [" + context.getFunctionName() + "], Parameters [" + parameters + "]"); return parameters; }
public AmazonSimpleEmailService createClient() { return AmazonSimpleEmailServiceClient .builder() .withRegion(Regions.US_EAST_1) .build(); }
public AmazonMailDeliveryServiceImpl(final AmazonSimpleEmailService amazonSimpleEmailService) { this.amazonSimpleEmailService = amazonSimpleEmailService; }
public AmazonSimpleEmailService getAmazonSESClient() { return amazonSESClient; }
/** * To use the AmazonSimpleEmailService as the client */ public void setAmazonSESClient(AmazonSimpleEmailService amazonSESClient) { this.amazonSESClient = amazonSESClient; }
public AmazonSimpleEmailService getSESClient() { return sesClient; }
@Bean @ConditionalOnMissingAmazonClient(AmazonSimpleEmailService.class) public AmazonWebserviceClientFactoryBean<AmazonSimpleEmailServiceClient> amazonSimpleEmailService(AWSCredentialsProvider credentialsProvider) { return new AmazonWebserviceClientFactoryBean<>(AmazonSimpleEmailServiceClient.class, credentialsProvider, this.regionProvider); }
@Bean @ConditionalOnMissingClass("org.springframework.cloud.aws.mail.simplemail.SimpleEmailServiceJavaMailSender") public MailSender simpleMailSender(AmazonSimpleEmailService amazonSimpleEmailService) { return new SimpleEmailServiceMailSender(amazonSimpleEmailService); }
@Bean @ConditionalOnClass(name = "javax.mail.Session") public JavaMailSender javaMailSender(AmazonSimpleEmailService amazonSimpleEmailService) { return new SimpleEmailServiceJavaMailSender(amazonSimpleEmailService); }
public SimpleEmailServiceMailSender(AmazonSimpleEmailService amazonSimpleEmailService) { this.emailService = amazonSimpleEmailService; }
protected AmazonSimpleEmailService getEmailService() { return this.emailService; }
public SimpleEmailServiceJavaMailSender(AmazonSimpleEmailService amazonSimpleEmailService) { super(amazonSimpleEmailService); }