How to send email based on listener outputs in AEM?



I've got this listener which listens to any changes including addition, modification and deletion for nodes under content directory. A mail has to be send to some mail address whenever the listener logs a change. How to work around that?

import javax.jcr.RepositoryException;

import javax.jcr.Session;

import javax.jcr.observation.Event;

import javax.jcr.observation.EventListener;

import org.osgi.service.component.annotations.Activate;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Deactivate;

import org.osgi.service.component.annotations.Reference;


import org.osgi.service.component.ComponentContext;

import javax.jcr.observation.EventIterator ;



service= EventListener.class)


public class ContentChangeListener implements EventListener{


     private Session adminSession;



     @Reference repository;



     private MessageGatewayService messageGatewayService;



     public void activate(ComponentContext context) throws Exception {

     System.out.println("Started Listener");

     try {

         adminSession = repository.loginService("testing",null);



          "/content/silversea/en/homepage", true, null, null, false);



     } catch (RepositoryException e){

      System.out.println("unable to register session"+e);




    public void deactivate(){

     if (adminSession != null){





    public void onEvent(EventIterator eventIterator) {

      try {

        while (eventIterator.hasNext()){


        System.out.println("Something has been added : {}"+ eventIterator.nextEvent().getPath());


       } catch(RepositoryException e){

       System.out.println("Error while treating events"+e);




Accepted Solutions (1)

Accepted Solutions (1)




Hi jezwn

Add following code in your Activate method and modify variables accordingly. I have configured gmail for sending the mails.​

String sender_mail = YOUR_EMAIL_ADDRESS;

String sender_pass = YOUR_PASSWORD;

String host = "";

int port    = 587;

String recipient_mail = '';

String mail_subject = "Test Subject";

String mail_content = "This is sample email content";

java.util.Properties props = new java.util.Properties();

props.put("", host);

props.put("mail.smtp.port", port);

props.put("mail.smtp.starttls.enable", true);

props.put("mail.smtp.auth", true);

Session session = Session.getInstance(props, new javax.mail.Authenticator() {

    protected PasswordAuthentication getPasswordAuthentication() {

        return new PasswordAuthentication(sender_mail, sender_pass);



try {

    MimeMessage msg = new MimeMessage(session);

    InternetAddress[] address = InternetAddress.parse(recipient_mail, true);

    msg.setRecipients(Message.RecipientType.TO, address);

    String timeStamp = new SimpleDateFormat("yyyymmdd_hh-mm-ss").format(new Date());


    msg.setSentDate(new Date());


    msg.setHeader("XPriority", "1");

    Transport.send(msg);"Mail has been sent successfully");

} catch (MessagingException mex) {"Unable to send an email: " + mex);


Don't forget to import libraries

Hope it helps!

Answers (0)