1 Reply Latest reply on Jul 19, 2013 11:34 AM by aditya p

    MailServiceHelper Failed while sending email java.lang.NullPointerException

    zacpow Level 1

      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.

       

      Thanks in advance !

        • 1. Re: MailServiceHelper Failed while sending email java.lang.NullPointerException
          aditya p

          The MessageGateway instance (MessageGateway<HtmlEmail> MsgGateway) is null therby throwing a null pointer.

           

          Instead of instanciating msgGateway as :

          MessageGatewayService MsgService = getMessageGateWayService(sling);

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

           

          Inject your MessageGateway as:

          @Reference

          private MessageGateway<HtmlEmail> msgGateway;

           

          and then call send on msgGateway as msgGateway.send(htmlEmail)

           

          ~ Aditya