Sending Messages Using the Amazon SES Service Client in Java
SesClient from the com.aspose.email.services.ses.client package sends messages through the Amazon SES API version 2. The client connects over HTTP. To learn how to select an HTTP library, see HTTP Clients for Mail Services.
Sending a Message
To create a client, pass your AWS credentials and the region to the builder. If you do not set the region, the client uses us-east-1.
SesEmailRequest.simple prepares a message with a sender, recipients, a subject, an HTML body and a text body. You can pass null for one of the two bodies.
import com.aspose.email.services.ses.auth.AwsCredentials;
import com.aspose.email.services.ses.client.SesClient;
import com.aspose.email.services.ses.model.SesDestination;
import com.aspose.email.services.ses.model.SesEmailRequest;
import com.aspose.email.services.ses.model.SesSendResponse;
try (SesClient ses = SesClient.builder()
.credentials(new AwsCredentials("access-key-id", "secret-access-key"))
.region("eu-west-1")
.build()) {
SesEmailRequest request = SesEmailRequest.simple(
"from@example.com",
new SesDestination("to@example.com"),
"Weekly report",
"<p>The report is ready.</p>",
"The report is ready.");
SesSendResponse response = ses.email().send(request);
System.out.println(response.getMessageId());
}
Amazon SES accepts a message only if the sender is a verified identity in your account. Other code samples on this page use the ses variable created here.
Using Temporary Credentials
To use temporary security credentials, pass the session token as the third argument of AwsCredentials:
import com.aspose.email.services.ses.auth.AwsCredentials;
import com.aspose.email.services.ses.client.SesClient;
SesClient ses = SesClient.builder()
.credentials(new AwsCredentials("access-key-id", "secret-access-key", "session-token"))
.region("eu-west-1")
.build();
Setting Recipients and Other Properties
SesDestination accepts one or more recipients in its constructor and has methods for adding other recipients. The request also has properties for reply addresses and a configuration set:
import com.aspose.email.services.ses.model.SesDestination;
import com.aspose.email.services.ses.model.SesEmailRequest;
import java.util.Arrays;
SesDestination destination = new SesDestination("to@example.com", "to2@example.com")
.addCc("cc@example.com")
.addBcc("bcc@example.com");
SesEmailRequest request = SesEmailRequest.simple(
"from@example.com", destination, "Weekly report", null, "The report is ready.");
request.setReplyToAddresses(Arrays.asList("reply@example.com"));
request.setConfigurationSetName("my-configuration-set");
ses.email().send(request);
Adding Attachments
SesAttachment.fromBytes creates an attachment from the file data, the file name and the content type:
import com.aspose.email.services.ses.model.SesAttachment;
import com.aspose.email.services.ses.model.SesDestination;
import com.aspose.email.services.ses.model.SesEmailRequest;
import java.nio.file.Files;
import java.nio.file.Paths;
byte[] data = Files.readAllBytes(Paths.get("report.pdf"));
SesEmailRequest request = SesEmailRequest.simple(
"from@example.com", new SesDestination("to@example.com"),
"Weekly report", "<p>The report is attached.</p>", null);
request.addAttachment(SesAttachment.fromBytes(data, "report.pdf", "application/pdf"));
ses.email().send(request);
Sending a MIME Message
If you already have a complete message in the MIME format, use SesEmailRequest.raw. The sender and the subject come from the message itself, and addAttachment is not available for such a request:
import com.aspose.email.services.ses.model.SesDestination;
import com.aspose.email.services.ses.model.SesEmailRequest;
import java.nio.file.Files;
import java.nio.file.Paths;
byte[] mime = Files.readAllBytes(Paths.get("message.eml"));
ses.email().send(SesEmailRequest.raw(new SesDestination("to@example.com"), mime));
Handling Errors
When Amazon SES returns an error status, the client throws HttpClientException, which contains the status and the response body. The exception extends java.io.IOException, which the client also throws for connection failures.
import com.aspose.email.services.ses.auth.AwsCredentials;
import com.aspose.email.services.ses.client.SesClient;
import com.aspose.email.services.ses.model.SesDestination;
import com.aspose.email.services.ses.model.SesEmailRequest;
import com.aspose.ext.http.HttpClientException;
try (SesClient ses = SesClient.builder()
.credentials(new AwsCredentials("invalid-access-key-id", "secret-access-key"))
.build()) {
ses.email().send(SesEmailRequest.simple(
"from@example.com", new SesDestination("to@example.com"), "Weekly report", null, "The report is ready."));
} catch (HttpClientException e) {
System.out.println(e.getStatusCode() + ": " + e.getResponseBody());
}