Example usage for com.amazonaws.services.simpleemail AmazonSimpleEmailServiceClient AmazonSimpleEmailServiceClient

List of usage examples for com.amazonaws.services.simpleemail AmazonSimpleEmailServiceClient AmazonSimpleEmailServiceClient

Introduction

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

Prototype

AmazonSimpleEmailServiceClient(AwsSyncClientParams clientParams) 

Source Link

Document

Constructs a new client to invoke service methods on Amazon SES using the specified parameters.

Usage

From source file:AWSJavaMailSample.java

License:Open Source License

public static void main(String[] args) throws IOException {
    /*//from   www . j  a v  a  2s  . c  om
     * Important: Be sure to fill in your AWS access credentials in the
     * AwsCredentials.properties file before you try to run this sample.
     * http://aws.amazon.com/security-credentials
     */
    PropertiesCredentials credentials = new PropertiesCredentials(
            AWSJavaMailSample.class.getResourceAsStream("AwsCredentials.properties"));
    AmazonSimpleEmailService ses = new AmazonSimpleEmailServiceClient(credentials);

    /*
     * Before you can send email via Amazon SES, you need to verify that you
     * own the email address from which youll be sending email. This will
     * trigger a verification email, which will contain a link that you can
     * click on to complete the verification process.
     */
    verifyEmailAddress(ses, FROM);

    /*
     * If you've just signed up for SES, then you'll be placed in the Amazon
     * SES sandbox, where you must also verify the email addresses you want
     * to send mail to.
     *
     * You can uncomment the line below to verify the TO address in this
     * sample.
     *
     * Once you have full access to Amazon SES, you will *not* be required
     * to verify each email address you want to send mail to.
     *
     * You can request full access to Amazon SES here:
     * http://aws.amazon.com/ses/fullaccessrequest
     */
    //verifyEmailAddress(ses, TO);

    /*
     * Setup JavaMail to use the Amazon Simple Email Service by specifying
     * the "aws" protocol.
     */
    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "aws");

    /*
     * Setting mail.aws.user and mail.aws.password are optional. Setting
     * these will allow you to send mail using the static transport send()
     * convince method.  It will also allow you to call connect() with no
     * parameters. Otherwise, a user name and password must be specified
     * in connect.
     */
    props.setProperty("mail.aws.user", credentials.getAWSAccessKeyId());
    props.setProperty("mail.aws.password", credentials.getAWSSecretKey());

    Session session = Session.getInstance(props);

    try {
        // Create a new Message
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(FROM));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(TO));
        msg.setSubject(SUBJECT);
        msg.setText(BODY);
        msg.saveChanges();

        // Reuse one Transport object for sending all your messages
        // for better performance
        Transport t = new AWSJavaMailTransport(session, null);
        t.connect();
        t.sendMessage(msg, null);

        // Close your transport when you're completely done sending
        // all your messages
        t.close();
    } catch (AddressException e) {
        e.printStackTrace();
        System.out.println("Caught an AddressException, which means one or more of your "
                + "addresses are improperly formatted.");
    } catch (MessagingException e) {
        e.printStackTrace();
        System.out.println("Caught a MessagingException, which means that there was a "
                + "problem sending your message to Amazon's E-mail Service check the "
                + "stack trace for more information.");
    }
}

From source file:AmazonSESSample.java

License:Open Source License

