Expand my Community achievements bar.

SOLVED

Mail MessageGateway is null

Avatar

Level 2

I created a mail service class, which should use the CQ Mail Configuration.
However I compared my code to several samples from the Internet my service does not work, because
messageGatewayService.getGateway(...) always returns null. This is my simplified code:

------------------------------------------
import org.apache.commons.mail.Email;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.mailer.MessageGateway;
import com.day.cq.mailer.MessageGatewayService;

@Component(immediate = true)
@Service(MailService.class)
public class MailServiceImpl implements MailService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MailServiceImpl.class);
    
    @Reference
    private MessageGatewayService messageGatewayService;
    
    @Override
    public void sendMail(String subject, String mailText, List<String> recipients) {
        LOGGER.info("Send mail");
        try {        
            Email email = new SimpleEmail();    
            for (String recipient : recipients) {
                email.addTo(recipient, recipient);
            }
            email.setSubject(subject);
            email.setMsg(mailText);
            
            MessageGateway<Email> messageGateway = messageGatewayService.getGateway(Email.class);
            // --> returns null
            MessageGateway<SimpleEmail> messageGateway1 = messageGatewayService.getGateway(SimpleEmail.class);
            // --> returns null
            MessageGateway<HtmlEmail> messageGateway2 = messageGatewayService.getGateway(HtmlEmail.class);
            // --> returns null
            //MessageGateway messageGateway3 = messageGatewayService.getGateway(com.day.cq.mailer.impl.DefaultMailService.class);
            // --> deactivated in code, because DefaultMailService.class unknown

            if (messageGateway!=null) {
                messageGateway.send((Email) email);
            } else {
                LOGGER.error("Message gateway could not be retrieved.");
            }        
        } catch(Exception ex) {
            LOGGER.error("Error on sending mail.");        
        }            
    }
}
------------------------------------------
I tested the params used in my CQ Mail configuration in a different mail application and mails were sent out correctly, so the configuration seems not to be the problem.

When I debug the messageGatewayService class I can see it contains a map for gateway configurations which contains the CQ Mail configuration I defined in OSGI (see attachment).
So the messageGatewayService knows about my CQ Mail config. But it resists to deliver it to me :(

I noticed that the CQ Mail configuration in the gateway map is stored with key 'com.day.cq.mailer.impl.DefaultMailService' so it is the PID of the service.
But all examples I saw get the gateway via
MessageGateway<Email> messageGateway = messageGatewayService
.getGateway(Email.class) or
.getGateway(SimpleEmail.class) or
.getGateway(HtmlEmail.class)
which does not work for me?

Any idea?

1 Accepted Solution

Avatar

Correct answer by
Level 10

See this community article that covers this use case: 

https://helpx.adobe.com/experience-manager/using/creating-custom-aem-workflow-steps.html

In this article - it steps you through how to create a custom workflow step that emails using a MessageServiceGateway. 

If you follow this and get it working - you can compare it with your code and where the differences are. 

View solution in original post

15 Replies

Avatar

Correct answer by
Level 10

See this community article that covers this use case: 

https://helpx.adobe.com/experience-manager/using/creating-custom-aem-workflow-steps.html

In this article - it steps you through how to create a custom workflow step that emails using a MessageServiceGateway. 

If you follow this and get it working - you can compare it with your code and where the differences are. 

Avatar

Level 9

@fstarfeld ,

Your code seems correct to me also debug attachment file. My guess is that something not right with the mail server configuration. 

Jitendra

Avatar

Level 9

Also, Kindly check whether mail server is up & running or not.

Jitendra

Avatar

Administrator

As mentioned by Jitendra, make sure that Until you configure the SMTP information for the messagegateway it will return null. As for the username and password, that would be the username and password you use to authenticate to the SMTP server.

 

CONFIGURING CQ MAIL SERVICE

The first step to sending emails through Adobe CQ is to configure the Day CQ Mail Service.  To do this, log into to the OSGi Console at {server}:{port}/system/console/configMgr and look for a service called Day CQ Mail Service. Select the service and you should see a screen like the below:

Configuring the Day CQ Mail Service

Enter all of the relevant information for your current SMTP provider.  If you don't have or can't easily get SMTP set up within your organization, a Gmail account works for testing.

Once you have the Day CQ Mail Service configured you should be able to send emails through Adobe CQ.  If, later on you run into problems with getting a null Message Gateway, you probably entered something incorrectly here.

I hope this will work for you.

Reference Link:- http://labs.6dglobal.com/blog/2012-08-20/sending-email-adobe-cq-api/

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Level 2

kautuksahni wrote...

As mentioned by Jitendra, make sure that Until you configure the SMTP information for the messagegateway it will return null. As for the username and password, that would be the username and password you use to authenticate to the SMTP server.

 

CONFIGURING CQ MAIL SERVICE

The first step to sending emails through Adobe CQ is to configure the Day CQ Mail Service.  To do this, log into to the OSGi Console at {server}:{port}/system/console/configMgr and look for a service called Day CQ Mail Service. Select the service and you should see a screen like the below:

Configuring the Day CQ Mail Service

Enter all of the relevant information for your current SMTP provider.  If you don't have or can't easily get SMTP set up within your organization, a Gmail account works for testing.

Once you have the Day CQ Mail Service configured you should be able to send emails through Adobe CQ.  If, later on you run into problems with getting a null Message Gateway, you probably entered something incorrectly here.

I hope this will work for you.

Reference Link:- http://labs.6dglobal.com/blog/2012-08-20/sending-email-adobe-cq-api/

Thanks and Regards

Kautuk Sahni

 


Thanks for your feedback!! You all provided me some links I already checked out. You point to the mail server configuration. However I tested with a gmail and local demo SMTP server and mails were received from other mail apps with the same config used as in CQ Mail service --- I now will try to find a "real" productive SMTP within my company I can use. I will give a feedback lateron.

Avatar

Level 2

Ok, meanwhile I tested with 4 different SMTP servers. The same result for all of them, messageGatewayService.getGateway(Email.class) returns null however the configuration works, when I test them with other mail apps.

So no idea. Maybe its my environment, I will ask a colleague to test the code on his dev environment.

Avatar

Level 10

Also - if you have a fresh install of CQ - try testing on a new install, I have seen when libs get deleted by mistake that cause unexplained behavior. 

Avatar

Level 2

Hi fstarfeld, 

I am having EXACTLY the same issue. Were you able to solve this? Would be very interested in an update :)

