@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."); } }
@PostConstruct public void afterPropertiesSet() { AWSCredentials awsSesCredentials = new BasicAWSCredentials(mailProperties.getAwsAccessKey(), mailProperties.getAwsSecretKey()); this.client = AmazonSimpleEmailServiceClientBuilder.standard() .withRegion(Regions.US_EAST_1) .withCredentials(new AWSStaticCredentialsProvider(awsSesCredentials)) .build(); logger.info("mail agent ready, sender:" + mailProperties.getAwsSenderAddress() + ", access key:" + awsSesCredentials.getAWSAccessKeyId()); }
public static AmazonSimpleEmailServiceClient createEmailClient() { BasicCredentialsProvider credentials = BasicCredentialsProvider.standard(); AmazonSimpleEmailServiceClient client = !credentials.isValid() ? null : (AmazonSimpleEmailServiceClient) AmazonSimpleEmailServiceClientBuilder.standard() .withCredentials(credentials) .withRegion("eu-west-1") .build(); return client; }
@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; }
/** * No-args constructor. */ public AWSEmailer() { sesclient = AmazonSimpleEmailServiceClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider( new BasicAWSCredentials(Config.AWS_ACCESSKEY, Config.AWS_SECRETKEY))). withRegion(Config.AWS_REGION).build(); }