@Reference EmailService emailService throwing null | Community
Skip to main content
Level 3
June 23, 2019
Solved

@Reference EmailService emailService throwing null

  • June 23, 2019
  • 6 replies
  • 3779 views

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

}

}

}

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by joerghoh

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

Jörg

6 replies

joerghoh
Adobe Employee
Adobe Employee
June 23, 2019

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?

arunpatidar
Community Advisor
Community Advisor
June 23, 2019

Hi,

Please check for more info

Email API

or you can send a mail using MessageGatewayService

AEM - Custom Template'd Email 

Arun Patidar
tahir1601Author
Level 3
June 24, 2019

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

}

}

}

tahir1601Author
Level 3
June 24, 2019

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

joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
June 24, 2019

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

Jörg

tahir1601Author
Level 3
June 24, 2019

Thanks for your input!!