【问题标题】:Why is the AWS SDK for Java v2 not letting me send emails larger than 10 MB?为什么 AWS SDK for Java v2 不允许我发送大于 10 MB 的电子邮件?
【发布时间】:2022-05-25 01:06:03
【问题描述】:

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

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

我得到错误:

消息长度超过 10485760 字节长: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 电子邮件大小限制的错误?

  • 您多久发送一次这些电子邮件?这是至少每秒一条 40MB 的消息,还是每秒发送超过一条 40MB 的消息? AWS SES 将带宽限制在 10MB 以上。
  • @ErmiyaEskandary 它在测试环境中,只发送了 1 封电子邮件,但它仍然出现。是的,我联系了支持人员,但它似乎是 40MB 的默认报价,没有必要或不可能提出增加限制的请求。他们提到“要实现发送 40MB 大小的电子邮件的目标,您需要使用 SES v2 API 或 SMTP。”我相信这已经通过使用“software.amazon.awssdk:ses:2.17”来实现。 196\"
  • 啊哈!使用SesV2Client 而不是SesClient。那样有用吗? V1 客户端的硬限制为 10MB。 V2V1 客户端都存在于 V2 SDK 中,我不得不承认,这令人困惑。让我知道这是否有效,应该对语法进行一些小的调整。
  • @ErmiyaEskandary 就像你说的那样。我必须使用单独的包 \"software.amazon.awssdk:sesv2:2.17.196\" 才能访问 SesV2Client 文件。对代码进行了一些小的调整,现在它可以工作了。非常感谢你!
  • 非常欢迎,很高兴你让它工作。

标签: java amazon-web-services amazon-ses aws-sdk-java-2.0 amazon-simple-email-service


【解决方案1】:

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

AWS SDK for Java 2.x 通常按组 ID software.amazon.awssdk 分类,而 AWS SDK for Java 1.x 通常按组 ID com.amazonaws 分类,如您在docs 中所见。

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

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

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

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

【讨论】:

    猜你喜欢
    • 2011-03-07
    • 2022-07-17
    • 2019-01-24
    • 2016-03-24
    • 1970-01-01
    • 2016-05-02
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多