Apache camel mail with Spring-Boot

How to send e-mails from a spring-boot-3 application using Apache camel-4

Vimukthi Mayadunne
4 min readMay 1, 2023

Sending emails from a Spring Boot application is a crucial aspect of many modern software systems. Fortunately, there are several ways to achieve this task, each with its unique advantages and disadvantages. Let’s explore some of the most popular ways!

  1. Using JavaMail API: JavaMail API is a standard Java API used to send and receive email messages. Spring Boot provides support for JavaMail API through its spring-boot-starter-mail module.
  2. Using Spring Email abstraction: Spring provides an Email abstraction that allows you to send emails using a simplified API. This abstraction is built on top of JavaMail API and provides a more user-friendly interface to configure and send emails.
  3. Using third-party libraries: There are many third-party libraries available that can be used to send emails from a Spring Boot application. Some of the popular libraries are Apache Commons Email, Spring Cloud Stream, and SendGrid. These libraries provide additional features such as email templates, attachments, and email tracking.
  4. Using REST API: If you are using a cloud-based email service provider such as SendGrid, you can send emails using their REST API. You can make HTTP requests to the API endpoint with the necessary parameters to send emails.

Why use apache camel to send mails in spring boot applications?

Apache Camel is a powerful integration framework that can be used to build complex, yet scalable and maintainable integrations between different systems. One of the many components provided by Camel is the Camel Mail component, which provides a convenient way to send and receive emails from within your Spring Boot application. Using the Camel Mail component in a Spring Boot application has several benefits, including:

  1. Simplified email configuration: With Camel Mail, you can easily configure the email properties in a single place, without having to repeat the configuration for each email-sending method.
  2. Multiple protocol support: Camel Mail supports multiple email protocols, including SMTP, POP3, and IMAP, allowing you to choose the protocol that suits your specific needs.
  3. Attachments and templates support: Camel Mail allows you to attach files to your emails, and also provides support for email templates, which can help you send emails with a consistent format.
  4. Integration with other Camel components: Camel Mail can be seamlessly integrated with other Camel components, such as Camel HTTP or Camel File, allowing you to build more complex integrations that involve email sending and other operations.
  5. Error handling and retry mechanisms: Camel Mail provides robust error handling and retry mechanisms, which ensure that your email-sending operation is reliable and resilient.

Step 1: Add Apache Camel Dependencies to Your Project

The first step is to add the Apache Camel dependencies to your Spring Boot project. You can do this by adding the following dependencies to your pom.xml file:

        <dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>4.0.0-M2</version>
</dependency>

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-mail</artifactId>
<version>4.0.0-M2</version>
</dependency>

The first dependency camel-spring-boot-starter is a Spring Boot starter that includes the basic Camel dependencies needed to run a Camel application within a Spring Boot container. It provides auto-configuration and default settings to simplify Camel configuration for Spring Boot users. And the second dependency is for the Camel Mail component, which provides support for sending and receiving emails.

Step 2: Configure Email Settings

Then Next, you’ll need to configure the email settings for your application. You can do this by adding the following properties to your application.properties file:

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=your_email_address
spring.mail.password=your_email_password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

Step 3: Create a Camel Route to Send Email

Now that we’ve configured the email settings, we can create a Camel route to send an email. Here’s an example route that sends an email:

@Component
public class EmailRoute extends RouteBuilder {

@Value("${spring.mail.properties.mail.smtp.auth}")
private boolean auth;
@Value("${spring.mail.properties.mail.smtp.starttls.enable}")
private boolean starttls;

@Override
public void configure() throws Exception {
from("seda:sendEmail")
.log(LoggingLevel.INFO,"SENDING EMAIL NOTIFICATION")
.to("smtps://{{spring.mail.host}}:{{spring.mail.port}}?username={{spring.mail.username}}&password={{spring.mail.password}}&mail.smtp.auth=auth&mail.smtp.starttls.enable=starttls")
.log(LoggingLevel.INFO,"EMAIL NOTIFICATION SENT")
.stop();
}

}

In this route, the seda:sendEmail endpoint is used to receive a message that needs to be sent as an email notification. The message is then passed to the SMTP server using the smtps endpoint, which is configured using properties defined in a Spring configuration file. Since email sending can be an expensive operation, using an asynchronous seda endpoint can help to prevent blocking other components in the route while the email is being sent. If a direct endpoint was used instead, it could potentially block other components and slow down the processing of messages in the route.

Step 4: Create an Email service class

Then We need to create a service class that contains a method named sendEmail. This method will be responsible for sending an email using Apache Camel and the configured SMTP server.

@Service
public class EmailService {

@Autowired
private ProducerTemplate producerTemplate;

@Value("${sspring.mail.username}")
private String from;

public void sendEmail(String recipient, String subject, String body) {
Map<String, Object> headers = new HashMap<>();
headers.put("To", recipient);
headers.put("From", from);
headers.put("Subject", subject);
producerTemplate.sendBodyAndHeaders("seda:sendEmail", body, headers);
}

}

Step 5: Send the Email

Finally, we can send the email by invoking the sendEmail method in EmailService class. Here’s an example of how to do this in a controller:

@RequiredArgsConstructor
@RestController
public class EmailController {

private final EmailService emailService;

@GetMapping("/sendEmail")
public String sendEmail() {
emailService.sendEmail("test@mail.com","Testing Email Service",
"This is a test email.");
return "Email sent!";
}
}

In this article, we’ve learned how to send emails using Apache Camel in a Spring Boot application. We started by adding the Apache Camel dependencies to our project, then configured the email settings, created a Camel route to send an email, Created an email service, and finally sent the email by invoking the service. With Apache Camel, sending emails from your Spring Boot application is easy and straightforward.

--

--