Play framework Mailer throwing NullPointerExceptio

2019-02-27 17:16发布

问题:

I am using play Mailer module. I am getting NullPointerException at mailerClient.send(email).

send mail code

import javax.inject.Inject;
import play.libs.mailer.Email;
import play.libs.mailer.MailerClient;
public class MailerService {
    @Inject MailerClient mailerClient;
    public void sendEmail() {
        Email email = new Email();
        email.setSubject("Activation Link");
        email.setFrom("from@gmail.com");
        email.addTo("to@gmail.com");
        email.setBodyText("hello");
        mailerClient.send(email);
    }
}

application.conf

 # Logger provided to your application:
logger.application=DEBUG
play{
  modules {
    enabled += "play.api.libs.mailer.MailerModule"
    #enabled += "play.api.libs.mailer.SMTPConfigurationModule"
  }
  mailer {
  host=smtp.gmail.com
  port=465
  ssl=yes
  tls=no
  user="my gmail username"
  password="gmail password"
  debug=yes
}
}

Also i have added "com.typesafe.play" %% "play-mailer" % "3.0.1" in build.sbt

Play version:- 2.4

java version: 1.8

I am adding stack trace

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[NullPointerException: null]]
    at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:265) ~[play_2.11-2.4.6.jar:2.4.6]
    at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:191) ~[play_2.11-2.4.6.jar:2.4.6]
    at play.api.GlobalSettings$class.onError(GlobalSettings.scala:179) [play_2.11-2.4.6.jar:2.4.6]
    at play.api.DefaultGlobal$.onError(GlobalSettings.scala:212) [play_2.11-2.4.6.jar:2.4.6]
    at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:94) [play_2.11-2.4.6.jar:2.4.6]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$9$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:151) [play-netty-server_2.11-2.4.6.jar:2.4.6]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$9$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:148) [play-netty-server_2.11-2.4.6.jar:2.4.6]
    at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36) [scala-library-2.11.6.jar:na]
    at scala.util.Failure$$anonfun$recover$1.apply(Try.scala:215) [scala-library-2.11.6.jar:na]
    at scala.util.Try$.apply(Try.scala:191) [scala-library-2.11.6.jar:na]
    at scala.util.Failure.recover(Try.scala:215) [scala-library-2.11.6.jar:na]
    at scala.concurrent.Future$$anonfun$recover$1.apply(Future.scala:324) [scala-library-2.11.6.jar:na]
    at scala.concurrent.Future$$anonfun$recover$1.apply(Future.scala:324) [scala-library-2.11.6.jar:na]
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) [scala-library-2.11.6.jar:na]
    at play.api.libs.iteratee.Execution$trampoline$.executeScheduled(Execution.scala:109) [play-iteratees_2.11-2.4.6.jar:2.4.6]
    at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:71) [play-iteratees_2.11-2.4.6.jar:2.4.6]
    at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:40) [scala-library-2.11.6.jar:na]
    at scala.concurrent.impl.Promise$DefaultPromise.tryComplete(Promise.scala:248) [scala-library-2.11.6.jar:na]
    at scala.concurrent.Promise$class.complete(Promise.scala:55) [scala-library-2.11.6.jar:na]
    at scala.concurrent.impl.Promise$DefaultPromise.complete(Promise.scala:153) [scala-library-2.11.6.jar:na]
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:23) [scala-library-2.11.6.jar:na]
    at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) [akka-actor_2.11-2.3.13.jar:na]
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) [akka-actor_2.11-2.3.13.jar:na]
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.6.jar:na]
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.6.jar:na]
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.6.jar:na]
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.6.jar:na]

**Caused by: java.lang.NullPointerException: null**
    at mailer.MailerService.sendEmail(MailerService.java:17) ~[classes/:na]
    at controllers.Application.sendMailTest(Application.java:14) ~[classes/:na]
    at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$10$$anonfun$apply$10.apply(Routes.scala:324) ~[classes/:na]
    at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$10$$anonfun$apply$10.apply(Routes.scala:324) ~[classes/:na]
    at play.core.routing.HandlerInvokerFactory$$anon$4.resultCall(HandlerInvoker.scala:136) ~[play_2.11-2.4.6.jar:2.4.6]
    at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$14$$anon$3$$anon$1.invocation(HandlerInvoker.scala:127) ~[play_2.11-2.4.6.jar:2.4.6]
    at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:70) ~[play_2.11-2.4.6.jar:2.4.6]

回答1:

It looks like you incorrectly initialize MailerClient.

The example shows that you need to inject it into the controller, that itself initialized by the Play Framework.

You inject it into your own MailerService class. I suppose that you do something like new MailerService() in the controllers.Application.sendMailTest. As a result, MailerClient does not inject into the MailerService class (Who will do this if you initialise MailerService yourself ?)

Solution: You need to inject MailerClient into the controller, like in the example, and then pass it into your MailerService class.

import play.libs.mailer.Email;
import play.libs.mailer.MailerClient;
public class MailerService {
  MailerClient mailerClient;

  public MailerService(MailerClient mailerClient){
    this.mailerClient = mailerClient;
  }

  public void sendEmail() {
    Email email = new Email();
    email.setSubject("Activation Link");
    email.setFrom("from@gmail.com");
    email.addTo("to@gmail.com");
    email.setBodyText("hello");
    mailerClient.send(email);
  }
}