簡體   English   中英

為什么 AWS SDK for Java v2 不允許我發送大於 10 MB 的電子郵件?

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

我想利用自 2021 年 9 月以來將 ​​SES 發送限制從 10MB 增加到現在的 40MB,以將更大的 Excel 文件作為電子郵件附件發送。

我使用了官方代碼示例,但不幸的是,我的大小不能超過 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 電子郵件大小限制的錯誤?

雖然從語法的角度來看,該示例是正確的,但它實際上使用的是v1 SES 客戶端而不是v2 SES 客戶端,這有點誤導,您不會期望使用v2示例。 我已提交反饋請求以糾正此問題。

AWS SDK for Java 2.x 通常按software.amazon.awssdk的組ID 分類,而AWS SDK for Java 1.x 通常按com.amazonaws的組ID 分類,如您在文檔中所見。

在這種情況下,v2 SDK 中的 SES 客戶端不是一個,而是兩個 這可能是因為向后兼容。

  1. SesClient - software.amazon.awssdk.services.ses - 調用 SES v1 API - 限制為 10 MB
  2. SesV2Client - software.amazon.awssdk.services.sesv2 - 調用 SES v2 API - 允許 40 MB

該示例(以及您)正在使用 v1 客戶端,因此請改用SesV2Client您應該能夠發送大於 10 MB 的電子郵件。

(您可能需要一些語法調整,但核心邏輯將保持不變)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM