Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

@Reference EmailService emailService throwing null

Avatar

Level 4

Requirement:- send email upon form submission.
Below is my class. I am getting null pointer exception when i print the emailService. Can anyone help me out please.

import java.util.HashMap;

import java.util.List;

import java.util.Map;

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

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

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.adobe.acs.commons.email.EmailService;

Arun Patidarsmacdonald2008

@Component(immediate = true, service = ContactEmailService.class)

public class ContactEmailService {

@Reference

private EmailService mailService;

Logger logger = LoggerFactory.getLogger(this.getClass());

EmailDetails details = new EmailDetails(); //bean class

private Map<String, String> emailParams = new HashMap<>();

public void sendEmail() {

logger.info("email service"+mailService); // prints null

details.setTemplatePath("/etc/notification/email/kohler/contact_email.txt");

emailParams.put("body", "hello there");

emailParams.put("senderEmailAddress", "abcd@example.com");

emailParams.put("senderName", "David Smith");

details.setEmailParams(emailParams);

String[] recipients = { "test@gmail.com", "sample@example.com" };

details.setRecipients(recipients);

List<String> failureList = mailService.sendEmail(details.getTemplatePath(), details.getEmailParams(),details.getRecipients()); //throws null pointer exception

logger.info(""+failureList.size());

if (failureList.isEmpty()) {

logger.info("successfully sent");

} else {

logger.info("failed to deliver");

}

}

}

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

That's what I was assuming. Thanks for reporting back and confirming that it was indeed this problem.

Jörg

View solution in original post

6 Replies

Avatar

Employee Advisor

How do you get access to an instance of the ContactEmailService? Remember that calling the constructor of this class does not cause the injection, but you need to use it via a service reference. Can you post the relevant code which invokes the sendEmail() function?

Avatar

Community Advisor

Hi,

Please check for more info

Email API

or you can send a mail using MessageGatewayService

AEM - Custom Template'd Email 



Arun Patidar

Avatar

Level 4

@Component(service = Servlet.class, property = { "sling.servlet.methods=POST",

"sling.servlet.paths=/bin/contactServlet" })

public class ContactServlet extends HttpServlet {

private static final long serialVersionUID = -6506682026701304964L;

Logger logger = LoggerFactory.getLogger(this.getClass());

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

VerifyRecaptcha recaptcha = new VerifyRecaptcha();

String gRecaptchaResponse = request.getParameter("g-recaptcha-response");

String formData = request.getParameter("form-data");

logger.info("formData response" + formData);

String flag = request.getParameter("flag");

if (flag.equalsIgnoreCase("contactus")) {

// boolean verify = recaptcha.verify(gRecaptchaResponse);

// if (verify) {

// logger.info("validated recaptcha");

// }

// else {

// logger.info("recaptcha validation failed");

// }

//

//

ContactEmailService obj = new ContactEmailService();

obj.sendEmail();

}

}

}

Avatar

Level 4

My bad I created object to call sendemail()

After lot of trial and error
I replaced below

ContactEmailService obj = new ContactEmailService();

obj.sendEmail();

with

@Reference

private ContactEmailService contactEmailService;

contactEmailService.sendEmail();

I was able to get the instance of the emailService

Avatar

Correct answer by
Employee Advisor

That's what I was assuming. Thanks for reporting back and confirming that it was indeed this problem.

Jörg