Expand my Community achievements bar.

SOLVED

CQ - MailServiceHelper Failed while sending email java.lang.NullPointerException

Avatar

Former Community Member

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 !

1 Accepted Solution

Avatar

Correct answer by
Level 6

Hi,

first, it would be easier to read the code if you used the Java Coding convention and not name your variable names with capital letter first. As an example

MessageGatewayService MsgService = getMessageGateWayService(sling);

MessageGateway<HtmlEmail> MsgGateway = MsgService.getGateway(HtmlEmail.class);

would be

MessageGatewayService msgService = getMessageGateWayService(sling);

MessageGateway<HtmlEmail> msgGateway = msgService.getGateway(HtmlEmail.class);

That way it is easier to see what is a class and what is an instance. The first notation signals to me that the MsgService.getGateway(HtmlEmail.class) is a static method in the class MsgService.

 

Second: The preferred way of getting a reference to a service is to use annotations.

@Reference private MessageGatewayService messageGatewayService;

A good example on using the MessageGatewayService is found at http://labs.sixdimensions.com/blog/dklco/2012-08-20/sending-email-adobe-cq-api

/Ove

View solution in original post

3 Replies

Avatar

Level 2
First check your SMTP mail configurations in DAY CQ Mail Service (com.day.cq.mailer.DefaultMailService ). Then you can proceed as: 

    import com.day.cq.mailer.MessageGateway;
    import com.day.cq.mailer.MessageGatewayService;
    
    @Reference
    private MessageGateway<HtmlEmail> messageGateway;

    @Reference
    private MessageGatewayService messageGatewayService;
    
    messageGateway = messageGatewayService.getGateway(HtmlEmail.class);

    messageGateway.send(htmlEmail);

Avatar

Level 2

I would not recommend referencing a particular MessageGateway directly, instead I would recommend retrieving it from the ReferenceGatewayService:

http://labs.sixdimensions.com/blog/dklco/2012-08-31/messagegatewayservice-vs-messagegateway

Generally, the issue you're indicating means that you need to configure the Day CQ Mail Service, you can update it here:

http://localhost:4502/system/console/configMgr/com.day.cq.mailer.DefaultMailService

Avatar

Correct answer by
Level 6

Hi,

first, it would be easier to read the code if you used the Java Coding convention and not name your variable names with capital letter first. As an example

MessageGatewayService MsgService = getMessageGateWayService(sling);

MessageGateway<HtmlEmail> MsgGateway = MsgService.getGateway(HtmlEmail.class);

would be

MessageGatewayService msgService = getMessageGateWayService(sling);

MessageGateway<HtmlEmail> msgGateway = msgService.getGateway(HtmlEmail.class);

That way it is easier to see what is a class and what is an instance. The first notation signals to me that the MsgService.getGateway(HtmlEmail.class) is a static method in the class MsgService.

 

Second: The preferred way of getting a reference to a service is to use annotations.

@Reference private MessageGatewayService messageGatewayService;

A good example on using the MessageGatewayService is found at http://labs.sixdimensions.com/blog/dklco/2012-08-20/sending-email-adobe-cq-api

/Ove