Building Your Own Email Marketing Application with Spring Boot
Written on
Chapter 1: Introduction to the Project
In this tutorial, we will develop a Spring Boot (3) application designed to send emails and monitor user engagement, specifically tracking whether recipients have opened the emails. Additionally, the app will check for any errors encountered during the email dispatch process.
If you're looking to enhance this application with a front-end interface, consider utilizing a rich text editor based on Angular 14, which will complement our project well.
For those familiar with sending newsletters via various platforms, you likely have reviewed analytics to understand how many people actually opened your emails. This process, known as analytics, is a standard feature in most email marketing tools. Our aim is to construct a similar system that can send bulk emails or serve as a fun experiment.
What do you need to get started?
- A computer (obviously).
- JDK (This application utilizes OpenJDK 17).
- Redis (Feel free to choose any database, but remember to update the database configuration in the code if using a different one).
For Redis installation, refer to the official installation guide for local setup. The spring-boot-starter-data-redis dependency uses Lettuce by default, so we will configure LettuceConnectionFactory instead of jedisconnectionfactory in our database configuration file.
Suggestions — If you’re still using Java 8 or older versions, consider upgrading to version 17 or later to experience the evolution of the language. Check this article for details on new features introduced from Java 8 to 17.
In our application, we've utilized records instead of traditional classes for creating request and response objects. As Java EE transitioned to Jakarta EE, you will notice that the JavaMail API is now referred to as the Jakarta Mail API:
import jakarta.mail.internet.InternetAddress;
Chapter 2: Sending Emails
To send an email, we can programmatically connect to an SMTP server, using our credentials to authenticate and dispatch the email. Just as we use libraries to make HTTP requests, there are libraries designed to manage the complexities of establishing a secure connection with an SMTP server and sending emails.
For users of older Java versions, the mail libraries are part of the JavaMail API, while in the latest versions, this has been rebranded as the Jakarta Mail API.
Here’s an example code snippet in Java that demonstrates how to populate email addresses and send emails:
Properties prop = new Properties();
// Populate SMTP server details here
Session session = Session.getInstance(prop, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("email-id", "pwd");}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from-email-id"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to-email-id"));
message.setSubject("email subject");
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent("email body", "text/html; charset=utf-8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);
message.setContent(multipart);
Transport.send(message);
Chapter 3: Tracking Email Engagement
To implement email tracking, one effective method is to include a specific header when sending the email, prompting the email client to send a read receipt upon opening the message. However, this approach has its limitations as clients can choose not to send receipts, and certain email clients, like Gmail, often do not provide this feature back to the sender.
An alternative and more reliable approach is to insert a unique tracking pixel within the email body. This involves embedding a 1px by 1px image tag and calling an API when the email client attempts to retrieve that image.
In the code below, we add a unique identifier for each email and set it as the source of the image:
String imgUrl = "http://localhost:8080/tracker/" + pixelId;
String formattedContent = String.format("<html><body>%1s<img src="" + imgUrl + "" width="1" height="1"></body>n</html>", request.content());
When the email client downloads this image, it triggers our API along with the unique tracking ID, which we can then store in our database for analytics.
Where can I find the complete code?
The full code for this application is available on GitHub. Please refer to the README for instructions on setting up and running the application. If you have any questions, feel free to leave a comment. If you enjoy this app, please consider giving a star to the repository.
This video titled "Build your own Email marketing system" walks you through the steps needed to create your own email marketing system, offering insights into practical implementations.
In this video, "Learn How To Build Your Own Email Marketing Software," you will discover various strategies for developing robust email marketing software tailored to your needs.