2

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?

Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44
MJey
  • 345
  • 3
  • 16
  • How often are you sending these emails? Is this one 40MB message per second at the very least or are you sending more than one 40MB message per second? AWS SES limits bandwidth beyond 10MB. – Ermiya Eskandary May 24 '22 at 12:36
  • @ErmiyaEskandary it's in a test environment, only 1 email is send, but it still appears. Yes I went into contact with the support, but it seems to be a default quote of 40MB, there is no need or possibility for an limit increase request on that. They mention "To achieve your goal of sending emails which are 40MB in size, you will need to use SES v2 API or SMTP." which I believe am doing already by using "software.amazon.awssdk:ses:2.17.196" – MJey May 24 '22 at 13:39
  • 1
    Aha! Use `SesV2Client` instead of `SesClient`. Does that work? The V1 client has a hard limit of 10MB. [V2](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/sesv2/SesV2Client.html) *and* [V1](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/ses/SesClient.html) clients both exist alongside in the V2 SDK which I have to admit, is confusing. Let me know if that works, should have some minor tweaks in syntax. – Ermiya Eskandary May 24 '22 at 13:48
  • @ErmiyaEskandary it was exactly as you said. I had to use the separate package "software.amazon.awssdk:sesv2:2.17.196" to be able to access the SesV2Client files. A few minor adjustments to the code and now it works. Many thanks to you! – MJey May 24 '22 at 15:48
  • More than welcome, glad you got it working. – Ermiya Eskandary May 24 '22 at 16:57

1 Answers1

4

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)

Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44