0

我正在尝试使用 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);

感谢您的帮助!!

4

1 回答 1

1

可以在此 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();

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

在此处输入图像描述

于 2021-06-17T12:33:22.627 回答