Email service setup

tahir1601

21-06-2019

Hey All,

I am stuck at a point, request someone to help me out.

I have setup email service by following the tutorial below

AEM Component To Send An Email Tutorial | A Developers Blog

Email API

however my requirement is i am passing values to emailservice in java class rather than in jsp. I am getting a null pointer error as below

21.06.2019 05:09:01.273 *ERROR* [0:0:0:0:0:0:0:1 [1561111741271] POST /bin/contactServlet HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught Throwable

java.lang.NullPointerException: null

at com.kohler.jacobdelafon.core.services.impl.ContactEmailService.sendEmail(ContactEmailService.java:31) [com.kohler.jacobdelafon.core:1.0.0.SNAPSHOT]

at com.kohler.jacobdelafon.core.servlets.ContactServlet.doPost(ContactServlet.java:48) [com.kohler.jacobdelafon.core:1.0.0.SNAPSHOT]

Below is my java class

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;

import com.sample.myapp.core.beans.EmailDetails;

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

public class ContactEmailService {

@Reference

EmailService emailService;

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

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

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

public void sendEmail() {

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", "abc@example.com" };

details.setRecipients(recipients);

List<String> failureList = emailService.sendEmail(details.getTemplatePath(), details.getEmailParams(),

details.getRecipients());

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

if (failureList.isEmpty()) {

logger.info("successfully sent");

} else {

logger.info("failed to deliver");

}

}

}