使用 Java SDK v2 将配置集添加到 SES 电子邮件?

问题描述

我正在尝试使用 Java SDK 2 通过 AWS SES 发送电子邮件。 虽然我能够成功发送电子邮件。我想知道如何在 Java SDK 2 中发送电子邮件时设置配置集。

我正在尝试遵循与 AWS 文档中提供的代码类似的代码,但它没有指定如何将配置集添加到电子邮件请求对象。


    /* 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);

需要帮助将配置集添加rawEmailRequest

AWS Java SDK 1 指定使用代码示例中的配置集。 (下)

  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);

谢谢你的帮助!!

解决方法

可以在此处的 Javadoc 中找到此问题的解决方案:

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

您可以在创建 SendRawEmailRequest 对象时使用 overrideConfiguration

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

有关 AwsRequestOverrideConfiguration 对象的更多信息,请参阅:

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

根据评论更新

以下是创建 AwsRequestOverrideConfiguration 对象的示例。在本例中,我们可以设置 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();

我刚刚对此进行了测试并成功发送了电子邮件:

enter image description here