Hi @Niall,
thank you so much for your reply. I attach my simple bpmn model.diagram.bpmn (2.4 KB)
What I have done is to create a maven project, in which I created a java class named “SendEmail”. Then I install the Maven project and paste the “demoEmail.war” file inside the webapps folder of tomcat server.
The code of the java class is exactly the following:
package com.camunda.demoEmail.demoEmail;
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 SendEmail implements JavaDelegate {
public void execute (DelegateExecution execution) throws Exception{
String email= (String) execution.getVariable("email");
System.out.println("Email preparation to "+email);
Properties properties=new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
String myAccountEmail="**********";
String password="*************";
Session session=Session.getInstance(properties, 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) {
// TODO Auto-generated catch block
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("Uom Bank - Loan Application");
message.setText("Your application has been created.");
return message;
} catch (Exception ex) {
Logger.getLogger(SendEmail.class.getName()).log(Level.SEVERE,null, ex);
}
return null;
}
}
When I start my process instance on Tasklist I create a new variable “email” as String type with value a valid email address.
What I have noticed is that if I create a Java Project, with a java class “JavaMailUtil” that has exactly the same code and another java class “JavaMail” that it has a main method, the email is sent successfully. I paste here also the classes for the java project that works without issues.
JavaMailUtil class:
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;
public class JavaMailUtil {
public static void sendMail(String recipient) {
System.out.println("Prepare an email to ");
Properties properties=new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
String myAccountEmail="******************";
String password="**************";
Session session=Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(myAccountEmail,password);
}
});
Message message= prepareMessage (session, myAccountEmail, recipient);
try {
Transport.send(message);
System.out.println("Message sent successfully");
} catch (MessagingException e) {
// TODO Auto-generated catch block
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("Uom Bank - Loan Application");
message.setText("Your application has been created.");
return message;
} catch (Exception ex) {
Logger.getLogger(JavaMailUtil.class.getName()).log(Level.SEVERE,null, ex);
}
return null;
}
}
And the “JavaSend” class:
public class JavaMail {
public static void main(String[] args) throws Exception {
JavaMailUtil.sendMail("someone@gmail.com");
}
}
Niall, if you could help me on that I would be so grateful for your assistance and your kindness to help me. I have spent so many hours without understanding what I am missing, since the java project works perfectly, but when I deploy my similar maven project, I face issues.
Thank you in advance. Looking forward to solving this.