Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

MessageGateway is

Avatar

Level 2

Hi 
I am created an email servlet in AEM and it works fine when I tested it on my local 4502,

but when I test this servlet on company DEV environment, the messageGateway is always null

	    	MessageGateway<HtmlEmail> messageGateway = messageGatewayService.getGateway(HtmlEmail.class);

 Here's my code:

@component(service = Servlet.class,
property = {
"sling.servlet.methods=" + "GET",
"sling.servlet.paths =" + "/bin/test/email-content-report"})
public class ContentReportServlet extends SlingAllMethodsServlet {

    @reference
    MessageGatewayService messageGatewayService;
    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
    	try {
    		
			
	    	MessageGateway<HtmlEmail> messageGateway = messageGatewayService.getGateway(HtmlEmail.class);
	    	
	    	if(messageGateway != null) {
	    		HtmlEmail email = new HtmlEmail();
	    		email.setCharset("utf-8");
	    		
		    	email.addTo(recipientemail);
	    		email.setFrom("noreply@test.ca");
	    		email.setSubject("Email Testing");
	    		email.setHostName("mailgateway.uk.fid-intl.com");
	    		email.setSmtpPort(587);
	    		email.setSSLOnConnect(false);

	    		email.setHtmlMsg("Test message");
	    		
	    		ByteArrayDataSource dataSource = new ByteArrayDataSource(byteStream.toByteArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
	    		email.attach(dataSource, "filename.xlsx", "ContentReport", HtmlEmail.ATTACHMENTS);

	    		messageGateway.send(email);
	    		
	    	}
		} catch (Exception e) {
	        log.error("Error occurred while sending report by email: {}", e.getMessage(), e);
        }  

}
}

I found a few old posts with the same issue saying that I need to change the day cq mail service, I tried that but still doesn't work
here's the day cq mail service on my local

HanL_0-1710182392260.png

Here's the day cq mail service for the dev environment

HanL_1-1710182862803.png

Please advise. 
Thank you

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

use same OSGI annotation for Reference also.

import org.osgi.service.component.annotations.Reference;
That is the issue. instead of using from Felix.

View solution in original post

4 Replies

Avatar

Community Advisor

Is the MessageGatewayService is active?

can you check that in configMgr.

Also make sure the annotations are used properly 

@reference

 

@component

 

keep some log statements to get more details and also can we get Service class code also?

Avatar

Level 2

Hi Suresh
For those two annotations, I am using:
import org.osgi.service.component.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
In the log, an exception was thrown 

java.lang.NullPointerException: null

at

 

	    	MessageGateway<HtmlEmail> messageGateway = messageGatewayService.getGateway(HtmlEmail.class);

 

I tried to search MessageGatewayService in configMgr in both my local and DEV environments, can't find anything.

May I ask, what Service class do you mean?

Thank you

Avatar

Correct answer by
Community Advisor

use same OSGI annotation for Reference also.

import org.osgi.service.component.annotations.Reference;
That is the issue. instead of using from Felix.