Example usage for com.amazonaws.services.simpleemail AmazonSimpleEmailService listVerifiedEmailAddresses

List of usage examples for com.amazonaws.services.simpleemail AmazonSimpleEmailService listVerifiedEmailAddresses

Introduction

In this page you can find the example usage for com.amazonaws.services.simpleemail AmazonSimpleEmailService listVerifiedEmailAddresses.

Prototype

ListVerifiedEmailAddressesResult listVerifiedEmailAddresses();

Source Link

Document

Simplified method form for invoking the ListVerifiedEmailAddresses operation.

Usage

From source file:AWSJavaMailSample.java

License:Open Source License

/**
 * Sends a request to Amazon Simple Email Service to verify the specified
 * email address. This triggers a verification email, which will contain a
 * link that you can click on to complete the verification process.
 *
 * @param ses//from w  ww. j a  v  a  2s. co  m
 *            The Amazon Simple Email Service client to use when making
 *            requests to Amazon SES.
 * @param address
 *            The email address to verify.
 */
private static void verifyEmailAddress(AmazonSimpleEmailService ses, String address) {
    ListVerifiedEmailAddressesResult verifiedEmails = ses.listVerifiedEmailAddresses();
    if (verifiedEmails.getVerifiedEmailAddresses().contains(address))
        return;

    ses.verifyEmailAddress(new VerifyEmailAddressRequest().withEmailAddress(address));
    System.out.println("Please check the email address " + address + " to verify it");
    System.exit(0);
}

From source file:com.ajah.email.data.AmazonSESTransport.java

License:Apache License

/**
 * Sends a message through Amazon's SES service.
 * /*from   w  ww. ja  va2 s.c o  m*/
 * @param message
 *            The message to send. Subject, from and to are required.
 * @throws AddressException
 *             If there is a problem with one of the email addresses.
 * @throws MessagingException
 *             If there is a problem with the transport of the message.
 */
@Override
public void send(final EmailMessage message) throws AddressException, MessagingException {
    AjahUtils.requireParam(message, "message");
    AjahUtils.requireParam(message.getSubject(), "message.subject");
    AjahUtils.requireParam(message.getFrom(), "message.from");
    AjahUtils.requireParam(message.getRecipients(), "message.recipients");

    final AWSCredentials credentials = new BasicAWSCredentials(Config.i.get("aws.accessKey", null),
            Config.i.get("aws.secretKey", null));
    if (Config.i.getBoolean("aws.ses.verify", false)) {
        // Verification is active so we'll need to check that first
        final AmazonSimpleEmailService email = new AmazonSimpleEmailServiceClient(credentials);
        final ListVerifiedEmailAddressesResult verifiedEmails = email.listVerifiedEmailAddresses();
        boolean verified = true;
        if (!isVerified(message.getFrom(), email, verifiedEmails)) {
            log.warning("Sender " + message.getFrom() + " is not verified");
            verified = false;
        }
        for (final EmailRecipient emailRecipient : message.getRecipients()) {
            if (!isVerified(emailRecipient.getAddress(), email, verifiedEmails)) {
                log.warning("Recipient " + emailRecipient.getAddress() + " is not verified");
                verified = false;
            }
        }
        if (!verified) {
            throw new MessagingException("Message not sent because one or more addresses need to be verified");
        }
    }
    final Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "aws");
    props.setProperty("mail.aws.user", credentials.getAWSAccessKeyId());
    props.setProperty("mail.aws.password", credentials.getAWSSecretKey());

    final Session session = Session.getInstance(props);

    final MimeMessage mimeMessage = new MimeMessage(session);
    mimeMessage.setFrom(new InternetAddress(message.getFrom().toString()));
    for (final EmailRecipient emailRecipient : message.getRecipients()) {
        switch (emailRecipient.getType()) {
        case BCC:
            mimeMessage.addRecipient(Message.RecipientType.BCC, new InternetAddress(emailRecipient.toString()));
            break;
        case CC:
            mimeMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(emailRecipient.toString()));
            break;
        case TO:
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(emailRecipient.toString()));
            break;
        default:
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(emailRecipient.toString()));
            break;
        }
    }
    mimeMessage.setSubject(message.getSubject());
    final String htmlContent = message.getHtml();
    if (StringUtils.isBlank(htmlContent)) {
        // No HTML so we'll just send a plaintext message.
        mimeMessage.setText(message.getText());
    } else {
        final Multipart multiPart = new MimeMultipart("alternative");

        final BodyPart text = new MimeBodyPart();
        text.setText(message.getText());
        multiPart.addBodyPart(text);

        final BodyPart html = new MimeBodyPart();
        html.setContent(message.getHtml(), "text/html");
        multiPart.addBodyPart(html);

        mimeMessage.setContent(multiPart);
    }
    mimeMessage.saveChanges();

    final Transport transport = new AWSJavaMailTransport(session, null);
    transport.connect();
    transport.sendMessage(mimeMessage, null);
    transport.close();

}

