0

I am trying to send an email through AWS SES using Java SDK 2. Although I am able to send the email successfully. I wanted to know how to set the Configuration Set while sending out the email in Java SDK 2.

I am trying to follow the code similar to the one provided in the AWS documentation but it does not specify how to add the configuration set to the email request object.


    /* EMAIL MESSAGE BODY SETUP */
            System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");

             ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
             message.writeTo(outputStream);
             ByteBuffer buf = ByteBuffer.wrap(outputStream.toByteArray());

             byte[] arr = new byte[buf.remaining()];
             buf.get(arr);

             SdkBytes data = SdkBytes.fromByteArray(arr);
             RawMessage rawMessage = RawMessage.builder()
                    .data(data)
                    .build();

             SendRawEmailRequest rawEmailRequest = SendRawEmailRequest.builder()
                    .rawMessage(rawMessage)
                    .build();

             client.sendRawEmail(rawEmailRequest);

Wanted help with adding the configuration set to the rawEmailRequest

AWS Java SDK 1 specifies the use of configuration set in the code example. (Below)

  AmazonSimpleEmailService client = 
          AmazonSimpleEmailServiceClientBuilder.standard()
          // Replace US_WEST_2 with the AWS Region you're using for
          // Amazon SES.
            .withRegion(Regions.US_WEST_2).build();
      SendEmailRequest request = new SendEmailRequest()
          .withDestination(
              new Destination().withToAddresses(TO))
          .withMessage(new Message()
              .withBody(new Body()
                  .withHtml(new Content()
                      .withCharset("UTF-8").withData(HTMLBODY))
                  .withText(new Content()
                      .withCharset("UTF-8").withData(TEXTBODY)))
              .withSubject(new Content()
                  .withCharset("UTF-8").withData(SUBJECT)))
          .withSource(FROM)
          // Comment or remove the next line if you are not using a
          // configuration set
          .withConfigurationSetName(CONFIGSET);
      client.sendEmail(request);

Thank you for the help!!

Akshat Tulsani
  • 101
  • 1
  • 8

1 Answers1

1

The solution to this can be found in this Javadoc here:

http://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/ses/model/SendRawEmailRequest.Builder.html#overrideConfiguration-software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration-

You can use overrideConfiguration when creating a SendRawEmailRequest object:

     SendRawEmailRequest rawEmailRequest = SendRawEmailRequest.builder()
            .rawMessage(rawMessage)
             .overrideConfiguration(<Set AwsRequestOverrideConfiguration Object>)
            .build();

For more information about an AwsRequestOverrideConfiguration object, see:

http://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/awscore/AwsRequestOverrideConfiguration.html

Update based on comment:

Here is an example of creating an AwsRequestOverrideConfiguration object. In this example, we can set the credentialsProvider.

 AwsCredentialsProvider credentialsProvider = new AwsCredentialsProvider() {
                @Override
                public AwsCredentials resolveCredentials() {
                    return null;
                }
            };

            AwsRequestOverrideConfiguration myConf = AwsRequestOverrideConfiguration.builder()
                    .credentialsProvider((AwsCredentialsProvider) credentialsProvider.resolveCredentials())
                    .build() ;

             SendRawEmailRequest rawEmailRequest = SendRawEmailRequest.builder()
                    .rawMessage(rawMessage)
                     .overrideConfiguration(myConf)
                    .build();

I just tested this and the email was sent successfully:

enter image description here

smac2020
  • 9,637
  • 4
  • 24
  • 38
  • Thank you for the help !! Just another thing, since I am new to Java, I am still confused about how to set the `AwsRequestOverrideConfiguration` object! Unable to understand the ideal function to use while doing `AwsRequestOverrideConfiguration configset = AwsRequestOverrideConfiguration.builder().{function_to_add_configset( String)}.build()` I have my configuration in String, how to add the same? – Akshat Tulsani Jun 17 '21 at 13:07
  • I am looking to provide my AWS credentials explicitly. which I am able to do and send out the emails, but still facing issues while setting up the configuration set explicitly. – Akshat Tulsani Jun 17 '21 at 14:55
  • When you use the AwsRequestOverrideConfiguration - what issue are you seeing – smac2020 Jun 17 '21 at 14:58
  • Thank you so much for your help @smach2020 ! I am looking to provide my AWS credentials explicitly, which I am able to do and send out the emails, but still facing issues while setting up the configuration set explicitly. My configuration set initially in Java Sdk1 was `String confSet= "value_for_configSet"`. which was provided explicitly attached to the email request object. Looking for a solution where I can attach the confSet variable explicitly in Sdk 2 upgrade along with explicit credentials. (Since it different envs trigger different SNS based on the Configuration Set) – Akshat Tulsani Jun 17 '21 at 15:01
  • Maybe I am doing something wrong, Just trying to figure out a way to provide a specific configuration set explicitly while sending out the email. – Akshat Tulsani Jun 17 '21 at 15:08