public static void main(String[] args) throws IOException {

    // 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(SUBJECT);
    Content textBody = new Content().withData(BODY);
    Body body = new Body().withText(textBody);

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

    // Assemble the email.
    SendEmailRequest request = new SendEmailRequest().withSource(FROM).withDestination(destination)
            .withMessage(message);/*from  w  w w  .j  ava2s .c o m*/

    try {
        System.out.println("Attempting to send an email through Amazon SES by using the AWS SDK for Java...");

        /*
         * The ProfileCredentialsProvider will return your [haow2]
         * credential profile by reading from the credentials file located at
         * (/Users/Dawn/.aws/credentials).
         *
         * TransferManager manages a pool of threads, so we create a
         * single instance and share it throughout our application.
         */
        AWSCredentials credentials = null;
        try {
            credentials = new ProfileCredentialsProvider("haow2").getCredentials();
        } catch (Exception e) {
            throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                    + "Please make sure that your credentials file is at the correct "
                    + "location (/Users/Dawn/.aws/credentials), and is in valid format.", e);
        }

        // Instantiate an Amazon SES client, which will make the service call with the supplied AWS credentials.
        AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(credentials);

        // Choose the AWS region of the Amazon SES endpoint you want to connect to. Note that your production
        // access status, sending limits, and Amazon SES identity-related settings are specific to a given
        // AWS region, so be sure to select an AWS region in which you set up Amazon SES. Here, we are using
        // the US East (N. Virginia) region. Examples of other regions that Amazon SES supports are US_WEST_2
        // and EU_WEST_1. For a complete list, see http://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html
        Region REGION = Region.getRegion(Regions.US_EAST_1);
        client.setRegion(REGION);

        // Send the email.
        client.sendEmail(request);
        System.out.println("Email sent!");

    } catch (Exception ex) {
        System.out.println("The email was not sent.");
        System.out.println("Error message: " + ex.getMessage());
    }
}

From source file:aws.sample.AWSJavaMailSample.java

License:Open Source License

public static void main(String[] args) throws IOException {
    /*//from   www. ja  v  a  2  s  .  co  m
     * Important: Be sure to fill in your AWS access credentials in the AwsCredentials.properties file before you try to run this sample. http://aws.amazon.com/security-credentials
     */
    PropertiesCredentials credentials = new PropertiesCredentials(
            AWSJavaMailSample.class.getResourceAsStream("/AwsCredentials.properties"));
    AmazonSimpleEmailService ses = new AmazonSimpleEmailServiceClient(credentials);

    /*
     * Before you can send email via Amazon SES, you need to verify that you own the email address from which you?l be sending email. This will trigger a verification email, which will contain a link that you can click on to complete the verification process.
     */
    verifyEmailAddress(ses, FROM);

    /*
     * If you've just signed up for SES, then you'll be placed in the Amazon SES sandbox, where you must also verify the email addresses you want to send mail to.
     * 
     * You can uncomment the line below to verify the TO address in this sample.
     * 
     * Once you have full access to Amazon SES, you will *not* be required to verify each email address you want to send mail to.
     * 
     * You can request full access to Amazon SES here: http://aws.amazon.com/ses/fullaccessrequest
     */
    // verifyEmailAddress(ses, TO);

    /*
     * Setup JavaMail to use the Amazon Simple Email Service by specifying the "aws" protocol.
     */
    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "aws");

    /*
     * Setting mail.aws.user and mail.aws.password are optional. Setting these will allow you to send mail using the static transport send() convince method. It will also allow you to call connect() with no parameters. Otherwise, a user name and password must be specified in connect.
     */
    props.setProperty("mail.aws.user", credentials.getAWSAccessKeyId());
    props.setProperty("mail.aws.password", credentials.getAWSSecretKey());

    Session session = Session.getInstance(props);

    try {
        // Create a new Message
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(FROM));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(TO));
        msg.setSubject(SUBJECT);
        msg.setText(BODY);
        msg.saveChanges();

        // Reuse one Transport object for sending all your messages
        // for better performance
        Transport t = new AWSJavaMailTransport(session, null);
        t.connect();
        t.sendMessage(msg, null);

        // Close your transport when you're completely done sending
        // all your messages
        t.close();
    } catch (AddressException e) {
        e.printStackTrace();
        System.out.println("Caught an AddressException, which means one or more of your "
                + "addresses are improperly formatted.");
    } catch (MessagingException e) {
        e.printStackTrace();
        System.out.println("Caught a MessagingException, which means that there was a "
                + "problem sending your message to Amazon's E-mail Service check the "
                + "stack trace for more information.");
    }
}