Thanks, 

Alex

Avatar

Level 2

Hi Alex,

sorry for you. We tested CQ Mailservice with version 6.0 and 6.1 with a lot of different mail accounts and smtp configurations but could not solve the issue.

In our case we stopped investigating this and created our own little mail service

1. OSGI configuration with SMTP settings

2. Some Java code to read the OSGI configuration and send out mail ( we used the org.apache.commons.mail library)

Avatar

Level 1

I was facing a similar problem here and found out that <Export-Package> and <Import-Package> were miss configured on maven-bundle-plugin.   

Avatar

Level 10

Did you try followoing the above article as it? 

Avatar

Level 2

I'm facing a similar problem where the emails are sent successfully sometimes and sometimes not.

If I check the logs, when emails are not sent I see this in my error.log

com.day.cq.wcm.foundation.forms.impl.MailServlet The mail service is currently not available! Unable to send form mail.

This translates to me that the object for class com.day.cq.mailer.MailService was null.

Avatar

Level 3

We are facing the same null pointer issue while sending mail via messgegatway

messageGateway = messageGatewayService.getGateway(email.class)

We have configured the SMTP server and able to test get the OOTB mails(Ex:request for activation) How ever same thing not working using the custom code?

Do we have any dependencies with felix scr annotations because we are using aem 6.3 sp2 with service annotations but still we are facing messagegatway as null ?

Avatar

Level 10

For AEM 6.3 - try using DS Annotations.

Avatar

Level 3

Hi Smac,

I have used latest DS annotation for the same but still messageGatewayService.getGateway(email.class) throwing null pointer do we have to add any other dependencies in the pom xml.if not using Felix scr annotations.below are the annotations used

import org.apache.commons.mail.Email;

import org.apache.commons.mail.SimpleEmail;

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

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

import com.day.cq.mailer.MessageGateway;

import com.day.cq.mailer.MessageGatewayService;

looks like with OSGi R6 annotations in AEM 6.3 it's mail service is not working ?