@Test void testSendEmailSuccess() { SesClient sesClient = mock(SesClient.class); when(sesClient.sendEmail(any(SendEmailRequest.class))) .thenReturn(SendEmailResponse.builder().messageId("1234").build()); MetricRegistry metrics = new MetricRegistry(); EmailService resource = new SesEmailService(sesClient, "testAddress", MESSAGE_OPTIONS, metrics); EmailService resourceSpy = spy(resource); boolean result = resourceSpy.sendVerificationEmail(MOCK_EMAIL, VERIFICATION_URL); assertTrue(result); assertEquals(1, metrics.counter( MetricRegistry.name(EmailService.class, "email-send-success")).getCount()); assertEquals(0, metrics.counter( MetricRegistry.name(EmailService.class, "email-send-failure")).getCount()); verify(resourceSpy).sendEmail(eq(MOCK_EMAIL), eq(SUBJECT_STRING), eq("HTML verification.com"), eq("TEXT verification.com")); }
@Override public boolean sendEmail(Email to, String subjectString, String htmlBodyString, String bodyString) { Destination destination = Destination.builder().toAddresses(to.getAddress()).build(); Content subjectText = Content.builder().charset("UTF-8").data(subjectString).build(); Content htmlBodyText = Content.builder().charset("UTF-8").data(htmlBodyString).build(); Content bodyText = Content.builder().charset("UTF-8").data(bodyString).build(); Body body = Body.builder().html(htmlBodyText).text(bodyText).build(); Message message = Message.builder().subject(subjectText).body(body).build(); SendEmailRequest request = SendEmailRequest.builder() .source(fromAddress) .destination(destination) .message(message) .build(); try { sesClient.sendEmail(request); } catch (SdkException e) { LOG.error("There was an error sending email to {}", to.getAddress(), e); return false; } return true; }
SesEmailService extends EmailService { @Override public boolean sendEmail(Email to, String subjectString, String htmlBodyString, String bodyString) { Destination destination = Destination.builder().toAddresses(to.getAddress()).build(); Content subjectText = Content.builder().charset("UTF-8").data(subjectString).build(); Content htmlBodyText = Content.builder().charset("UTF-8").data(htmlBodyString).build(); Content bodyText = Content.builder().charset("UTF-8").data(bodyString).build(); Body body = Body.builder().html(htmlBodyText).text(bodyText).build(); Message message = Message.builder().subject(subjectText).body(body).build(); SendEmailRequest request = SendEmailRequest.builder() .source(fromAddress) .destination(destination) .message(message) .build(); try { sesClient.sendEmail(request); } catch (SdkException e) { LOG.error("There was an error sending email to {}", to.getAddress(), e); return false; } return true; } }
SesEmailService extends EmailService { @Override public boolean sendEmail(Email to, String subjectString, String htmlBodyString, String bodyString) { Destination destination = Destination.builder().toAddresses(to.getAddress()).build(); Content subjectText = Content.builder().charset("UTF-8").data(subjectString).build(); Content htmlBodyText = Content.builder().charset("UTF-8").data(htmlBodyString).build(); Content bodyText = Content.builder().charset("UTF-8").data(bodyString).build(); Body body = Body.builder().html(htmlBodyText).text(bodyText).build(); Message message = Message.builder().subject(subjectText).body(body).build(); SendEmailRequest request = SendEmailRequest.builder() .source(fromAddress) .destination(destination) .message(message) .build(); try { sesClient.sendEmail(request); } catch (SdkException e) { LOG.error("There was an error sending email to {}", to.getAddress(), e); return false; } return true; } @Inject  SesEmailService(SesClient sesClient, String fromAddress,
                         MessageOptions messageOptions, MetricRegistry metrics); }
SesEmailService extends EmailService { @Override public boolean sendEmail(Email to, String subjectString, String htmlBodyString, String bodyString) { Destination destination = Destination.builder().toAddresses(to.getAddress()).build(); Content subjectText = Content.builder().charset("UTF-8").data(subjectString).build(); Content htmlBodyText = Content.builder().charset("UTF-8").data(htmlBodyString).build(); Content bodyText = Content.builder().charset("UTF-8").data(bodyString).build(); Body body = Body.builder().html(htmlBodyText).text(bodyText).build(); Message message = Message.builder().subject(subjectText).body(body).build(); SendEmailRequest request = SendEmailRequest.builder() .source(fromAddress) .destination(destination) .message(message) .build(); try { sesClient.sendEmail(request); } catch (SdkException e) { LOG.error("There was an error sending email to {}", to.getAddress(), e); return false; } return true; } @Inject  SesEmailService(SesClient sesClient, String fromAddress,
                         MessageOptions messageOptions, MetricRegistry metrics); @Override boolean sendEmail(Email to,
                           String subjectString,
                           String htmlBodyString,
                           String bodyString); }
SesEmailService extends EmailService { @Override public boolean sendEmail(Email to, String subjectString, String htmlBodyString, String bodyString) { Destination destination = Destination.builder().toAddresses(to.getAddress()).build(); Content subjectText = Content.builder().charset("UTF-8").data(subjectString).build(); Content htmlBodyText = Content.builder().charset("UTF-8").data(htmlBodyString).build(); Content bodyText = Content.builder().charset("UTF-8").data(bodyString).build(); Body body = Body.builder().html(htmlBodyText).text(bodyText).build(); Message message = Message.builder().subject(subjectText).body(body).build(); SendEmailRequest request = SendEmailRequest.builder() .source(fromAddress) .destination(destination) .message(message) .build(); try { sesClient.sendEmail(request); } catch (SdkException e) { LOG.error("There was an error sending email to {}", to.getAddress(), e); return false; } return true; } @Inject  SesEmailService(SesClient sesClient, String fromAddress,
                         MessageOptions messageOptions, MetricRegistry metrics); @Override boolean sendEmail(Email to,
                           String subjectString,
                           String htmlBodyString,
                           String bodyString);  }