DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Trending

  • AGENTS.md Makes Your Java Codebase AI-Agent Ready
  • Building a Config-Driven SOAP/REST Integration Layer: One Service, Many Protocols
  • HTTP QUERY in Java: The Missing Method for Complex REST API Searches
  • Spark Performance Deep Dive on Databricks: Shuffle Tuning, Skew Handling, and Z-Ordering With Delta Lake + Unity Catalog

How to Send Mail From Camunda to the SMTP Server

Learn how to send mail to the SMTP server from Camunda which is running as a Spring boot application in Eclipse IDE.

By 
Tiruvenkatasamy Baskaran user avatar
Tiruvenkatasamy Baskaran
·
Apr. 07, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
5.4K Views

Join the DZone community and get the full member experience.

Join For Free

Camunda exposes a mail interface to connect with SMTP using Camunda-bpm-mail-core library as a dependency in POM.xml file. Here Camunda acts as a mail client.

Prerequisites

  • Eclipse (any version) with Maven capabilities
  • Java 8+
  • SMTP Mail Server
  • Camunda

Installing Eclipse-IDE on Windows

  1. Click on the link: https://www.eclipse.org/downloads/download.php?file=/oomph/epp/2020-09/R/eclipse-inst-jre-win64.exe
  2. Download the eclipse-inst-jre-win64.exe file and run the eclipse installer.Run the installer
  3. Select Eclipse IDE for Eclipse committers and install.Set Eclipse IDE

Creating a Maven Project in Eclipse IDE

  • Open the Eclipse IDE
  • Go to File  > New > Project:

Go to File and select new project

  • Go to Maven -> Maven Project and click Next:

Go to Maven Project

  • Select your workspace location and click Next:

Select Workspace Location

  • Select quick start maven archetype and click Next:


Quick start Maven Archetype

  • Enter Group Id, Artifact Id, and package name:


Enter group ID, artifact ID and package name

Group Id: Fill in a groupId for the project of your choice.

Artifact Id: Fill artifactId for the project of your choice.

Package: java package structure of your choice

  • The above process will create a project structure like the below.:

Create project structure

  • Create a package like com.example.demo.delegate under src/main/java folder and create source folder src/main/resources folder.

 Place the CamundaApplication.java file in com. example.demo package:

Java
 
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class MailApplication {
	
	public static void main(String args[]) {
		SpringApplication.run(MailApplication.class, args);
	}

}

Add the Email.java file in com. example.demo package: 

Java
 
package com.example.demo;

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;

public class Email implements JavaDelegate{
	
		public void execute (DelegateExecution execution) throws Exception{
			
				String email= (String) execution.getVariable("email");
				System.out.println("Email preparation to "+email);
				
				 Properties props = new Properties();
	                props.setProperty("mail.transport.protocol", "smtp");
	                props.setProperty("mail.smtp.host", "smtp.alj.intraxa");
	                props.put("mail.smtp.auth", "true");
	                props.put("mail.smtp.port", "TCP 25");
	                props.put("mail.smtp.starttls.enable", "true");
	                
				String myAccountEmail="[email protected]";
				String password="";
				
				Session session=Session.getInstance(props, new Authenticator() {
					@Override
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(myAccountEmail,password);
					}
				});
				
				Message message= prepareMessage (session, myAccountEmail, email);
				
				
				try {
					Transport.send(message);
					System.out.println("Message sent successfully");
				} catch (MessagingException e) {
					
					e.printStackTrace();
					System.out.println("Message was not send");
				}
				
				
		}

		private static Message prepareMessage(Session session, String myAccountEmail, String recipient) {
			try {
				Message message=new MimeMessage(session);
				message.setFrom(new InternetAddress(myAccountEmail));
				message.setRecipient(Message.RecipientType.TO, new InternetAddress (recipient));
				message.setSubject("Confirmation");
				message.setText("Welcome to Camunda");
				return message;
				
			} catch (Exception ex) {
				Logger.getLogger(Email.class.getName()).log(Level.SEVERE,null, ex);
			}
			return null;
		}
	}

Add  application.yaml and Mail.bpmn in /src/main/resources folder:

Java
 
application.yaml

		camunda.bpm:
  admin-user:
    id: n
    password: demo   
    firstName: demo
  filter:
    create: All tasks

Email configuration

Java
 
 <?xml version="1.0" encoding="UTF-8"?>

