CQ - MailServiceHelper Failed while sending email java.lang.NullPointerException
Hi guys,
I am trying to send email with Adobe CQ API.
But i am getting a nullpointer exception at this line: MsgGateway.send(htmlEmail);
This is my method that i am using to send.
import com.day.cq.mailer.MessageGateway;
import com.day.cq.mailer.MessageGatewayService;
import javax.mail.internet.InternetAddress;
..
...
....
public boolean sendHtmlEmail(SlingHttpServletRequest sling,
String fromMailAdress, List<String> recepientmailAddress,
String emailSubject, String htmlbodyMail) {
HtmlEmail htmlEmail = new HtmlEmail();
List<InternetAddress> emailAddress = new ArrayList<InternetAddress>();
try {
for (String recipient : recepientmailAddress) {
if (!StringUtil.isEmpty(recipient)) {
emailAddress.add(new InternetAddress(recipient));
log.error(recipient);
}
}
if (!StringUtil.isEmpty(fromMailAdress)) {
htmlEmail.setFrom(fromMailAdress);
}
htmlEmail.setTo(emailAddress);
htmlEmail.setSubject(emailSubject);
htmlEmail.setHtmlMsg(htmlbodyMail);
htmlEmail.setCharset("utf-8");
MessageGatewayService MsgService = getMessageGateWayService(sling);
MessageGateway<HtmlEmail> MsgGateway = MsgService.getGateway(HtmlEmail.class);
MsgGateway.send(htmlEmail); //nullpointer exception caught here
return true;
} catch (Exception e) {
log.error("Failed while sending email", e);
}
return false;
}
I have checked to ensure the bundle com.day.cq.cq-mail and my own bundle is running .
I am also sure that 'htmlEmail' is not null by retrieving the email subject via .getSubject and it did return me the subject.
I do not understand what is returning the nullpointerexception.
I also tried using
@Reference
private MessageGateway<HtmlEmail> msgGateway;
and then call send on msgGateway as msgGateway.send(htmlEmail)
but it returns a "Reference cannot be resolved to a type" error.
Thanks in advance !