Skip to content

AWS SES V1 -> V2 Java SDK migration

0

During a migration of AWS SES to V2, I stumbled upon missing sourceArn and fromArn fields in the Message object (in v1 SendRawEmailRequest)

I couldn't find any related documentation to this.

How would this v1 java SDK example look like in v2?

sesClient.sendRawEmail(SendRawEmailRequest.builder().sourceArn(sourceArn).fromArn(fromArn).build())

3 Answers
1
Accepted Answer

In SES V2, the sourceArn and fromArn are replaced by fromEmailAddressIdentityArn. Here's how to migrate the code:

// V1 Code
sesClient.sendRawEmail(SendRawEmailRequest.builder()
    .sourceArn(sourceArn)
    .fromArn(fromArn)
    .build())

// V2 Code
SesV2Client sesv2Client = SesV2Client.builder()
    .region(Region.US_EAST_1)
    .build();

SendEmailRequest emailRequest = SendEmailRequest.builder()
    .fromEmailAddress(sender)  // The actual email address
    .fromEmailAddressIdentityArn(identityArn)  // This replaces both sourceArn and fromArn
    .content(EmailContent.builder()
        .raw(RawMessage.builder()
            .data(SdkBytes.fromByteArray(rawMessage))
            .build())
        .build())
    .destination(Destination.builder()
        .toAddresses(recipient)
        .build())
    .build();

sesv2Client.sendEmail(emailRequest);

Key Changes Explained:

  1. The sourceArn and fromArn are consolidated into a single parameter called fromEmailAddressIdentityArn

  2. From the AWS SDK documentation:

fromEmailAddressIdentityArn - This parameter is used only for sending authorization. It is the ARN of the identity that is associated with the sending authorization policy that permits you to use the email address specified in the FromEmailAddress parameter.
  1. Example usage with authorization:
// If you have an identity "example.com" with ARN:
// arn:aws:ses:us-east-1:123456789012:identity/example.com
// And you want to send from sender@example.com

SendEmailRequest request = SendEmailRequest.builder()
    .fromEmailAddress("sender@example.com")
    .fromEmailAddressIdentityArn("arn:aws:ses:us-east-1:123456789012:identity/example.com")
    // ... other parameters
    .build();

Important Notes:

  1. For raw emails, the fromEmailAddressIdentityArn value overrides any X-SES-SOURCE-ARN and X-SES-FROM-ARN headers specified in the raw email message content.

  2. The fromEmailAddress must be verified and associated with the identity specified in fromEmailAddressIdentityArn.

Sources:

  1. AWS SDK for Java 2.x API Reference - SendEmailRequest.Builder:
This parameter is used only for sending authorization. It is the ARN of the identity 
that is associated with the sending authorization policy that permits you to use 
the email address specified in the FromEmailAddress parameter.
  1. From the provided AWS documentation examples showing the proper structure for sending emails using SES V2:
SendEmailRequest emailRequest = SendEmailRequest.builder()
    .destination(destination)
    .content(emailContent)
    .fromEmailAddress(sender)
    .build();

The solution maintains the same functionality as V1 while adapting to V2's simplified authorization model where source and from ARNs are unified under a single identity ARN parameter.

AWS
EXPERT

answered 2 years ago

EXPERT

reviewed a year ago

-1

When migrating from AWS SES V1 to V2 using the Java SDK, there are some changes in how you construct the SendRawEmailRequest. In V2, the sourceArn and fromArn parameters are not directly available in the Message object. Instead, these parameters are now part of the SendRawEmailRequest builder itself.

Here's how you would construct a similar request using the SES V2 Java SDK:

SendRawEmailRequest request = SendRawEmailRequest.builder()
    .sourceArn(sourceArn)
    .fromArn(fromArn)
    .rawMessage(RawMessage.builder()
        .data(SdkBytes.fromByteBuffer(/* your raw email data */))
        .build())
    .build();

sesv2Client.sendRawEmail(request);

In this V2 example:

  1. We use the SendRawEmailRequest.Builder to construct our request.
  2. The sourceArn and fromArn are set directly on the request builder.
  3. The raw email content is wrapped in a RawMessage object, which is then set on the request.
  4. We use a SesV2Client instead of the V1 AmazonSimpleEmailService client.

It's worth noting that in V2, these Arn parameters are used for sending authorization. The sourceArn is the ARN of the identity associated with the sending authorization policy that permits you to send for the specified email address. The fromArn serves a similar purpose but for the "From" address in the header of the raw email.

If you prefer, you can also use X-headers in your raw email message instead of these parameters. For sourceArn, you can use the X-SES-SOURCE-ARN header, and for fromArn, you can use the X-SES-FROM-ARN header. If you use both the parameter and the corresponding X-header, Amazon SES will use the value of the parameter.

Remember to update your IAM policies to include the new and modified API actions introduced in SESv2 before migrating your code.

Sources
Upgrade Your Email Tech Stack with Amazon SESv2 API | AWS Messaging & Targeting Blog
SendRawEmail - Amazon Simple Email Service
SendRawEmailRequest.Builder (AWS SDK for Java - 2.21.29)

answered 2 years ago

EXPERT

reviewed 2 years ago

-1

In AWS SDK v2, the structure for sending raw emails has changed. The sourceArn and fromArn fields are now handled differently through the SendEmailRequest builder.

Here's how to migrate the v1 code to v2:

// V2 SDK Implementation
SesV2Client sesV2Client = SesV2Client.builder()
    .region(Region.US_EAST_1)
    .build();

SendEmailRequest request = SendEmailRequest.builder()
    .fromEmailAddress(fromAddress)
    .destination(Destination.builder()
        .toAddresses(toAddress)
        .build())
    .content(EmailContent.builder()
        .raw(RawMessage.builder()
            .data(SdkBytes.fromByteArray(rawMessage))
            .build())
        .build())
    .build();

SendEmailResponse response = sesV2Client.sendEmail(request);

Sources:

  1. https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/sesv2/SesV2Client.html#sendEmail(software.amazon.awssdk.services.sesv2.model.SendEmailRequest)

  2. https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/sesv2/model/SendEmailRequest.Builder.html

AWS
EXPERT

answered 2 years ago

  • what does it mean "handled differently"?

    in your example I don't see it. were sourceArn and fromArn deleted without replacement?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.