<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_10f2w0s" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.9.0" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.15.0">

  <bpmn:process id="Process_1sm92jk" isExecutable="true">

    <bpmn:startEvent id="StartEvent_1">

      <bpmn:outgoing>Flow_0yacy9g</bpmn:outgoing>

    </bpmn:startEvent>

    <bpmn:sequenceFlow id="Flow_0yacy9g" sourceRef="StartEvent_1" targetRef="Activity_06irpot" />

    <bpmn:serviceTask id="Activity_06irpot" name="Email" camunda:class="com.email.Email">

      <bpmn:incoming>Flow_0yacy9g</bpmn:incoming>

      <bpmn:outgoing>Flow_15uttn5</bpmn:outgoing>

    </bpmn:serviceTask>

    <bpmn:endEvent id="Event_0aidxzg">

      <bpmn:incoming>Flow_15uttn5</bpmn:incoming>

    </bpmn:endEvent>

    <bpmn:sequenceFlow id="Flow_15uttn5" sourceRef="Activity_06irpot" targetRef="Event_0aidxzg" />

  </bpmn:process>

  <bpmndi:BPMNDiagram id="BPMNDiagram_1">

    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1sm92jk">

      <bpmndi:BPMNEdge id="Flow_0yacy9g_di" bpmnElement="Flow_0yacy9g">

        <di:waypoint x="215" y="117" />

        <di:waypoint x="270" y="117" />

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNEdge id="Flow_15uttn5_di" bpmnElement="Flow_15uttn5">

        <di:waypoint x="370" y="117" />

        <di:waypoint x="432" y="117" />

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">

        <dc:Bounds x="179" y="99" width="36" height="36" />

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape id="Activity_0eznmhg_di" bpmnElement="Activity_06irpot">

        <dc:Bounds x="270" y="77" width="100" height="80" />

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape id="Event_0aidxzg_di" bpmnElement="Event_0aidxzg">

        <dc:Bounds x="432" y="99" width="36" height="36" />

      </bpmndi:BPMNShape>

    </bpmndi:BPMNPlane>

  </bpmndi:BPMNDiagram>

</bpmn:definitions>


Replace the pom.xml with the below content:

XML
 
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>CamundaEmailDemo</groupId>
  <artifactId>CamundaEmailDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <properties>
    <camunda.spring-boot.version>7.16.0</camunda.spring-boot.version>
    <spring-boot.version>2.5.4</spring-boot.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  
   <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring-boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  
  <dependencies>
    <dependency>
      <groupId>org.camunda.bpm.springboot</groupId>
      <artifactId>camunda-bpm-spring-boot-starter-webapp</artifactId>
      <version>${camunda.spring-boot.version}</version>
    </dependency>
    <!--        //for accessing camunda api's-->
        <!--        //for connectors-->
        <dependency>
            <groupId>org.camunda.bpm.springboot</groupId>
            <artifactId>camunda-bpm-spring-boot-starter-rest</artifactId>
            <version>${camunda.spring-boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.camunda.connect</groupId>
            <artifactId>camunda-connect-http-client</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.camunda.connect</groupId>
            <artifactId>camunda-connect-soap-http-client</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.camunda.bpm</groupId>
            <artifactId>camunda-engine-plugin-connect</artifactId>
            <version>7.16.0</version>
        </dependency>
    
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
    </dependency>
    <dependency>
      <groupId>com.sun.xml.bind</groupId>
      <artifactId>jaxb-impl</artifactId>
      <version>2.3.5</version>
    </dependency>
    <!--for email-->
        <dependency>
            <groupId>org.camunda.bpm.extension</groupId>
            <artifactId>camunda-bpm-mail-core</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.4.5</version>
</dependency>
  </dependencies>
  
   <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>${spring-boot.version}</version>
        <configuration>
          <layout>ZIP</layout>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  
</project>



Sending Mail

  1. Run the MailApplication.java and BPMN flow from Browser UI. It will send the mail-to-mail server. 
  2. Further, the code in Email.java can be customized according to your needs.

    Java
     
    Properties props = new Properties();
    
    props.setProperty("mail.transport.protocol", "smtp");
    
    props.setProperty("mail.smtp.host", "smtp.alj.intraxa");
    
    props.put("mail.smtp.auth", "true");
    
    props.put("mail.smtp.port", "TCP 25");
    
    props.put("mail.smtp.starttls.enable", "true");
    
    String myAccountEmail="[email protected]";


We have learned how to send mail from Camunda to the SMTP server.

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook