Search Tutorials


Apache Camel Tutorial - Send SMTP Email Using Gmail | JavaInUse

Apache Camel Tutorial - Send SMTP Email Using Gmail

In this tutorial we will create a simple application to send mail using apache camel. We will define a camel route which will read contents from a file and send it to multiple mail recipients.
We makes use of camel smtp end point to send mail.

Apache Camel - Table of Contents

File Transfer Using Java DSL Apache Camel Apache Camel Java DSL + Spring Integration Hello World Example Apache Camel Exception Handling Using Simple Example Apache Camel Redelivery policy using example Integrate Apache Camel and ActiveMQ EIP patterns using Apache Camel Apache Camel Tutorial- Integrate Spring Boot+ Apache Camel Apache Camel Tutorial- Integrate with MySQL DB using SQL query Apache Camel EIP - Splitter and Aggregator pattern Apache Camel Unit Testing Apache Camel + Spring + Quartz Hello World Example Camel application deployment on JBoss Fuse Apache Camel + Apache CXF SOAP Webservices Apache Camel + JAX-RS REST Webservice Apache Camel + CXFRS REST Webservice Apache Camel Routing Slip EIP Pattern Apache Camel Dynamic Router Pattern Apache Camel Load Balancer EIP Pattern Apache Camel Interceptors Apache Camel + Kafka Hello World Example Apache Camel - Marshalling/Unmarshalling XML/JSON Data Example Calling and Consuming Webservices using Apache Camel Apache Camel Tutorial - Send SMTP Email Using Gmail Apache Camel Tutorial - SEDA component Hello World Example Spring Boot + Apache Camel + RabbitMQ - Hello World Example Apache Camel Tutorial - Idempotent Consumer using MemoryIdempotentRepository and FileIdempotentRepository Spring Boot + Apache Camel JDBC component + MySQL - Hello World Example Spring Boot + Apache Camel SQL component + MySQL - Hello World Example Spring Boot + Apache Camel SQL component + Transaction Management Example

Lets Begin

The File component is present in the camel-core jar itself.Other than this we will be using the camel-mail dependency for smtp endpoint.
We will create Eclipse maven project as follows-

Apache Camel File Copy Tutorial

Our pom file will be as follows-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javainuse</groupId>
	<artifactId>camel-java-dsl</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-core</artifactId>
			<version>2.13.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-mail -->
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-mail</artifactId>
			<version>2.13.0</version>
		</dependency>

	</dependencies>
</project>

For sending mail using Gmail, google has provided instructions here As specified in this page fully qualified domain name of SMTP service is smtp.gmail.com and port is 465.
Next we create a class to construct a camel route. A Route is like an instruction definition to Camel on how to move your messages from one point to another. Here we will be routing the message from file endpoint to the smtp endpoint.
package com.javainuse;

import org.apache.camel.builder.RouteBuilder;

public class SimpleRouteBuilder extends RouteBuilder {

	@Override
	public void configure() throws Exception {
		from("file:C:/inputFolder?noop=true").doTry().setHeader("subject", simple("JavaInUse Invitation111"))
				.setHeader("to", simple("javainuse@gmail.com,testouthworking@gmail.com"))
				.to("smtps://smtp.gmail.com:465?username=testcamelsmtp@gmail.com&password=ABC@123");
	}
}

When Camel is started, it creates a CamelContext object that contains the definition of the Route to be started. Below we create default camel context and load the route created in SimpleRouteBuilder.
package com.javainuse;

import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;

public class MainApp {

    public static void main(String[] args) {
        SimpleRouteBuilder routeBuilder = new SimpleRouteBuilder();
        CamelContext ctx = new DefaultCamelContext();
        try {
            ctx.addRoutes(routeBuilder);
            ctx.start();
            Thread.sleep(5 * 60 * 1000);
            ctx.stop();
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }
}
Run this MainApp.java.

In Gmail we get a mail as follows-
Apache Camel GMAIL Mail recieved
It may be needed for you to go to the Google Account Security page and allow less secure applications to access your account.
Gmail Security Account

Gmail recieve less secure mails

Download Source Code

Download it - Apache Camel Java Mail Application

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions