com.amazonaws.services.simpleemail.model.Destination Java Examples

The following examples show how to use com.amazonaws.services.simpleemail.model.Destination. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Handler.java    From Building-Serverless-Architectures with MIT License 6 votes vote down vote up
private void sendEmail(final String emailAddress) {
  Destination destination = new Destination().withToAddresses(emailAddress);

  Message message = new Message()
      .withBody(new Body().withText(new Content("Welcome to our forum!")))
      .withSubject(new Content("Welcome!"));

  try {
    LOGGER.debug("Sending welcome mail to " + emailAddress);
    simpleEmailServiceClient.sendEmail(new SendEmailRequest()
        .withDestination(destination)
        .withSource(System.getenv("SenderEmail"))
        .withMessage(message)
    );
    LOGGER.debug("Sending welcome mail to " + emailAddress + " succeeded");
  } catch (Exception anyException) {
    LOGGER.error("Sending welcome mail to " + emailAddress + " failed: ", anyException);
  }

}
 
Example #2
Source File: EmailService.java    From oneops with Apache License 2.0 6 votes vote down vote up
/**
 * Send email.
 *
 * @param eMsg the e msg
 * @return true, if successful
 */
public boolean sendEmail(EmailMessage eMsg) {

    SendEmailRequest request = new SendEmailRequest().withSource(eMsg.getFromAddress());
    Destination dest = new Destination().withToAddresses(eMsg.getToAddresses());
    dest.setCcAddresses(eMsg.getToCcAddresses());
    request.setDestination(dest);
    Content subjContent = new Content().withData(eMsg.getSubject());
    Message msg = new Message().withSubject(subjContent);
    Content textContent = new Content().withData(eMsg.getTxtMessage());
    Body body = new Body().withText(textContent);
    if (eMsg.getHtmlMessage() != null) {
        Content htmlContent = new Content().withData(eMsg.getHtmlMessage());
        body.setHtml(htmlContent);
    }
    msg.setBody(body);
    request.setMessage(msg);
    try {
        emailClient.sendEmail(request);
        logger.debug(msg);
    } catch (AmazonClientException e) {
        logger.error(e.getMessage());
        return false;
    }
    return true;
}
 
Example #3
Source File: SesDaoImpl.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Prepares a {@link SendEmailRequest} with information harvested from emailSendRequest
 *
 * @param emailSendRequest the specified email information.
 *
 * @return the prepared send email request.
 */
private SendEmailRequest prepareSendEmailRequest(EmailSendRequest emailSendRequest)
{
    // Collect all required information and prepare individual email components required for the desired send email request.
    // Initialize a new aws send email request.
    SendEmailRequest sendEmailRequest = new SendEmailRequest();

    // Set 'from' address to the configured 'send-from' email address.
    sendEmailRequest.setSource(emailSendRequest.getSource());

    // Get destination information and add to send email request
    Destination destination = prepareDestination(emailSendRequest);
    sendEmailRequest.setDestination(destination);

    // Get message information and add to send email request
    Message message = prepareMessage(emailSendRequest);
    sendEmailRequest.setMessage(message);

    // Get config set name and add to send email request
    ConfigurationSet configurationSet = new ConfigurationSet().withName(configurationHelper.getProperty(ConfigurationValue.SES_CONFIGURATION_SET_NAME));

    sendEmailRequest.setConfigurationSetName(configurationSet.getName());

    return sendEmailRequest;
}
 
Example #4
Source File: LambdaSendMailFunctionHandler.java    From aws-lambda-java-example with Apache License 2.0 6 votes vote down vote up
private void sendMail(final String from, final String to,
		final String subjectStr, final String bodyStr) {
	// Construct an object to contain the recipient address.
	Destination destination = new Destination()
			.withToAddresses(new String[] { to });

	// Create the subject and body of the message.
	Content subject = new Content().withData(subjectStr);
	Content textBody = new Content().withData(bodyStr);
	Body body = new Body().withText(textBody);

	// Create a message with the specified subject and body.
	Message message = new Message().withSubject(subject).withBody(body);

	// TODO Assemble the email.

	// TODO Send the email.

}
 
Example #5
Source File: SimpleEmailServiceMailSender.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
private SendEmailRequest prepareMessage(SimpleMailMessage simpleMailMessage) {
	Destination destination = new Destination();
	destination.withToAddresses(simpleMailMessage.getTo());

	if (simpleMailMessage.getCc() != null) {
		destination.withCcAddresses(simpleMailMessage.getCc());
	}

	if (simpleMailMessage.getBcc() != null) {
		destination.withBccAddresses(simpleMailMessage.getBcc());
	}

	Content subject = new Content(simpleMailMessage.getSubject());
	Body body = new Body(new Content(simpleMailMessage.getText()));

	SendEmailRequest emailRequest = new SendEmailRequest(simpleMailMessage.getFrom(),
			destination, new Message(subject, body));

	if (StringUtils.hasText(simpleMailMessage.getReplyTo())) {
		emailRequest.withReplyToAddresses(simpleMailMessage.getReplyTo());
	}

	return emailRequest;
}
 
Example #6
Source File: SesDaoImpl.java    From herd with Apache License 2.0 4 votes vote down vote up
/**
 * Prepares the destination part of the email based on the information in the emailSendRequest
 *
 * @param emailSendRequest the specified email information.
 *
 * @return the prepared destination information.
 */
private Destination prepareDestination(EmailSendRequest emailSendRequest)
{
    // Initialize a new destinations object.
    Destination destination = new Destination();
    final String commaDelimiter = ",";

    // Set 'to' addresses.
    if (StringUtils.isNotEmpty(emailSendRequest.getTo()))
    {
        destination.setToAddresses(herdStringHelper.splitAndTrim(emailSendRequest.getTo(), commaDelimiter));
    }

    // Set 'cc' addresses if specified.
    if (StringUtils.isNotEmpty(emailSendRequest.getCc()))
    {
        destination.setCcAddresses(herdStringHelper.splitAndTrim(emailSendRequest.getCc(), commaDelimiter));
    }

    // Declare a list of bcc addresses to set in the destinations being collected.
    List<String> bccAddresses = new ArrayList<>();

    // Get the 'records-collector' address and add it to the bcc addresses list if specified.
    String recordsCollector = configurationHelper.getProperty(ConfigurationValue.SES_RECORDS_COLLECTOR_ADDRESS);
    if (StringUtils.isNotEmpty(recordsCollector))
    {
        bccAddresses.add(recordsCollector);
    }

    // Get 'bcc' addresses specified in the request.
    if (StringUtils.isNotEmpty(emailSendRequest.getBcc()))
    {
        bccAddresses.addAll(herdStringHelper.splitAndTrim(emailSendRequest.getBcc(), commaDelimiter));
    }

    // Add the final list of collected bcc addresses to destination.
    if (!CollectionUtils.isEmpty(bccAddresses))
    {
        destination.setBccAddresses(bccAddresses);
    }

    return destination;
}
 
Example #7
Source File: AwsSesMailAgent.java    From kaif with Apache License 2.0 4 votes vote down vote up
@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;
  });
}