Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

How to send email based on listener outputs in AEM?

jezwn
Level 4
Level 4

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 com.day.cq.mailer.MessageGatewayService;

import org.osgi.service.component.ComponentContext;

import javax.jcr.observation.EventIterator ;

 

@Component(immediate=true,

service= EventListener.class)

 

public class ContentChangeListener implements EventListener{

 

     private Session adminSession;

      

     

     @Reference

     org.apache.sling.jcr.api.SlingRepository repository;

    

     @Reference

     private MessageGatewayService messageGatewayService;

      

     @Activate

     public void activate(ComponentContext context) throws Exception {

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

     try {

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

         adminSession.getWorkspace().getObservationManager().addEventListener(

          this, Event.PROPERTY_ADDED|Event.NODE_ADDED|Event.NODE_MOVED|Event.NODE_REMOVED|Event.PROPERTY_CHANGED|Event.PROPERTY_REMOVED,

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

      

          

     } catch (RepositoryException e){

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

     }

    }

    @Deactivate

    public void deactivate(){

     if (adminSession != null){

      adminSession.logout();

     }

    }

      

    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);

      }

     }

    }

1 Accepted Solution
Umesh_Sondhi
Correct answer by
Community Advisor
Community Advisor

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 = "smtp.gmail.com";

int port    = 587;

String recipient_mail = 'email@address.com';

String mail_subject = "Test Subject";

String mail_content = "This is sample email content";

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

props.put("mail.smtp.host", 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.setSubject(mail_subject);

    msg.setSentDate(new Date());

    msg.setText(mail_content);

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

    Transport.send(msg);

    LOGGER.info("Mail has been sent successfully");

} catch (MessagingException mex) {

    LOGGER.info("Unable to send an email: " + mex);

}


Don't forget to import libraries

Hope it helps!

View solution in original post

1 Reply
Umesh_Sondhi
Correct answer by
Community Advisor
Community Advisor

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 = "smtp.gmail.com";

int port    = 587;

String recipient_mail = 'email@address.com';

String mail_subject = "Test Subject";

String mail_content = "This is sample email content";

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

props.put("mail.smtp.host", 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.setSubject(mail_subject);

    msg.setSentDate(new Date());

    msg.setText(mail_content);

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

    Transport.send(msg);

    LOGGER.info("Mail has been sent successfully");

} catch (MessagingException mex) {

    LOGGER.info("Unable to send an email: " + mex);

}


Don't forget to import libraries

Hope it helps!

View solution in original post