How to Email with Send Attachment using Amazon SES in Java Spring Boot?
In this example tutorial, you will learn how to send a plain-text or HTML-based email with attachment using Amazon SES in Java Spring Boot.
Follow the steps below to send an HTML/plain-text based email with attachment using AWS SES in Java Spring Boot:
Setup SES
- Login to the AWS Management Console and open the AWS SES console at https://console.aws.amazon.com/ses.
- Create identity.
- Verify your email address with Amazon SES.
- Get your AWS credentials.
Add Dependencies
Add Java Mail Sender and Amazon SES Java SDK dependencies to your project.
For Gradle
Add the following dependency to your build.gradle file:
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation group: 'com.amazonaws', name: 'aws-java-sdk-ses', version: '1.12.12'
For Maven
Add the following dependency to your pom.xml file:
Find the other versions of AWS Java SDK for Amazon SES in the AWS SES Maven Repository.
Add Configuration Class
Create a AwsConfiguration.java class and annotate it with @Configuration. Add a method that returns an instance of AmazonSimpleEmailService class and annotate it with @Bean annotation. The following code shows how to do so:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder;
@Configuration
public class AwsConfiguration {
public AWSStaticCredentialsProvider awsCredentials() {
BasicAWSCredentials credentials =
new BasicAWSCredentials("your-aws-access-key", "your-aws-access-secret");
return new AWSStaticCredentialsProvider(credentials);
}
@Bean
public AmazonSimpleEmailService getAmazonSimpleEmailService() {
return AmazonSimpleEmailServiceClientBuilder.standard().withCredentials(awsCredentials())
.withRegion("us-east-1").build();
}
}
Send Email with Attachment
The following code shows you how to send email using Amazon SES in Java:
Note: HTML based email content only support inline style css.
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message.RecipientType;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import com.amazonaws.services.simpleemail.model.RawMessage;
import com.amazonaws.services.simpleemail.model.SendRawEmailRequest;
@ Component
public class AwsSesExample {
@Autowired
public AmazonSimpleEmailService amazonSimpleEmailService;
public void sendEmailWithAttachment() {
Session session = Session.getInstance(new Properties(System.getProperties()));
MimeMessage mimeMessage = new MimeMessage(session);
String emailContent = "<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"utf-8\">\n" +
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n" +
" <title>Example HTML Email with Attachment</title>\n" +
"</head>\n" +
"<body style=\"background: whitesmoke; padding: 30px; height: 100%\">\n" +
"<h5 style=\"font-size: 18px; margin-bottom: 6px\">Dear example,</h5>\n" +
"<p style=\"font-size: 16px; font-weight: 500\">Greetings from TutorialsBuddy</p>\n" +
"<p>This is a simple html based email with attachment.</p>\n" +
"</body>\n" +
"</html>";
String filePath = "C:\\Users\\documents\\java-tutorials.pdf";
String fileName = "java-tutorials.pdf";
try {
mimeMessage.setSubject("Test Email", "UTF-8");
mimeMessage.setFrom("[email protected]");
mimeMessage.setRecipients(RecipientType.TO, "[email protected]");
MimeMultipart msgBody = new MimeMultipart("alternative");
MimeBodyPart wrap = new MimeBodyPart();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(emailContent, "text/html; charset=UTF-8");
msgBody.addBodyPart(htmlPart);
wrap.setContent(msgBody);
MimeMultipart msg = new MimeMultipart("mixed");
mimeMessage.setContent(msg);
msg.addBodyPart(wrap);
MimeBodyPart messageBodyPart = new MimeBodyPart();
// Attachment pdf file
DataSource source = new FileDataSource(filePath);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
msg.addBodyPart(messageBodyPart);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
mimeMessage.writeTo(outputStream);
RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));
SendRawEmailRequest rawEmailRequest = new SendRawEmailRequest(rawMessage);
amazonSimpleEmailService.sendRawEmail(rawEmailRequest);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}