Send Email with File Attachment Using AWS SES and Java: 2026 Guide

Learn how to send emails with file attachments using AWS SES and Java. Convert data into CSV files, attach them to emails, and send reliably.

Send Email with File Attachment Using AWS SES and Java: 2026 Guide

Send Email with File Attachment Using AWS SES and Java: 2026 Guide

Sending emails programmatically with attachments can be a crucial feature in many applications, especially when dealing with reports or data exports. AWS Simple Email Service (SES) offers a scalable and cost-effective way to send emails. In this tutorial, we will explore how to send an email with a file attachment using AWS SES and Java. By the end of this guide, you'll be equipped with the knowledge to implement this functionality in your own Java applications.

Key Takeaways

  • Learn to create and send emails with attachments using AWS SES in Java.
  • Understand how to convert data into a CSV format and attach it to an email.
  • Get familiar with AWS SES's integration with Java SDK version 2.x.
  • Troubleshoot common issues when sending emails with attachments.

Introduction

In today's digital age, automated emails are a fundamental feature for many applications, whether it's for sending newsletters, notifications, or data reports. AWS SES is a reliable service that allows you to send emails from your applications easily. In this guide, we will demonstrate how to send an email with a file attachment using AWS SES and Java, focusing on converting data into a CSV file, attaching it to an email, and sending it through SES.

This process is vital for applications that need to share data or reports automatically. By leveraging AWS SES, you can ensure that your emails are delivered reliably and efficiently, while Java provides a robust environment for handling the data processing and email construction.

Prerequisites

  • Basic understanding of Java programming.
  • An AWS account with SES enabled and a verified email address/domain.
  • Java Development Kit (JDK) 11 or higher installed.
  • Maven for managing Java dependencies.
  • AWS SDK for Java version 2.x.

Step 1: Set Up Your AWS SES Environment

Before you can send emails using AWS SES, you need to set up your AWS environment.

1.1 Verify Email Addresses or Domains

Log in to your AWS Management Console, navigate to SES, and verify the email addresses or domains you plan to send emails from. This is a security measure to prevent unauthorized use of your email addresses.

1.2 Configure AWS Credentials

Ensure your AWS credentials are configured. You can use the AWS CLI to configure your credentials or manually create a ~/.aws/credentials file:

[default]
aws_access_key_id=YOUR_ACCESS_KEY
aws_secret_access_key=YOUR_SECRET_KEY

1.3 Set SES to Production Mode

By default, SES is in sandbox mode, which restricts sending emails to verified addresses. Request AWS to move your account to production mode to lift this restriction.

Step 2: Create a Maven Project

Create a new Maven project in your preferred IDE (like IntelliJ IDEA or Eclipse). Add the AWS SDK for Java dependency in your pom.xml:


<dependency>
  <groupId>software.amazon.awssdk</groupId>
  <artifactId>ses</artifactId>
  <version>2.20.0</version> 
</dependency>

Step 3: Implement CSV File Creation

Let's assume you have a list of data that you need to convert into a CSV file. We'll use the Apache Commons CSV library to facilitate this process. Add the dependency to your pom.xml:


<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-csv</artifactId>
  <version>1.10.0</version> 
</dependency>

Here's a simple example of how to create a CSV file from a list of data:


import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class CsvCreator {
    public static void createCsvFile(String filePath) throws IOException {
        List<String[]> data = Arrays.asList(
            new String[]{"Name", "Age", "Email"},
            new String[]{"John Doe", "30", "john.doe@example.com"},
            new String[]{"Jane Smith", "25", "jane.smith@example.com"}
        );

        try (FileWriter out = new FileWriter(filePath);
             CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT)) {
            for (String[] record : data) {
                printer.printRecord(record);
            }
        }
    }
}

This code will generate a CSV file named abc.csv with sample data.

Step 4: Send Email with Attachment Using AWS SES

Now that we have the CSV file, we can proceed to send it as an attachment in an email using AWS SES. We'll use the JavaMail API to construct the email message.

4.1 Add JavaMail Dependency


<dependency>
  <groupId>com.sun.mail</groupId>
  <artifactId>javax.mail</artifactId>
  <version>1.6.2</version>
</dependency>

4.2 Construct the Email

Here's how to construct the email with an attachment:


import software.amazon.awssdk.services.ses.SesClient;
import software.amazon.awssdk.services.ses.model.*;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.util.Properties;

public class SesEmailSender {
    public static void sendEmailWithAttachment(String filePath) {
        String from = "verified-sender@example.com";
        String to = "recipient@example.com";
        String subject = "Subject: CSV Report";
        String bodyText = "Please find the attached CSV report.";

        try {
            Properties props = System.getProperties();
            Session session = Session.getDefaultInstance(props);

            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);

            Multipart multipart = new MimeMultipart();

            // Body part
            MimeBodyPart textPart = new MimeBodyPart();
            textPart.setText(bodyText);
            multipart.addBodyPart(textPart);

            // Attachment part
            MimeBodyPart attachmentPart = new MimeBodyPart();
            DataSource source = new FileDataSource(new File(filePath));
            attachmentPart.setDataHandler(new DataHandler(source));
            attachmentPart.setFileName("report.csv");
            multipart.addBodyPart(attachmentPart);

            message.setContent(multipart);

            // Send email
            SesClient sesClient = SesClient.builder().build();
            SendRawEmailRequest rawEmailRequest = SendRawEmailRequest.builder()
                .rawMessage(RawMessage.builder().data(SdkBytes.fromByteArray(message.toString().getBytes())).build())
                .build();
            sesClient.sendRawEmail(rawEmailRequest);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This code sets up an email with a CSV attachment and sends it using AWS SES.

Common Errors/Troubleshooting

  • Invalid Email Address: Ensure all email addresses are verified in SES.
  • Missing Permissions: Check IAM policies for SES permissions.
  • Sandbox Mode: Move to production to send to unverified addresses.
  • Attachment Issues: Verify the file path and format are correct.

Conclusion

By following this guide, you can now send emails with file attachments using AWS SES and Java. This functionality is crucial for applications that need to automate data sharing and reporting. Remember to handle exceptions and validate input data to ensure smooth operation.

Frequently Asked Questions

Can I send emails to unverified addresses using AWS SES?

Yes, but you need to request AWS to move your account out of the SES sandbox mode to send emails to unverified addresses.

What are the limits on attachments size in AWS SES?

AWS SES allows attachments of up to 10 MB in size. Ensure your email, including attachments, stays within this limit.

How can I handle bounced emails in AWS SES?

Configure bounce and complaint notifications in the AWS SES console to monitor and manage bounced emails effectively.