为什么适用于 Java v2 的 AWS 开发工具包不允许我发送大于 10 MB 的电子邮件?

Why is the AWS SDK for Java v2 not letting me send emails larger than 10 MB?

我想利用增加的 SES 发送限制从 10MB 到现在最多 40MBsince September 2021 来发送更大的 Excel 文件作为电子邮件附件。

我使用了 official code example 但不幸的是,我不能超过 10MB。

我收到错误:

Message length is more than 10485760 bytes long: 12148767

我正在使用 software.amazon.awssdk:ses 的最新版本,即 2.17.196。

  static Region region = Region.EU_CENTRAL_1;
  static SesClient client = SesClient.builder().region(region).build();  
      
  public static void sendemailAttachment(SesClient client,
                                           String sender,
                                           String recipient,
                                           String subject,
                                           String bodyText,
                                           String bodyHTML,
                                           String fileName, // must include .xlsx
                                           String fileLocation) throws AddressException, MessagingException, IOException {

    java.io.File theFile = new java.io.File(fileLocation);
    byte[] fileContent = Files.readAllBytes(theFile.toPath());

    Session session = Session.getDefaultInstance(new Properties());

    // Create a new MimeMessage object 

    MimeMessage message = new MimeMessage(session);

    // Add subject, from and to lines
    message.setSubject(subject, "UTF-8");
    message.setFrom(new InternetAddress(sender));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));

    // Create a multipart/alternative child container
    MimeMultipart msgBody = new MimeMultipart("alternative");

    // Create a wrapper for the HTML and text parts
    MimeBodyPart wrap = new MimeBodyPart();

    // Define the text part
    MimeBodyPart textPart = new MimeBodyPart();
    textPart.setContent(bodyText, "text/plain; charset=UTF-8");

    // Define the HTML part
    MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setContent(bodyHTML, "text/html; charset=UTF-8");

    // Add the text and HTML parts to the child container
    msgBody.addBodyPart(textPart);
    msgBody.addBodyPart(htmlPart);

    // Add the child container to the wrapper object
    wrap.setContent(msgBody);

    // Create a multipart/mixed parent container
    MimeMultipart msg = new MimeMultipart("mixed");

    // Add the parent container to the message
    message.setContent(msg);

    // Add the multipart/alternative part to the message
    msg.addBodyPart(wrap);

    // Define the attachment
    MimeBodyPart att = new MimeBodyPart();
    DataSource fds = new ByteArrayDataSource(fileContent, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    att.setDataHandler(new DataHandler(fds));

    String reportName = fileName; // include .xlsx
    att.setFileName(reportName);

    // Add the attachment to the message.
    msg.addBodyPart(att);

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

    } catch (SesException e) {
        System.err.println(e.awsErrorDetails().errorMessage()); // <--
        System.exit(1);
    }
    System.out.println("Email sent with attachment");
}

知道为什么我仍然收到有关 10 MB 电子邮件大小限制的错误消息吗?

虽然从语法的角度来看样本是正确的,但它有点误导,因为它实际上使用的是 v1 SES 客户端而不是 v2 SES 客户端,您不会期望使用 v2 示例。我已提交反馈请求以纠正此问题。

Java 2.x 的 AWS SDK 通常按组 ID software.amazon.awssdk 分类,而 Java 1.x 的 AWS SDK 通常按组 ID 分类通过 com.amazonaws 的组 ID,如您在 docs.

中所见

在这种情况下,v2 SDK 中没有一个,而是两个 个SES 客户端。这可能是因为向后兼容。

  1. SesClient - software.amazon.awssdk.services.ses - calls SES v1 API - limited to 10 MB
  2. SesV2Client - software.amazon.awssdk.services.sesv2 - 调用 SES v2 API - 允许 40 MB

示例(以及您)正在使用 v1 客户端,因此请改用 SesV2Client,您应该能够发送大于 10 MB 的电子邮件。

(您可能需要一些语法调整,但核心逻辑将保持不变)