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

The following examples show how to use com.amazonaws.services.simpleemail.model.Message. 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 5 votes vote down vote up
/**
 * Prepares the message part of the email based on the information in the emailSendRequest
 *
 * @param emailSendRequest the specified email information
 *
 * @return the prepared message
 */
private Message prepareMessage(EmailSendRequest emailSendRequest)
{
    // Using UTF-8 which is a more commonly used charset. This is also the default for SES client.
    final String charset = "UTF-8";

    // Initialize an empty email message.
    Message message = new Message();

    // Insert subject if specified.
    if (Objects.nonNull(emailSendRequest.getSubject()))
    {
        message.setSubject(new Content().withCharset(charset).withData(emailSendRequest.getSubject()));
    }

    // Insert text body if specified (this is the SES fall-back if the recipients email client does not support html).
    Body emailBody = new Body();
    if (Objects.nonNull(emailSendRequest.getText()))
    {
        emailBody.setText(new Content().withCharset(charset).withData(emailSendRequest.getText()));
    }

    // Set the email body prepared above to the wrapper email message object.
    message.setBody(emailBody);

    return message;
}
 
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;
  });
}