From source file:com.ajah.email.data.EmailMessageManager.java

License:Apache License

/**
 * Sends a message through Amazon's SES service.
 * //from w ww. j  av  a  2  s.  c  o  m
 * @param message
 *            The message to send. Subject, from and to are required.
 * @throws AddressException
 *             If there is a problem with one of the email addresses.
 * @throws MessagingException
 *             If there is a problem with the transport of the message.
 */
public static void send(final EmailMessage message) throws AddressException, MessagingException {
    AjahUtils.requireParam(message, "message");
    AjahUtils.requireParam(message.getSubject(), "message.subject");
    AjahUtils.requireParam(message.getFrom(), "message.from");
    AjahUtils.requireParam(message.getTo(), "message.to");

    final AWSCredentials credentials = new BasicAWSCredentials(Config.i.get("aws.accessKey", null),
            Config.i.get("aws.secretKey", null));
    if (Config.i.getBoolean("aws.ses.verify", false)) {
        // Verification is active so we'll need to check that first
        final AmazonSimpleEmailService email = new AmazonSimpleEmailServiceClient(credentials);
        final ListVerifiedEmailAddressesResult verifiedEmails = email.listVerifiedEmailAddresses();
        boolean verified = true;
        if (!isVerified(message.getFrom(), email, verifiedEmails)) {
            log.warning("Sender " + message.getFrom() + " is not verified");
            verified = false;
        }
        for (final EmailAddress emailAddress : message.getTo()) {
            if (!isVerified(emailAddress, email, verifiedEmails)) {
                log.warning("Recipient " + emailAddress + " is not verified");
                verified = false;
            }
        }
        if (!verified) {
            throw new MessagingException("Message not sent because one or more addresses need to be verified");
        }
    }
    final Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "aws");
    props.setProperty("mail.aws.user", credentials.getAWSAccessKeyId());
    props.setProperty("mail.aws.password", credentials.getAWSSecretKey());

    final Session session = Session.getInstance(props);

    final MimeMessage mimeMessage = new MimeMessage(session);
    mimeMessage.setFrom(new InternetAddress(message.getFrom().toString()));
    for (final EmailAddress to : message.getTo()) {
        mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to.toString()));
    }
    mimeMessage.setSubject(message.getSubject());
    final String htmlContent = message.getHtml();
    if (StringUtils.isBlank(htmlContent)) {
        // No HTML so we'll just send a plaintext message.
        mimeMessage.setText(message.getText());
    } else {
        final Multipart multiPart = new MimeMultipart("alternative");

        final BodyPart text = new MimeBodyPart();
        text.setText(message.getText());
        multiPart.addBodyPart(text);

        final BodyPart html = new MimeBodyPart();
        html.setContent(message.getHtml(), "text/html");
        multiPart.addBodyPart(html);

        mimeMessage.setContent(multiPart);
    }
    mimeMessage.saveChanges();

    final Transport transport = new AWSJavaMailTransport(session, null);
    transport.connect();
    transport.sendMessage(mimeMessage, null);
    transport.close();

}

From source file:spikes.email.AmazonSimpleEmailServiceSpike.java

License:Open Source License

/**
  * SES requires that the sender and receiver of each message be
  * verified through the service. The verifyEmailAddress interface will
  * send the given address a verification message with a URL they can
  * click to verify that address./*from w  w w  .j  a  v a  2  s  .com*/
  */
static void verifyAddressIfNecessary(AmazonSimpleEmailService service, String address) {
    ListVerifiedEmailAddressesResult verifiedEmails = service.listVerifiedEmailAddresses();
    if (verifiedEmails.getVerifiedEmailAddresses().contains(address))
        return;

    service.verifyEmailAddress(new VerifyEmailAddressRequest().withEmailAddress(address));
    System.out.println("Please check the email address " + address + " to verify it.");
    System.exit(0);
}