From source file:com.aipo.aws.ses.SES.java

License:Open Source License

/**
 * AmazonSimpleEmailService???/* ww w .  j  a v a  2s  . c  o m*/
 * 
 * @return
 */
public static AmazonSimpleEmailService getClient() {
    AWSContext awsContext = AWSContext.get();
    if (awsContext == null) {
        throw new IllegalStateException("AWSContext is not initialized.");
    }
    AmazonSimpleEmailService client = new AmazonSimpleEmailServiceClient(awsContext.getAwsCredentials());
    String endpoint = awsContext.getSesEndpoint();
    if (endpoint != null && endpoint != "") {
        client.setEndpoint(endpoint);
    }
    return client;
}

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

License:Apache License

/**
 * Sends a message through Amazon's SES service.
 * /*from   w  w  w .  jav  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.
 */
@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  w  w .ja v  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:com.amazon.aws.demo.AmazonClientManager.java

License:Open Source License

public void validateCredentials() {
    if (sesClient == null) {
        Log.i(LOG_TAG, "Creating New Clients.");

        AWSCredentials credentials = new BasicAWSCredentials(PropertyLoader.getInstance().getAccessKey(),
                PropertyLoader.getInstance().getSecretKey());
        sesClient = new AmazonSimpleEmailServiceClient(credentials);
    }//from ww w.j av a  2s.  c o  m
}

From source file:com.app.ses.AmazonSES.java

License:Open Source License

public void sendMail(String TO, String USUARIO2, String SUBJECT, String CONTENIDO, String FOLIO,
        String USERCREATE, String NOMBRE) {

    // 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(SUBJECT);

    Content htmlContent = new Content().withData(" <body bgcolor=\"#FFFFFF\">\n" + "    <!-- HEADER -->\n"
            + "        <table class=\"head-wrap\" bgcolor=\"red\">\n" + "            <tr>\n"
            + "                <td></td>\n" + "                <td class=\"header container\" >\n"
            + "                    <div class=\"content\">\n"
            + "                        <table bgcolor=\"red\">\n" + "                            <tr>\n"
            + "                                <td style=\"font-size: x-large; color:white;\">\n"
            + "                                    S G C o n\n" + "                                </td>\n"
            + "                                <td style=\"text-align: right; color:white; fon\">\n"
            + "                                    Notificaciones\n" + "                                </td>\n"
            + "                            </tr>\n" + "                        </table>\n"
            + "                    </div>\n" + "                </td>\n" + "                <td></td>\n"
            + "            </tr>\n" + "        </table><!-- /HEADER -->\n" + "        <!-- BODY -->\n"
            + "        <table class=\"body-wrap\">\n" + "            <tr>\n" + "                <td></td>\n"
            + "                <td class=\"container\" bgcolor=\"#FFFFFF\">\n"
            + "                    <div class=\"content\">\n" + "                        <table>\n"
            + "                            <tr>\n" + "                                <td>\n"
            + "                                    <h3>Hola, " + USUARIO2 + "</h3>\n"
            + "                                    <p class=\"callout\">\n"
            + "                                        " + CONTENIDO + "\n"
            + "                                    </p>\n" + "                                    <p>\n"
            + "                                        <table style=\"font-size: medium\">\n"
            + "                                            <tr>\n"
            + "                                                <td>Folio: " + FOLIO + "</td>\n"
            + "                                            </tr>\n"
            + "                                            <tr>\n"
            + "                                                <td>" + USERCREATE + "</td>\n"
            + "                                            </tr>\n"
            + "                                            <tr>\n"
            + "                                                <td>" + NOMBRE + "</td>\n"
            + "                                            </tr>\n"
            + "                                        </table>\n"
            + "                                    </p> <br/>\n"
            + "                                     <table>\n"
            + "                                         <tr>\n"
            + "                                             <td>\n"
            + "                                                 <p>\n"
            + "                                                     <a href=\"http://www.sgcon-infonavit.com\" class=\"soc-btn fb\">Ir a la aplicacin</a> \n"
            + "                                                 </p>\n"
            + "                                             </td>\n"
            + "                                         </tr>\n"
            + "                                     </table\n" + "                                </td>\n"
            + "                            </tr>\n" + "                        </table>\n"
            + "                    </div>\n" + "                </td>\n" + "                <td></td>\n"
            + "            </tr>\n" + "        </table>\n" + "    </body>");

    Body body = new Body().withHtml(htmlContent);

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

    // Assemble the email.
    SendEmailRequest request = new SendEmailRequest().withSource(FROM).withDestination(destination)
            .withMessage(message);//from  w  w w  . j a  va2s.com

    try {
        System.out.println("Attempting to send an email through Amazon SES by using the AWS SDK for Java...");

        /*
         * The ProfileCredentialsProvider will return your [default]
         * credential profile by reading from the credentials file located at
         * (~/.aws/credentials).
         *
         * TransferManager manages a pool of threads, so we create a
         * single instance and share it throughout our application.
         */
        /*AWSCredentials credentials = null;
         try {
         credentials = new ProfileCredentialsProvider().getCredentials();
         } catch (Exception e) {
         throw new AmazonClientException(
         "Cannot load the credentials from the credential profiles file. "
         + "Please make sure that your credentials file is at the correct "
         + "location (~/.aws/credentials), and is in valid format.",
         e);
         }*/
        AWSCredentials awsCreds = new BasicAWSCredentials("AKIAJFO4NNHIPNTB4ZCQ",
                "VjYeQ9+xj3HAcLBSxxxbHTo6jy2ft0wF6tgWZXmA");
        // Instantiate an Amazon SES client, which will make the service call with the supplied AWS credentials.
        //AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(credentials);
        AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(awsCreds);

        // Choose the AWS region of the Amazon SES endpoint you want to connect to. Note that your production
        // access status, sending limits, and Amazon SES identity-related settings are specific to a given
        // AWS region, so be sure to select an AWS region in which you set up Amazon SES. Here, we are using
        // the US East (N. Virginia) region. Examples of other regions that Amazon SES supports are US_WEST_2
        // and EU_WEST_1. For a complete list, see http://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html
        Region REGION = Region.getRegion(Regions.US_WEST_2);
        client.setRegion(REGION);

        // Send the email.
        client.sendEmail(request);
        System.out.println("Email sent!");

    } catch (Exception ex) {
        System.out.println("The email was not sent.");
        System.out.println("Error message: " + ex.getMessage());
    }
}

From source file:com.app.ses.AmazonSES.java

License:Open Source License

