How to implement a Java Class into a Service Task

Hi, I am trying to implement a Java Class into my Service task but I am getting the following error in Camunda: “Cannot submit task form 943fe63b-7d3f-11ec-9632-70665515d97d: ENGINE-09008 Exception while instantiating class(…)”. It basically says that it can not load my Java class. Where is the error?

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;



public class SendEmail {
	final String senderEmail = "random@gmail.com"; //change email address
	final String senderPassword = "random";	//change password
	final String emailSMTPserver = "smtp.gmail.com";
	final String emailServerPort = "465";
	String receiverEmail = null;
	static String emailSubject;
	static String emailBody;
   public SendEmail(String receiverEmail, String subject, String body) {
	   //receiver email
	   this.receiverEmail = receiverEmail;
	   //subject
	   this.emailSubject = subject;
	   //body
	   this.emailBody = body;
	   
	   Properties props = new Properties();
       props.put("mail.smtp.user",senderEmail);
       props.put("mail.smtp.host", emailSMTPserver);
       props.put("mail.smtp.port", emailServerPort);
       props.put("mail.smtp.starttls.enable", "true");
       props.put("mail.smtp.auth", "true");
       props.put("mail.smtp.socketFactory.port", emailServerPort);
       props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
       props.put("mail.smtp.socketFactory.fallback", "false");
       SecurityManager security = System.getSecurityManager();
       
       try {
    	   Authenticator auth = new SMTPAuthenticator();
    	   Session session = Session.getInstance(props, auth);
    	   MimeMessage msg = new MimeMessage(session);
    	   msg.setText(emailBody);
    	   //System.out.println(emailBody);
    	   msg.setSubject(emailSubject);
    	   msg.setFrom(new InternetAddress(senderEmail));
    	   msg.addRecipient(Message.RecipientType.TO, new InternetAddress(receiverEmail) );
    	   Transport.send(msg);
    	   System.out.println("Message sent!");
    	   String className = this.getClass().getSimpleName();
    	   System.out.println(className);
    	   
    	   
       }
       
       catch (Exception e) {
    	   e.printStackTrace();
       }
			   
	   
   }
	
	public class SMTPAuthenticator extends javax.mail.Authenticator{
		public PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(senderEmail, senderPassword );
		}
	}
		
public class Tester {
	public static void main( String [] args) {
		SendEmail send = new SendEmail("testemail@gmail.com", "Test", "Camunda" ); //change receiver email
	}
		


}

*here the print of the definitions made in the Service Task

Hi @Salva you may want to look at this example in the Getting Started guide regarding how to set up and invoke a Java Class in a Service Task: Invoking a Java Class from a BPMN 2.0 Service Task | docs.camunda.org

Joe

2 Likes