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

Time:05-25

I would like to make use of the increased SES sending limit from 10MB to now up to 40MB since September 2021 to send larger Excel files as email attachment.

I used an official code example but unfortunately, I can't go beyond 10MB in size.

I get the error:

Message length is more than 10485760 bytes long: 12148767

I am using the newest version of software.amazon.awssdk:ses which is 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");
}

Any ideas why I still get an error regarding a 10 MB email message size limit?

CodePudding user response:

While the sample is correct from a syntax perspective, it's slightly misleading in that it's actually using the v1 SES client & not the v2 SES client, which you wouldn't expect using a v2 sample. I have submitted a feedback request to rectify this.

The AWS SDK for Java 2.x is normally categorised by the group ID of software.amazon.awssdk while the AWS SDK for Java 1.x is normally categorised by the group ID of com.amazonaws as you can see here in the docs.

In this case, there are not one, but two SES clients in the v2 SDK. This is probably because of backwards compatibility.

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

The sample (& thus you) are using the v1 client so instead, use SesV2Client & you should be able to send email messages larger than 10 MB.

(you might need a few syntactical tweaks but the core logic will remain the same)

  • Related