public void sendMailDos(String TO, String USUARIO2, String SUBJECT, String CONTENIDO, String LISTA) {

    System.out.println("Si imprime la llamada del la clase  djscnlskdncfksdlncfksdcldsncewldn");

    //operaciones con el json       
    // 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(SUBJECT);

    Content htmlContent = new Content().withData(" <body bgcolor=\"#FFFFFF\">\n" + "    <!-- HEADER -->\n"
            + "        <table class=\"head-wrap\" bgcolor=\"red\">\n" + "            <tr>\n"
            + "                <td></td>\n" + "                <td class=\"header container\" >\n"
            + "                    <div class=\"content\">\n"
            + "                        <table bgcolor=\"red\">\n" + "                            <tr>\n"
            + "                                <td style=\"font-size: x-large; color:white;\">\n"
            + "                                    S G C o n\n" + "                                </td>\n"
            + "                                <td style=\"text-align: right; color:white; fon\">\n"
            + "                                    Notificaciones\n" + "                                </td>\n"
            + "                            </tr>\n" + "                        </table>\n"
            + "                    </div>\n" + "                </td>\n" + "                <td></td>\n"
            + "            </tr>\n" + "        </table><!-- /HEADER -->\n" + "        <!-- BODY -->\n"
            + "        <table class=\"body-wrap\">\n" + "            <tr>\n" + "                <td></td>\n"
            + "                <td class=\"container\" bgcolor=\"#FFFFFF\">\n"
            + "                    <div class=\"content\">\n" + "                        <table>\n"
            + "                            <tr>\n" + "                                <td>\n"
            + "                                    <h3>Hola, " + USUARIO2 + "</h3>\n"
            + "                                    <p class=\"callout\">\n"
            + "                                        " + CONTENIDO + "\n"
            + "                                    </p>\n" + "                                    <p>\n"
            + "                                        <table style=\"font-size: medium\">\n"
            + "                                            <tr>\n"
            + "                                                <td>Folio: " + LISTA + "</td>\n"
            + "                                            </tr>\n"
            + "                                        </table>\n"
            + "                                    </p> <br/>\n"
            + "                                     <table>\n"
            + "                                         <tr>\n"
            + "                                             <td>\n"
            + "                                                 <p>\n"
            + "                                                     <a href=\"http://www.sgcon-infonavit.com\" class=\"soc-btn fb\">Ir a la aplicacin</a> \n"
            + "                                                 </p>\n"
            + "                                             </td>\n"
            + "                                         </tr>\n"
            + "                                     </table\n" + "                                </td>\n"
            + "                            </tr>\n" + "                        </table>\n"
            + "                    </div>\n" + "                </td>\n" + "                <td></td>\n"
            + "            </tr>\n" + "        </table>\n" + "    </body>");

    Body body = new Body().withHtml(htmlContent);

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

    // Assemble the email.
    SendEmailRequest request = new SendEmailRequest().withSource(FROM).withDestination(destination)
            .withMessage(message);//www  .  ja va2s. co  m

    try {
        System.out.println("Attempting to send an email through Amazon SES by using the AWS SDK for Java...");

        /*
         * The ProfileCredentialsProvider will return your [default]
         * credential profile by reading from the credentials file located at
         * (~/.aws/credentials).
         *
         * TransferManager manages a pool of threads, so we create a
         * single instance and share it throughout our application.
         */
        /*AWSCredentials credentials = null;
         try {
         credentials = new ProfileCredentialsProvider().getCredentials();
         } catch (Exception e) {
         throw new AmazonClientException(
         "Cannot load the credentials from the credential profiles file. "
         + "Please make sure that your credentials file is at the correct "
         + "location (~/.aws/credentials), and is in valid format.",
         e);
         }*/
        AWSCredentials awsCreds = new BasicAWSCredentials("AKIAJFO4NNHIPNTB4ZCQ",
                "VjYeQ9+xj3HAcLBSxxxbHTo6jy2ft0wF6tgWZXmA");
        // Instantiate an Amazon SES client, which will make the service call with the supplied AWS credentials.
        //AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(credentials);
        AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(awsCreds);

        // Choose the AWS region of the Amazon SES endpoint you want to connect to. Note that your production
        // access status, sending limits, and Amazon SES identity-related settings are specific to a given
        // AWS region, so be sure to select an AWS region in which you set up Amazon SES. Here, we are using
        // the US East (N. Virginia) region. Examples of other regions that Amazon SES supports are US_WEST_2
        // and EU_WEST_1. For a complete list, see http://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html
        Region REGION = Region.getRegion(Regions.US_WEST_2);
        client.setRegion(REGION);

        // Send the email.
        client.sendEmail(request);
        System.out.println("Email sent!");

    } catch (Exception ex) {
        System.out.println("The email was not sent.");
        System.out.println("Error message: " + ex.getMessage());
    }
}

From source file:com.devnexus.ting.core.service.integration.AmazonSesSender.java

License:Apache License

@Autowired
public AmazonSesSender(AmazonSettings amazonSettings) {
    final AWSCredentials awsCredentials = new BasicAWSCredentials(amazonSettings.getAwsAccessKeyId(),
            amazonSettings.getAwsSecretKey());
    client = new AmazonSimpleEmailServiceClient(awsCredentials);
}