Using the Amazon SES SMTP Interface to Send Email
To send email using the Amazon SES SMTP interface, you will need the following:
An AWS account. For more information, see Signing up for AWS.
The SMTP interface hostname and port number. The hostname is email-smtp.us-east-1.amazonaws.com.
The port number varies with the connection method. For more information, see Connecting to the Amazon SES SMTP Endpoint.
An SMTP user name and password.
import java.io.IOException;
import com.amazonaws.services.simpleemail.;
import com.amazonaws.services.simpleemail.model.;
import com.amazonaws.auth.PropertiesCredentials;
public class amazon_ses_sdk_test {
static final String FROM = "SENDER@FINDNERD.COM"; // Replace with your "From" address. This address must be verified.
static final String TO = "RECIPIENT@FINDNERD.COM"; // Replace with a "To" address. If you have not yet requested
// production access, this address must be verified.
static final String BODY = "This email was sent through Amazon SES by using the AWS SDK for Java.";
static final String SUBJECT = "Amazon SES test (AWS SDK for Java)";
public static void main(String[] args) throws IOException {
// Your AWS credentials are stored in the AwsCredentials.properties file within the project.
// You entered these AWS credentials when you created a new AWS Java project in Eclipse.
PropertiesCredentials credentials = new PropertiesCredentials(
amazon_ses_sdk_test.class
.getResourceAsStream("AwsCredentials.properties"));
// Retrieve the AWS Access Key ID and Secret Key from AwsCredentials.properties.
credentials.getAWSAccessKeyId();
credentials.getAWSSecretKey();
// 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);
try
{
System.out.println("Attempting to send an email through Amazon SES by using the AWS SDK for Java...");
// Instantiate an Amazon SES client, which will make the service call with the supplied AWS credentials.
AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(credentials);
// 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());
ex.printStackTrace();
}
}
}
0 Comment(s)