Accessing AWS SES from applications

Sending e-mails to users from your applications is a very common requirement. Provisioning hardware and installing the mail server software in your own data center adds to the upfront costs, aside from the security and high-availability concerns. AWS Simple Email Service (SES) is an e-mail platform from AWS. For example, when you want to send e-mail messages, Amazon SES is your outbound e-mail server. Alternatively, you can configure your existing e-mail server to send the e-mails via Amazon SES.

You can track deliveries, bounced messages, complaints, and rejects from the AWS console, or access the same via the SES API. You can view the e-mail volume limits for your account. You can request an increase in your quota by providing some basic information on e-mail content, and many more to AWS. You can use any standard SMTP library or the AWS SDK to interact with SES.

How to do it…

  1. Verify the e-mail address.

    Execute the following sample command to verify the e-mail address, for example, :

    $ aws ses verify-email-identity 
    --email-address [email protected]
    
  2. Installing AWS Java SDK.

    In your Maven dependency section, add the following dependency for AWS Java SDK Version 1.9.28.1:

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>1.9.28.1</version>
    </dependency>
  3. Send an e-mail using the AWS SES SDK.

    Execute the sample program to send an e-mail. The program uses the N. Virginia region for this purpose.

        // Send Email.
            public static void SendEmail() {
        // List of to addresses.
            String[] TO = new String[] { "[email protected]" };
    
        // Email from address.
            String FROM = "[email protected]";
    
        // Email subject.
            String MAILSUBJECT = "Test Email";
    
        // Email body.
            String BODY = "This is test email.";
    
        // Set to addresses.
            Destination destination = new Destination().withToAddresses(TO);
    
        // Create email subject.
            Content subject = new Content().withData(MAILSUBJECT);
    
        // Create email body.
            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);
    
        // Create BasicAWSCredentials with Access Key Id and Secret Access Key.
            BasicAWSCredentials credentials = new BasicAWSCredentials(
              "Access Key Id",
              "Secret Access Key");
    
        // Create SES client.
            AmazonSimpleEmailServiceClient sesClient = new AmazonSimpleEmailServiceClient(
            credentials);
        // Set endpoint.
            sesClient.setEndpoint("email.us-east- 1.amazonaws.com");
    
        // Send the email.
            sesClient.sendEmail(request);
        }

How it works…

In the first step, we verify our e-mail address. You have to verify the sender e-mail address before sending e-mails from that address in order to prove that you own it. If you have a list of e-mail addresses, then you can also verify your entire domain with SES.

We will receive a verification e-mail from AWS SES service, and after we click on the verification link, the process is completed. You can track the verification status using the AWS console.

Next, we install the AWS SDK, as the AWS Java SDK is required to access the Amazon SES API from our Java application.

You can use any SMTP libraries or AWS SDK to interact with AWS SES service. In our sample program, we use the AWS SDK to send an e-mail. At this time, AWS SES is available in N. Virginia, Oregon, and Ireland regions only. It is recommended to use the nearest AWS region to reduce network latency.

There's more…

You can send an e-mail using Amazon SES using the SES console, SES SMTP interface, or SES API. Typically, the SMTP and SES APIs are used to send bulk e-mails. You can also use the SMTP interface to integrate your existing e-mail server with SES. As a best practice, choose an SES endpoint in a region that is closest to your application.

When you send a message, Amazon SES constructs an e-mail message that consists of a header, a body, and an envelope that is as per the Internet Message Format specification (RFC 5322).

The Amazon SES account can regulate the number and rate at which you can send e-mails. This can protect from getting classified as a spammer and have your e-mails blocked by an ISP. The sending quota defines the maximum number of e-mails you can send in a given 24-hour period. Over time, both your sending limit and the rate are increased. If you try to send an e-mail after you have hit the sending limits, you will encounter a throttling error and your e-mail will be dropped.

Note

Excessive bounce rates and complaints by the e-mail recipients can lead to a reduction or termination of your SES account.

As a best practice, you should test out your e-mail functionality. Use the mailbox simulator provided by Amazon SES. The simulator is basically a set of test e-mail addresses that are used for testing your sending scenarios without impacting sending quota.

Note

Refer to the Amazon SES Developer Guide for best practices for sending e-mail using Amazon SES.

You can also use Amazon SES to receive e-mails. For receiving e-mails, handle all the underlying operations including communicating with other mail servers, scanning for spam, scanning for viruses, and rejecting e-mails from untrusted sources. It can also route the received e-mails to S3 buckets.

You can create IAM policies to specify the users permitted to perform SES actions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset