@Reference EmailService emailService throwing null

tahir1601

23-06-2019

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

}

}

}

Accepted Solutions (1)

Accepted Solutions (1)

Answers (5)

Answers (5)

Jörg_Hoh

Employee

23-06-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?

tahir1601

23-06-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

tahir1601

23-06-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();

}

}

}